Memory Allocation

Memory API malloc

  • Description:

    • Allocate a memory region on the heap.
  • Syntax: void* malloc(size_t size)

    • size_t size: the size of the memory block (in bytes)
    • size_t: is an unsigned integer type
  • Return:

    • Success: a void type pointer to the memory block allocated by malloc
    • Fail: a null pointer

Function sizeof

  • Description:

    • Usually use size in malloc instead of typing a number.

    • Two types of results of sizeof

      1
      2
      3
      4
      5
      6
      7
      8
      /* The actual size of 'x' is known at run-time */
      int *x = malloc(10 * sizeof(int));
      printf("%d\n", sizeof(x)) // 4 (size of a pointer)


      /* The actual size of 'y' is known at compile-time */
      int y[10]
      printf("%d\n", sizeof(y)) // 40 (y is static)

Memory API free

  • Description:

    • To free a memory region allocated by a call to malloc.
  • Syntax: void free(void *ptr)

    • void *ptr is a pointer to a memory block allocated with malloc
  • Return none

Memory API calloc

  • Description:

    • Allocate memory on the heap and set with zero before returning.
  • Syntax: void* calloc(size_t num, size_t size)

    • size_t num: number of block to allocate
    • size_t size: size of each block (in bytes)
  • Return:

    • Success: a void type pointer to memory block allocated by calloc
    • Fail: a null pointer

Memory API realloc

  • Description:

    • Change the size of memory block.
  • Syntax: void* realloc(void *ptr, size_t size)

    • void *ptr: pointer to memory block allocated with malloc, calloc or realloc
    • size_t size: new size for the memory block (in bytes)
  • Return:

    • Success: a void type pointer to memory block
    • Fail: a null pointer

Common Error

Forget Memory Allocation

  • Description:

    • created a pointer but forgot to allocate the memory
    • if operating (e.g. strcpy) that pointer, then it reports an error
  • Incorrect Code (Error):

    1
    2
    3
    4
    /* Incorrect Code */
    char *src = "hello"; // character string constant
    char *dst; // unallocated
    strcpy(dst, src); // segfault and die

  • Correct Code:

    1
    2
    3
    char *src = "hello";
    char *dst = malloc(strlen(src) + 1); // allocated
    strcpy(dst, src);

Insufficient Memory Allocation

  • Description:

    • do not allocate the pointer with enough memory
    • it works properly
  • Incorrect Code (but work properly):

    1
    2
    3
    char *src = "hello";
    char *dst = malloc(strlen(src)); // too small
    strcpy(dst, src);

Forget Initialization

  • Description:

    • use an uninitialized the pointer
    • it will allocate with the value used before
  • Incorrect Code:

    1
    2
    int *x = (int *)malloc(sizeof(int));   // allocated
    printf("*x = %d\n", *x); // uninitialized memory access

Memory Leak

  • Description:
    • do not free the used memory access
    • the program will run out of memory

Dangling Pointer

  • Description:
    • freeing the memory before it is finished using
    • the program accesses to that memory with invalid pointer

Double Free

  • Description:
    • Free the memory that was freed before
    • it reports an undefined error