Specifically the libraries of GSL and Raylib have special memory allocation functions that include allocs() and free() based on their respective structures and implementations. However, these are just specific examples, there are times when C memory allocations being used by different components interact and cause segmentation faults even when they are being used properly by the program running them. What is this problem called, why does it occur, and what are the best practices to keep memory allocations from interfering with each other?
there are times when C memory allocations being used by different components interact and cause segmentation faults even when they are being used properly by the program running them. What is this problem called, why does it occur, and what are the best practices to keep memory allocations from interfering with each other?
That's usually not the case. Memory allocations do not "interfere with each other" apart from heap memory fragmentation over time, which isn't an error state but can lead to slower access or memory waste.
More commonly there are bugs in the application using the library:
"Out of bounds" bugs, when the application is accessing arrays/pointers out of bounds. Usually leading to "seg fault" on *nix-based systems.
"Heap corruption" bugs, which is just another flavour of the above, since out of bounds access could cause corrupted data on the heap rather than a seg fault. But this error is also often caused by the application incorrectly assigning the pointer returned from the allocation function to some other memory location, then trying to use/free the invalid pointer.
"Memory leak" bugs, when the application is not calling the corresponding free() function after it is done using the object.
"Dangling pointer" bugs, when the application is accessing an object after calling the corresponding free() function in the library.
"Double free" bugs, where the corresponding free() function of the library is called twice, first on a valid object, next on an invalid object.
Best practices to avoid the mentioned bugs:
free(null_pointer)
is a safe no-op - it is guaranteed by C that nothing will happen if you try this.