Domanda

Free() knows how many bytes of memory to free but can delete[] do same thing? What if we allocate from stack instead of heap, do they work flawlessly with free() and delete[] ? Last question: do we need to assign to NULL at the end?

#include <stdio.h>
#include <stdlib.h>

char * malloc2()
{
    char * m = (char *)malloc(100);
    //so malloc(10000000) cannot cause stack-overflow?
    //cast from void * to char *
    return m;
}

char * malloc3()
{
    static char m[100];
    //can [1000000] cause stack overflow?
    return m;
}

char * newX()
{
    char * output = new char[100];
    return output;
}

int main(){

    char * p = malloc2();
    //sizeof(p) gives 8 because this is on 64 bit OS/CPU
    free(p);
    //free() knows the size of p is 100.
    //Does it matter if this allocation from stack of malloc2()?
    p=NULL;

    char * q = malloc3();
    //again, sizeof(q) fives 8
    //is this allocation from stack of malloc3()?
    //I dont need to free because that was from an array declaration ?
    q=NULL;
    //then who  deletes that static array from the stack?


    char * r = malloc3();
    //now r and q point to same memory area ? 
    // then if I free q, I dont need to free r.
    r=NULL;

    char * s = newX();
    //allocation from stack again?
    //sizeof(s) gives 8, how can delete[] know its size?
    delete [] s;
    s=NULL;

    return 0;
}

Thank you.

È stato utile?

Soluzione

Neither free nor delete nor delete [] work with stack-allocated memory.

The rule is actually ridiculously simple:

  • Every malloc must be paired with exactly one free, and vice-versa.1
  • Every new must be paired with exactly one delete, and vice-versa.
  • Every new [] must be paired with exactly one delete [], and vice-versa.

The end.


1 Okay, I lied. malloc/free is harder, because there’s also calloc and realloc. Here’s the amended rule:

  • Every malloc or calloc must be paired with exactly one call to free or realloc.
  • Every realloc that does not free memory must be paired with exactly one call to free or realloc.
  • (Vice-versa: Every free must belong to exactly one call made to malloc, calloc or realloc.)

In other words, calloc behaves like malloc (for the sake of memory allocation). realloc is an inclusive intermediate link of the mallocfree chain – it can replace both malloc and free, and it can be put in between such calls.

Altri suggerimenti

You should never free or delete a stacked variable. They will automatically be destroyed when their stack frame returns. You may, only delete things allocated with new, and only free() things allocated by the malloc() family.

To answer the last question: storing NULL in a pointer variable is only needed if your design calls for it; it's used to indicate that a pointer doesn't point to anything. Doing it here is just a waste of time, because p goes away at the end of the function:

void f() {
    char *p = new char[10];
    delete [] p;
    p = NULL;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top