Thread: structures and allocation

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715

    structures and allocation

    Code:
    struct person
    {
    	int age;
    	char* name;
    };
    I've always used to work with this kind of structures like this:
    Code:
    int main ( void )
    {
    	struct person per;
    	/* some code */
    	per.name = malloc ( strlen ( "Test" ) + 1 );
    	strcpy ( per.name, "Test" )
    	/* some code */
    }
    I always first allocate memory, but often I see this form:
    Code:
    /*...*/
    	per.name = "Test"
    /*...*/
    Is there any problem with the latter way? I think there is but...
    Can you comment this?

    Thanks
    Last edited by Micko; 12-02-2005 at 02:47 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Code:
    per.name = "Test"
    Depending on your compiler "test" may reside in memory that is marked as read only. You shouldn't try to alter this and shouldn't try to free this memory.

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    That's Ok, but will that cause possible problems with segmentation faults??
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  4. #4
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Quote Originally Posted by Micko
    That's Ok, but will that cause possible problems with segmentation faults??
    not untill you try to modify it

  5. #5
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    To be more specific, every structure references the same copy of the string "test". In many systems, string literals (that is, source-level quoted strings) are stored as read-only data, so any attempt to modify the string will either cause a segmentation fault of some sort or modify the name string for each person struct initialized in this manner.
    Insert obnoxious but pithy remark here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 09-21-2008, 04:18 PM
  2. Mysterious memory allocation problem
    By TomServo1 in forum C Programming
    Replies: 7
    Last Post: 07-08-2007, 11:29 AM
  3. Help with file reading/dynamic memory allocation
    By Quasar in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2004, 03:36 PM
  4. reallocating structures dynamically in functions
    By cezaryn in forum C Programming
    Replies: 3
    Last Post: 05-29-2003, 09:54 PM
  5. Memory allocation for structures
    By Lynux-Penguin in forum C Programming
    Replies: 3
    Last Post: 04-28-2002, 03:33 PM