What is the difference between memory leak, accessing freed memory and double free?

Viewed 581

I'm trying to figure out what is the difference between those three kinds of problems associated with memory models.

If I want to simulate a memory leak scenario, I can create a pointer without calling corresponding delete method.

int main() {
    // OK
    int * p = new int;
    delete p; 

    // Memory leak
    int * q = new int;
    // no delete
}

If I want to simulate a double free scenario, I can free a pointer twice and this part memory will be assigned twice later.

a = malloc(10);     // 0xa04010
b = malloc(10);     // 0xa04030
c = malloc(10);     // 0xa04050

free(a);
free(b);  // To bypass "double free or corruption (fasttop)" check
free(a);  // Double Free !!

d = malloc(10);     // 0xa04010
e = malloc(10);     // 0xa04030
f = malloc(10);     // 0xa04010   - Same as 'd' !

However, I don't know what is accessing freed memory. Can anybody give me an example of accessing freed memory?

4 Answers
Related