DEV Community

Tomislav Kraljic
Tomislav Kraljic

Posted on • Updated on

Dynamic Memory Allocation in C

In this article, I will explain how to dynamically allocate memory in C.

malloc()

  • This function allows us to dynamically allocate memory.
  • This function comes from the standard library.
  • You specify the number of bytes of memory you want to allocate as the argument.
  • It returns the address of the first byte of memory that is allocated.
  • Because you get an address returned, you must pass in a pointer.
  • Always call free() after done using malloc().
  • Syntax -> *ptr = (cast-type*) malloc(byte-size)*

Alt Text

  • You always want to check to see if you can allocate memory. If your computer is not able to, that is a major problem and you need to exit or abort from the program.
  • In this program, we are asking the user the total amount of numbers. This will allocate that many integers.
  • After that, we check to see if our pointer is pointing to NULL. If it is, then we abort from the program. This means that memory could not be allocated.
  • After that, we make a for loop, asking the user to input each number.
  • Then, we do another for loop to print each number to the console.
  • Finally, we call free() to free up the memory. This is because we are no longer needing it. This will prevent a memory leak.
  • When we dynamically allocate memory, it is our responsibility to manage that memory. If we don't, we will run into memory leaks.

calloc()

  • This function is very similar to malloc()
  • It creates contiguous blocks of memory, where malloc() creates only one.
  • It is slower than malloc()
  • It initializes the allocated memory blocks to zero, where malloc() does not.
  • It takes in two arguments : number of data items, and size of each data item.
  • Syntax -> *ptr = (cast-type*)calloc(num of elements, byte-size)*

Alt Text

  • This is very similar to the previous program, except instead of malloc(), we called calloc().
  • We set each data item with a initial value of zero.
  • If we tried to access the value with malloc(), we would get a segmentation fault error. However, with calloc(), we would get 0.

realloc()

  • This function enables you to reuse/extend memory that you previously allocated with malloc() or calloc().
  • Expects 2 arguments:
    • a pointer containing an address that was previously returned by a call to malloc or calloc.
    • Size in bytes of new memory you want allocated.
  • Transfers the contents of the previously allocated memory to the new allocated memory.
  • This function deallocates the old object and returns a pointer to a new object.

Alt Text

  • This is a simple program to demonstrate how realloc() works.
  • We create a char pointer that holds 15 characters. Then, we copy "tommy" to that string.
  • Then, we print the string and memory address of it.
  • After that, we reallocate more memory. Now, instead of holding 15 characters, now we are holding 25 characters.
  • We concatenate "kraljic" to the other string and print the contents of the string and the memory address.
  • You will notice, that there are two different memory addresses. This is because, we initially had the string in one memory address. However, when we called realloc(), we deallocated the old memory and allocated memory in a new address.
  • Finally, we call free to free up the memory so we don't have a memory leak.

Guidelines

  • Always, always, always call free() when you are done with the memory. We do not want memory leaks.
  • Avoid allocating lots of small amount of memory.
  • Only dynamically allocate memory if you need to.
  • Allocating many small blocks of memory will much more overhead than allocating fewer larger blocks.

Top comments (0)