RE: [suse-programming-e] Freeing memory
Hi Rau,
This might be an obvious question but I am a little unsure so I am >>going to go ahead and ask:
Thats fine. Even a genius asks questions.
When I create a pointer of array, and while declaring I initialize >>them, am I responsible of freeing them?
No, you don't have to free them. The only time you free a pointer is after you have set it to the return value of a malloc(), realloc() or calloc() function call in C. (since I didnt have to call >>malloc I think the OS releases it..) The PROCESS is what releases/obtains memory from the OS. Normally the process obtains a certain amount of memory from the OS and sets this aside as part of the "heap". This occurs because system calls are expensive. When you call malloc() in your program for instance then the memory is allocated from the heap. If the heap gets too small as the program is running and more memory is needed, then malloc() will invoke the brk() or sbrk() system calls to obtain more memory from the Operating System. Thats why it is always wise to free() memory that you don't need anymore. When you call free() memory is returned to the heap, not the operating system per say.
And where are they saved, in >>the heap ? Example:
char mypntrs[]= { "hi", "this", "is", "a", "test" };
Not in your example. There is a syntax error there, I think you meant to say: char *mypnts[] = { "hi", "this", "is", "a", "test" }; It depends where this is declared. If you declare it as a global (outside of any function definition), then it will remain on the heap and is statically allocated at compile-time. It will not be destroyed until the program terminates. If it is declared inside a block ({}), then it is allocated/deallocated upon entry/exit of that block. This part has to do with the stack. A good book to read on this subject is a book called "The C Programming Language" by Kernighan and Richie. ~~Nick (BTW, this is in C, I guess it applies to every language, the question I mean) _______________________________________________ Join Excite! - http://www.excite.com The most personalized portal on the Web!
participants (1)
-
Nicholas Parsons