Thread: Dynamic allocation of an array of strings

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    Dynamic allocation of an array of strings

    Hey,

    I am allocating a 2d array of char dynamically like this:
    Code:
    Params = (char **)calloc(NumParam, sizeof(char *));
    
       for(i=0; i<NumParam; ++i)
    	   Params[i] = (char*)calloc(NameLengthP,sizeof(char));
    it refuses to let me not typecast calloc.
    Then I read in value into the array like so (It's a binary input file):
    Code:
    for (i=0; i<NumParam; ++i)
       {
    	   j=0;
    
    	   do
    	   {
    		   fread(&Params[i][j], sizeof(char), 1, inp);
    		   j++;
    	   }
    	   while(Params[i][j] != ' ' && j<NameLengthP);
       }
    When I print the array using printf("%s", Params[i]), the strings are longer then allocated. and the output is:
    Code:
    BARO    ııııİİİİİİİİİİİİA
    V2201A00ıııı
    V4610M00ıııı
    SG      ıııı
    ..........
    instead of:
    Code:
    BARO    
    V2201A00
    V4610M00
    SG
    Where is this leak coming from?
    Last edited by earth_angel; 06-24-2005 at 09:34 AM.

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Gotta NULL terminate your data after your read.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > it refuses to let me not typecast calloc.
    Either
    - you didn't include stdlib.h (the warning was about casting int to ptr)
    - you're compiling your C code with a C++ compiler (the warning was about casting void* to some other ptr)

    > while(Params[i][j] != ' ' && j<NameLengthP);
    Should be
    while(Params[i][j] != ' ' && j<NameLengthP-1);
    If you fill up to j<NameLengthP, you've actually filled all the elements, and there is no longer a \0.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    That did it.

    Thanks,

    A.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to array with dynamic allocation
    By cfdprogrammer in forum C Programming
    Replies: 22
    Last Post: 04-07-2009, 09:56 AM
  2. dynamic 2D array allocation
    By deprekate in forum C Programming
    Replies: 5
    Last Post: 03-03-2009, 04:25 AM
  3. array of strings?
    By mc72 in forum C Programming
    Replies: 5
    Last Post: 11-16-2008, 12:15 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM