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?