First page Back Continue Last page Overview Graphics
Malloc
Dynamic allocation of memory
- Very easy to break by overwriting stuff that you didn't allocate!
Often problem doesn't show up immediately
- Perhaps next time you do a malloc
- Perhaps when you are about to demo it to a customer......
Notes:
Malloc is used to allocate memory on the fly for dynamic data structures (e.g. Linked lists). But it is very easy to make mistakes.
The example program 'badmalloc' (gcc badmalloc.c -g -o badmalloc ) allocates memory but then writes over it and some memory after the allocated area. Running this under gdb might show a segfault in malloc itself (at a later call), or it might just happen to work if nothing important was overwritten.