I have a C program that allocates memory dynamically in main() and gives its address to pointer variables that are local to main(). I would prefer to allocate memory in functions and make main() as simple as possible. In order for that I would have to make pointer variables that point to allocated memory global. Then however, encapsulation of functions would be lost as they make a use of global variables, thus can't be reused in other program later. Are there best practices conventions regarding that? Should I maintain simple main() function with minimum logic in it or self-contained functions that don't depend on global variables. Are there any other reasons not to use global variables?
-
Perhaps few other SO posts may also be worth visiting for you: [1](http://stackoverflow.com/questions/8377944/hiding-c-struct-definition), [2](http://stackoverflow.com/questions/37531993/get-size-of-hidden-struct-c), [3](http://stackoverflow.com/questions/1154709/how-can-i-hide-the-declaration-of-a-struct-in-c). – user3078414 Jun 26 '16 at 09:14
-
You could put them in a structure. Then you could return a pointer to that structure to `main`, but that's the only pointer `main` would need. – Tom Karzes Jun 26 '16 at 09:24
-
You do not have to make it global!? Simply make them the return value of the function or pass them as out parameter. – Amin Negm-Awad Jun 26 '16 at 09:31
-
Note the difference between *local variables of `main()`* and *global variables*? – tofro Jun 26 '16 at 09:56
-
sure, my variables are local to main() now and thus not accessible from other functions directly contrary to global ones – LynnXe Jun 26 '16 at 10:17
2 Answers
You can return the pointer variable to main rather than making them global. Remember, memory allocated off the heap only goes out of scope when they are freed so they are not the same as local variables declared inside the function therefore it is safe to return them to caller, who can then use it and then free that memory.
The important thing is that memory allocated off the heap only goes out of scope when they are returned to the free store, and not necessarily when the function returns. By returning a pointer to that memory location, you can reuse it how you wish but then always remember to free it.
I hope this was helpful.
-
Note this is a "C" question according to the tags - There's no such thing as a `delete` operator in C. – tofro Jun 26 '16 at 09:57
-
It depends a lot on what you are doing.
If you are just using malloc in a few functions that aren't called a lot of times and are confident that you can make sure each pointer gets freed at appropriate times just return the pointer from the function.
A more elegant solution may be passing handles to functions. So declare pointer in main and pass it as a parameter by reference and have the function alter it.
If you have functions that are repeatedly called and use malloc and those pointers have to be freed erratically some sort of table which hold all the addresses and then return numeric ids instead of pointers can be elegant but look up garbage collection and think about your own allocators if this is the case. This can get a lot more complex and there is a lot of theory surrounding this kind of thing.
Most likely passing handles will work best for you though I think. Here is an example.
void test ( char* &p ){
p = (char *) malloc (10);
if ( p == NULL ) exit(1);
}
void main () {
char * p;
test(p);
free(p);
}

- 31