Dynamic allocation of an array of strings

This is a discussion on Dynamic allocation of an array of strings within the C Programming forums, part of the General Programming Boards category; Hey, I am allocating a 2d array of char dynamically like this: Code: Params = (char **)calloc(NumParam, sizeof(char *)); for(i=0; ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 03:25 AM
  3. array of strings?
    By mc72 in forum C Programming
    Replies: 5
    Last Post: 11-15-2008, 11:15 PM
  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21