Dynamic allocation

  • the function calls of dynamic allocation generate system calls

  • manual management of memory

  • useful for input management

<stdlib.h>

  • size_t: data type for an unsigned integer very large

  • void*: pointer to a general data type (usually 4 bytes on 32-bit systems, 8 bytes on 64-bit systems)

malloc

It allocates a portion of memory of size bytes

  • Not initialized

  • memory addressed by bytes (not typed)

void *malloc(size_t  size);

Organization of the allocated memory

To organize the memory area pointed to by a void* pointer, it can be used an implicit cast

  • Attempting to access memory areas outside the allocated portion can easily lead to a KILLED PROCESS by the operating system (because allocated areas are usually isolated from other defined variables)

  • When using malloc or realloc, be careful to ensure that the memory is a multiple of the number of data elements you want to use (for reading and writing). (Otherwise, when performing the implicit cast of the pointer to the memory area, unallocated memory regions could be included within a data element.)

calloc

It allocates a portion of memory of size×nmem bytes

  • Initialized to 0 (bit by bit)

  • Consists of nmem elements, each of size bytes

void  *calloc(size_t  nmem, size_t  size);										

The function returns NULL if the allocation fails

realloc

  • It allocates a new memory block of size bytes in total

  • It modifies the memory block previously allocated and pointed to by ptr

    • If ptr == NULL, it behaves like malloc

    • If size == 0, it frees the memory block

  • If it expands the memory block pointed to by ptr:

    • It behaves like malloc for the newly added bytes

    • The previously initialized bytes pointed to by ptr are left unchanged (same formatting and values)

  • The returned pointer may be different from ptr

    • In case of expansion, the function might need to move the expanded memory block (due to space constraints)

void  *realloc(void *ptr, size_t  size);										

where ptr is the pointer returned from malloc,calloc or realloc.

The function returns NULL if the allocation fails

free

  • It frees the allocated memory (pointed to by ptr)

    • if ptr == NULL, it does nothing

  • It works like realloc with size == 0

void free(void *ptr);						

where ptr is the pointer returned from malloc,calloc or realloc

Memory leak

Often, when changing the value pointed to by a pointer, if the memory is not deallocated, the previously pointed-to area may remain occupied.

This leads to a memory leak, which reduces the available memory for the program during execution.

Example:

p=malloc(..);			
q=malloc(..);			
q=p;

If the allocated area pointed to by q is not freed before changing the reference:

  • the previously pointed-to area cannot be accessed anymore (the pointers to that area will be not available anymore)

  • The garbage area still occupies memory and isn't deleted.

FREE THE MEMORY before changing the reference.

In Java, on the other hand, memory deallocation is automatically handled by the Garbage Collector.

Correct deallocation

  • Memory deallocation functions do not remove the pointer variables passed as arguments (the pointer ptr is passed by value, so a copy is created inside the function)

  • At most, I can assign the value NULL to the pointer after freeing the memory.

p = malloc(..);
q = malloc(..);

free(q);
q = p;

Last updated