Memory allocation VS std * alloc and free

I prefer to use the std * alloc / free functions to allocate / free dynamic memory in my C programs. I wonder if there are good reasons to use the GLIB memory allocation functions instead of the standard ones.

I would be grateful if the community could point out situations where any of these decisions is the winner / winner. I am also interested in performance issues that I could hit if I use one or the other.

Thanks!

Edited for government platforms

These programs usually run on all types of Linux / Unix distributions, usually 64 bits compiled using gcc 4.2.

+7
performance c malloc memory glib
source share
3 answers

In my opinion, the most valuable difference between GLib functions and standard libraries is that GLib functions interrupt the program if distribution is not executed. No more checking if the return value with malloc() NULL ! In addition, there is no difference in the distribution strategy - g_malloc() calls malloc() internally, although as one of the other answers say here, this can be changed.

Another difference is that the GLib functions allow you to (rudimentary) check for memory leaks using g_mem_profile() .

GLib also has a slice distributor , which is more efficient if you select many blocks of equal size. This does not use the malloc() and free() systems, but again, this can be changed for debugging purposes.

+5
source share

If for some reason you want to manage the main allocation strategy yourself, you can use g_mem_set_vtable () to use your own functions instead of malloc () / free ().

This is possible using malloc / free also using magic links, but GLIB provides an explicit API for this, as well as the ability to add your own distribution and free registration using mem-profiler-table .

+4
source share

Depends on the underlying architecture. SCO Unix fe malloc follows a β€œbest fit” strategy that is optimized for memory but limits speed.

So, if your program depends on a special assumption on different systems / platforms, it is always good to control the malloc strategy.

+2
source share

All Articles