I want to create a function that allocates space for a pointer (from main) to point at. If having the memory allocated in a function, will this memory still be available and safe to use after the that function returns? Here is an example of code:
int foo(int **number)
{
*number = (int*)malloc(sizeof(int));
}
int main()
{
int *myVar;
foo(&myVar);
}
So we see that the memory is allocated in the process of execution of the foo()
function.
Will the memory for the myVar
variable be allocated in "space" of the foo()
function, or in "space" of the main()
?
After finishing the execution of the foo()
, its memory is being cleared. Will myVar
point to the same "space"? Will it be safe to use?
Could it be overwritten by other function, or variable declarations?