Could someone please help me understand the concept of memory leaking and how specific data structures promote/prevent it (e.g. linked lists, arrays etc). I’ve been taught it twice by 2 different people a while ago – which has confused me slightly because of the differences in teaching methods.
Could someone please help me understand the concept of memory leaking and how specific
Share
Wikipedia has a good description on memory leaks. The defintion given there is:
For example, the following C function leaks memory:
The above function leaks
nbytes of memory as the programmer forgot to callfree(b). What that means is that the operating system hasnbytes less memory to satisfy further calls tomalloc. If the program callsleakymany times, the OS may eventually run out of memory that it could allocate for other tasks.As for the second part of your question, there is nothing intrinsic to data structures that makes them leak memory, but a careless implementation of a data structure could leak memory. As an example, consider the following function that deletes an element from a linked list: