Can I free something if it was declared like that: char str[100] ?
Thanks.
Can I free something if it was declared like that: char str[100] ?
Thanks.
"I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe
http://www.Bloodware.net - Developing free software for the community.
Nope. You can only free memory that is used by some pointer that was allocated by malloc (or a variant of it). char str[100] is declared on the stack, not on the heap, because the compiler knows the size of it at compile-time. It doesn't need freeing.
The heap and stack have always interested me, where can I read more on the subject?
Thanks.
"I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe
http://www.Bloodware.net - Developing free software for the community.
The heap is a block of storage that a program can allocate and deallocate when needed.
The system stack is a storage location for variables and function calls. Information is pushed on the stack when, for example, a function is called or variables are declared, and popped from the stack when the function exits.
Peter O. Programming tips
Lets say char str[100] is declared in a function so it has automatic storage duration:
void funcExample(void)
{
int x;
char str[100];
....(other things)
return;
}
Each time funcExample is called, an "activation record" is created.
An activation record is a data structure on the "stack" that holds the variables for an invocation of a function. The activation record holds a set of the variables of funcExample (x, and str);
Storage is allocated for that str. On return from funcExample, the storage is deallocated.
On the other hand, if char str[100] is declared outside of any function, str has static duration and external linkage. The array is allocated on the heap, but free cannot be used on it. It stays there until the program ends.
Hope that helps.
Last edited by voltson4; 06-29-2003 at 01:05 PM.
Thanks alot guys
"I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe
http://www.Bloodware.net - Developing free software for the community.