Thread: dynamic allocation of string pointers

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    8

    dynamic allocation of string pointers

    The snippet below (or similar) compiles and runs OK but I am using Visual Studio C++ compiler. Are the lines where .nameFirst and .nameLast assigned kosher in ANSI C?

    Also I am concerned about the memory allocation for these string constants. Does the runtime system put them on the heap? It doesn't seem that they are really constants since they are not defined before runtime.

    Thanks,
    jvh

    Code:
    #include "stdlib.h"
    typedef struct
    {
     unsigned id;
     char* nameFirst;
     char* nameLast;
    } myList;
     
    
    int main(int argc, char* argv[])
    {
     myList* myListP;
     myListP = (mylist*) calloc(10, sizeof(myList));
     myListP[0].nameFirst = "Jack";
     strcpy(myListP[0].nameLast = "Aubrey";
    }

  2. #2
    Registered User
    Join Date
    Aug 2012
    Posts
    8
    Oops, no return statement in main(). Just threw this together in notepad....

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    3,629
    You failed to allocate space for first and last name variables if you wanted them to be C-Strings.

    The "=" does NOT copy the contents of a C-string; it just copies the address of the C-String.
    This is likely NOT what you want to do.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The first assignment is technically safe/kosher, it assigns the address of the string literal "Jack" to the variable nameFirst for the first struct in your array. However this is usually not what most people want to do, since the string literal is read-only, if you attempt to modify this value with the pointer pointing to the string literal you are likely to get a crash. If you know you are going to be assigning string literals like this it is safer to make the pointer a const char* so the compiler can tell you when you do such things. In most cases you'd likely want to allocate space and then strcpy the info to it.

    The second is a syntax error as it currently stands... assuming you wrote it the proper way as strcpy(myListP[0].nameLast,"Aubrey"); it would be syntactically correct but still non-kosher as it attempts to copy data into an uninitialized pointer. You would need to first allocate space for that element (or point it to an existing writable buffer of an appropriate size) before using strcpy to copy data into it.

    Avoid casting the return value of malloc/calloc/realloc calls in C, C++ would force you to do this but in C it is better to simply assign the return value of such calls directly to the pointer variable without a cast. You also used "mylist" in the cast when it should have been "myList", remember case sensitivity.
    Last edited by hk_mp5kpdw; 03-12-2014 at 01:41 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    8
    Thanks folks.

    Duh me, I knew better. Must be the rabies vaccine.

    Figured there would be syntax errors and typos as I just typed it up in notepad.

    Thanks, again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Pointers and dynamic memory allocation
    By Casanova411 in forum C Programming
    Replies: 6
    Last Post: 11-16-2013, 05:14 PM
  2. dynamic allocation of file* pointers
    By uz_mz in forum C Programming
    Replies: 18
    Last Post: 11-13-2013, 09:34 PM
  3. dynamic memory allocation using pointer to pointers
    By mp252 in forum C++ Programming
    Replies: 12
    Last Post: 06-22-2013, 05:34 PM
  4. Pointers and Dynamic memory allocation help needed
    By dave_the_bear10 in forum C Programming
    Replies: 3
    Last Post: 11-09-2011, 04:23 PM
  5. Replies: 16
    Last Post: 02-16-2011, 01:52 AM