Memory Allocation

7. Memory Allocation

7.1Why doesn'tthe code ``char *answer; gets(answer);'' work?

7.2I can't get strcat to work. I tried ``char *s3 = strcat(s1, s2);'' but I got strange results.

7.3But theman pagefor strcat says that it takes two char *'s as arguments. How am I supposed to know to allocate things?

7.5I have a function that is supposed to return a string,but when it returns to its caller,the returned string is garbage.

7.6Why am I getting``warning: assignment of pointer from integer lacks a cast''for calls to malloc?

7.7Why does some code carefully cast the values returned by malloc to the pointer type being allocated?

7.8Why does so much codeleave out the multiplication by sizeof(char) when allocating strings?

7.14I've heard that some operating systemsdon't actually allocate malloc'ed memory until the program tries to use it. Is this legal?

7.16I'm allocating a large array for some numeric work,butmalloc is acting strangely.

7.17I've got 8 meg of memory in my PC.Why can I only seem to malloc 640K or so?

7.19My program is crashing, apparently somewhere down insidemalloc.

7.20You can't use dynamically-allocated memory after you free it, can you?

7.21Why isn't a pointernullafter callingfree?

7.22When I call malloc to allocate memory for a local pointer, do I have to explicitly free it?

7.23When I freea dynamically-allocated structure containing pointers,do I have to free each subsidiary pointer first?

7.24Must I free allocated memory before the program exits?

7.25Why doesn't my program's memory usage go down when I free memory?

7.26How does free know how many bytes to free?

7.27So can I query the malloc package to find out how big anallocated block is?

7.30Is it legal to pass a null pointer as the first argument to realloc?

7.31What's the difference between calloc and malloc?

7.32What is alloca and why is its use discouraged?


top