malloc (free) vs. new (delete)

new

  • allocate dynamic memory
  • syntax:
    // allocate memory to contain one single element
    pointer = new type
    // allocate a block memory for multiple elements
    pointer = new type [number_of_elements]
    
  • using new vs. normal declaration
    // using new to allocate dynamic memory
    int *foo = new int [5]; 
    // normal array declaration
    int foo[5] = {1, 2, 3, 4, 5};
    int foo[5][5];
    
  • Difference:
    • the size of a regular array is a constant value
    • the dynamic memory allocationallows to assign memory during runtime using any variable value as size. The dynamic memory is allocated from the memory heap.
    • int foo[n] is put on top of stack memory; declaration using new and malloc are put into heap memory.

Note: how to avoid unexpected modification? -- Make it a constant via:

MyType* const p = new MyType;
// The following two won't 
Mytype const* p = new MyType;
const MyType* p = new MyType;

delete

  • Once the dynamically allocated memory is no longer needed, it can be freed by operator delete.
  • syntax
    // releases memory of a single element allocated using new
    delete pointer;
    // releases the memory for arrays of elements using new[]
    delete[] pointer;
    
    Note the importance of delete[], it makes sure that the memory of the whole array is released.

dynamic memory in C

  • new and delete were not available in the C language.
  • C used malloc, calloc, realloc and free to handle dynamic memory. (also available in C++)
  • The memory blocks allocated by these functions are not necessarily compatible with those returned by new, so they should not be mixed; each one should be handled with its own set of functions or operators.

malloc

  • allocates a block of size bytes of memory
  • returns a pointer to the beginning of the block.
  • example
    int * buffer1, * buffer2, * buffer3;
    // note the typecasting
    buffer1 = (int*) malloc (100*sizeof(int));
    buffer2 = (int*) calloc (100,sizeof(int));
    buffer3 = (int*) realloc (buffer2,500*sizeof(int));
    free (buffer1);
    free (buffer3);
    
    Note: malloc() returns a void* because it just produces a patch of memory, not an object. C++ doen't allow a void* to be assigned to any other pointer, so it must be cast.

Q&A Summary

  • Q: What is the difference between new/delete and malloc/free?
    A: (1) malloc/free do not know about constructors and destructors. new and delete create and destroy objects, while malloc and free allocate and deallocate memory. (2) malloc requires a special "typecasting" while new does not.

  • Q: Why should I use new instead of trustworthy old malloc()?
    A:
    constructors/destructors;
    type safety: malloc() returns a void which isn't type safe. new Fred() returns a pointer of the right type (a Fred);
    overridability: new is an operator that can be overridden by a class, while malloc() is not overridable on a per-class basis.

null pointer vs. void pointer A null pointer is a value. It is a special reserved value used to indicate that the pointer is not pointing to anything conceptually. A void pointer is a type of pointer, void* . It is used to indicate that the storage location pointed to by the pointer has no specific type.

results matching ""

    No results matching ""