#168760 - moonlightcheese - Wed May 20, 2009 9:01 pm
if i declare a pointer to an object in a function and use malloc to allocate space for the size of the object... does the object die at the end of the function?
ex.
i think malloc'd memory isn't free'd at the end of a function, but i'm asking to make sure i'm not mucking things up. if i'm reading this right, it looks like memory that is claimed with malloc is reserved until explicitly free'd... right?
ex.
Code: |
typedef struct { int* foo; } bar; void myFunc(bar* myBar) { int* myInt=malloc(sizeof(int)); *myInt=4; myBar->foo=myInt; } int main() { bar* theBar=malloc(sizeof(bar)); myFunc(theBar); //would theBar contain a pointer to an integer 4? return 0; } |
i think malloc'd memory isn't free'd at the end of a function, but i'm asking to make sure i'm not mucking things up. if i'm reading this right, it looks like memory that is claimed with malloc is reserved until explicitly free'd... right?