[Sussex] More C programming help

sparkeh at btinternet.com sparkeh at btinternet.com
Thu May 12 22:32:36 UTC 2005


> Exploring this a bit I discovered that if I put the
> value of  X at 25, that is NULL just the first 25 
> elements, the  program compiles and runs fine and 
> Valgrind is also happy with it but if I put X=26
then 
> it crashes again. 

malloc(x) allocates x bytes.

You've allocated 100 bytes and are attempting to store
100 pointers to chars. However, pointers to chars
aren't 1 byte long - from what you say above, they're
4 bytes long (since 4 * 25 = 100).

So you really want to be allocating 100 * sizeof(char
*) - I think that's the root of the problem.

-Mark







More information about the Sussex mailing list