For the code below: (1) "main" calls a function "f1". (2) function "f1" does some number crunching; creates an array of "char" with malloc and then, returns the pointer of the array to the main (without de-allocating -freeing- the array).
I have 3 questions related to the case: (1) I assume, although the function "f1" has terminated, the allocated char array still stays allocated until the main program terminates completely. That is, the allocated memory still belongs to the main and no other process can access (I mean, interfere with) it from outside. Am I right? (2) Do I have to free the array (allocated in "f1") before the program terminates (or does it get freed as soon as the main program terminates) ? (3) If the answer for the second question is "yes" then how do you free an array allocated in another function?
note: I want to stay within the boundaries of pure c and not to spill over to c++.
char *f1 (...) {
...
...
char *fTmp = malloc (length1 * sizeof (char));
char *fData = malloc (length2 * sizeof (char));
...
...
free (fTmp);
return (fData);
}
int main () {
char *fData = f1 (...);
...
return (0);
}
I assume, although the function "f1" has terminated, the allocated char array still stays allocated until the main program terminates completely.
True. Dynamically allocated memory has nothing to do with functions, it belongs to process.
That is, the allocated memory still belongs to the main and no other process can access it from outside. Am I right?
Memory doesn't belong to main()
(intended as function) but to process itself (of which main()
is just the entry point). In a system with memory protection (where each process is isolated from the others) it's not accessible from outside. You can, however, allocate it in a system specific way to share memory across processes.
Do I have to free the array (allocated in "f1") before the program terminates (or does it get freed as soon as the main program terminates) ?
Yes. Unallocated memory - in most systems - is automatically deallocated by Operating System when process terminates but this is system dependant. IMO even when OS does it you should always deallocate, using such automatic deallocation as a red flag (I forget that to deallocate, is it a bug? something I missed?). Moreover if f1
is invoked 1000 times it'll leak memory each time quickly eating all available memory. Think about a process in a server, it may (and should) be up and running for years.
If the answer for the second question is "yes" then how do you free an array allocated in another function?
It's nice when who allocates memory also frees it. If it's not possible then caller will become responsible for such memory. It's, for example, what strdup()
does. In such case called function must return (somehow) a pointer to allocated memory (or an handle/token that can be used by another specialized function). For example:
char* pBuffer = f1();
// Use it
free(pBuffer);
Note that there are many many techniques if you want to hide such internal pointer. You may use a token (for example an integer, key in a dictionary), a typedef
or an opaque type.
Yes, memory allocated with malloc()
stays until it is freed. How else could a function ever return variable-sized data to its caller?
When a program exits, all the memory it allocated with malloc()
is freed. However, it's generally not a good idea to keep lots of unneeded memory around until the program terminates, as it can impact performance, or the system can run out of virtual memory. This can be a particular concern for long-running programs, their memory use sometimes keeps growing until they use of all the available virtual memory.
You call free()
on the pointer returned by the function. So in your case, main()
can do free(fData)
after it's done using the array.
This should all be covered in any C programming class or textbook.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With