When there is only one instance of a variable we will

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: loc(m * sizeof(int)); for (i = 0; i < m; i++) y[i] = x[i]++; /* oops! x[i] is a word in a free block */ return y; } /* other calls to malloc and free go here */ Depending on the pattern of malloc and free calls that occur between lines 6 and 10, when the program references x[i] in line 14, the array x might be part of some other allocated heap block and have been overwritten. As with many memory-related bugs, the error will only become evident later in the program when we notice that the values in y are corrupted. 10.11.10 Introducing Memory Leaks Memory leaks are slow, silent killers that occur when programmers inadvertently create garbage in the heap by forgetting to free allocated blocks. For example, the following function allocates a heap block x and then returns without freeing it. 1 2 3 4 5 6 void leak(int n) { int *x = (int *)Malloc(n * sizeof(int)); return; } /* x is garbage at this point */ If foo is called frequently, then the heap will gradually fill up with garbage, in the worst case consuming the entire virtual address space. Memory...
View Full Document

This note was uploaded on 09/02/2010 for the course ELECTRICAL 360 taught by Professor Schultz during the Spring '10 term at BYU.

Ask a homework question - tutors are online