Using Dynamic Memory Management Functions;
http://siber.cankaya.edu.tr/OperatingSystems/cfiles/code45.c code45.c
Illustrates the standard library's memory management functions.
Lines 15-18 illustrate malloc() usage. It is attempted to allocate ten bytes of memory, check malloc()'s return value, display the contents of the uninitialized memory, and then return the memory to the heap.
Lines 20-22 repeat this procedure for calloc().
Rather than freeing d, however, it is attempted to extend it on lines 26-28. Whether realloc() succeeds or fails, it should still point to the string "foobar".
The pointer, e, as shown on lines 31-33, is allocated off the stack and, when main() returns (that is, when the program exits), its memory is automatically freed.
Write a program that containing the code segment above.
What kind of an error will you obtain? Why?
A Problem Child;
http://siber.cankaya.edu.tr/OperatingSystems/cfiles/code46.c code46.c.
C assumes you know what you are doing, most C compilers ignore
uses of uninitialized memory, buffer overruns, and buffer underruns.
Nor do most compilers catch memory leaks or dangling pointers.
Bugs in the program:
A memory leak (line 18),
Overruns the end of dynamically allocated heap memory (lines 22 and 28),
Underruns a memory buffer (line 32),
Frees the same buffer twice (lines 36 and37),
Accesses freed memory (lines 40 and41),
Clobbers statically allocated stack and global memory (lines 48 and 44, respectively)
These bugs can prevent the program from executing depending on the configuration (to allow core dumps), but leaks and clobbered memory usually show up as unpredictable behavior elsewhere in the program.