Free up Global Pointers in C

How do I properly free up global pointers, specifically character pointers?
Last edited on
Properly done would be to not use global pointers if at all possible.

Using globals just because they make something easier to program and maintain is not a good idea.
try to use char arrays when you can, as dynamic strings are a bug waiting to happen. If you have many MB of text, sure, but not for hello world etc.

try to not use globals at all.

const char* cjunk = "fubar"; //does not need to be freed. const may be optional?
char bettah[] = "forty two"; //you can edit this one, above you should not, though this one also has a small fixed length.

past that its simple enough: every call to malloc & family needs a call to free.
you can do that at the end of main, or in a cleanup function. But tracking all the stuff that happened can get hairy... I believe that C and C++ both work such that null pointers can be freed without incident, nothing happens. If that is true (sorry, my C is rusty), then set to null after any incidental frees makes it a little easier, main can free them all without looking and its generally good defensive programming to null out deleted pointers.

another way is your own memory management, you can allocate all the bytes you need up front and peel offsets from it as char*s and only have to deal with 1 object to free. A little tracking of how big each item is, and whether you want to try to reclaim unused sections. Wasting space by using fixed sized strings makes this simpler too.
Last edited on
Well in c dynamic memory is allocated with malloc()/calloc(). Before the program exists, this allocated memory should be freed using free(). Whether these pointers are global or not, the allocated memory still needs to be freed.

If these pointers are global, then any exit point from the program should free this memory. In a well behaved program this is just before the main() function terminating }. If exit()/abort() et al are used, then the memory should also be freed before each of these. In this case have a function that performs whatever clean-up is needed before the program exits and the call this function before every program exit point.

Note that only dynamic allocated memory needs to be freed. arrays (eg char str[100] ) do not have the used memory freed. Also like const char *str = "qwerty"; also doesn't have allocated memory freed.
I think there is no such thing as a "global" pointer. If you dynamically allocate some memory, e.g. via C-style malloc() function or via C++-sytle new operator, then you get a pointer to that memory block. And, in order to avoid memory leaks, you will have to de-allocate that memory, by using free() or delete (or delete[]), respectively, when the memory block is no longer needed. Where you store the pointer doesn't really matter.

Yes, you can store a pointer to dynamically allocated memory in a "global" variable. But that does not make it a different kind of pointer. It does not change anything about the requirement to de-allocate the dynamically allocated memory when you are done using it! The only thing that changes when you store the pointer in a "global" variable is that it becomes much more difficult to figure out who might be accessing that pointer...

In any case: Whenever you dynamically allocate some memory, you should always decide who "owns" that memory. The owner should be responsible for de-allocating the memory when it is no longer needed.

Furthermore, since you want to avoid "use after free" or "double free" vulnerabilities in your program, I suggest you explicitly set the "global" pointer variable to NULL after de-allocating the memory.

You can use tools like Valgrind (Linux) or Dr. Memory (Windows) to detect memory leaks in your program:
- https://valgrind.org/
- https://drmemory.org/
Last edited on
Thanks guys, I now got a better understanding of this matter.

also thanks kigar for the tool links re: memory leaks, this would really help.
Topic archived. No new replies allowed.