Copyright

Dynamic Memory Allocation: Definition & Example

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Stacks in C Programming: Structure & Implementation

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:03 What Is Dynamic Memory?
  • 0:58 Dynamic Memory Allocation
  • 1:29 Malloc()
  • 2:40 Calloc() & Realloc()
  • 3:19 Free()
  • 4:05 Lesson Summary
Add to Add to Add to

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Login or Sign up

Timeline
Autoplay
Autoplay
Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Avinash Srinivasan
In this lesson, we will learn about dynamic memory through definitions and examples. We will also learn about dynamic memory allocation using the C programming language along with C program examples.

What Is Dynamic Memory?

Memory allocation is a very important part of software development. When the program is loaded into the system memory, the memory region allocated to the program is divided into three broad regions: stack, heap, and code.

Program Memory Layout
Program Memory Layout

Stack region is used for storing program's local variables when they're declared. Also, variables and arrays declared at the start of a function, including main, are allocated stack space. Stacks grow from high address to low address.

Heap region is exclusively for dynamic memory allocation. Unlike stacks, heaps grow from low address to high address.

Code region can be further divided as follows:

  • BSS segment: stores uninitialized static variables
  • Data segment: stores static variables that are initialized
  • Text segment: stores the program's executable instructions

Dynamic Memory Allocation

Dynamic memory allocation refers to managing system memory at runtime. Dynamic memory management in C programming language is performed via a group four functions named malloc(), calloc(), realloc(), and free(). These four dynamic memory allocation functions of the C programming language are defined in the C standard library header file <stdlib.h>. Dynamic memory allocation uses the heap space of the system memory. Let's examine the four dynamic memory management functions in more detail.

Malloc()

Let's first look at malloc(). Malloc() is used to allocate a block of memory on the heap. Specifically, malloc() allocates the user a specified number of bytes but does not initialize. Once allocated, the program accesses this block of memory via a pointer that malloc() returns. The default pointer returned by malloc() is of the type void but can be cast into a pointer of any data type. However, if the space is insufficient for the amount of memory requested by malloc(), then the allocation fails and a NULL pointer is returned.

The function takes a single argument, which is the size of the memory chunk to be allocated. You can dynamically allocate an integer buffer using malloc() in the code that you see here:

int *buffer = (int *) malloc(SIZE_USER_NEEDS * sizeof(int));

In this statement, sizeof(int) will return 2 or 4 bytes as the default size of an integer type. Assuming a user wants a buffer to hold N integer objects, the above statement will dynamically allocate N*4 bytes on the heap. For clarity, if user input SIZE_USER_NEEDS=5, then 5*4=20 bytes will be allocated on the heap that can be accessed using the pointer *buffer.

Calloc() & Realloc()

Now let's look at calloc(). Calloc() allocates a user-specified number of bytes and initializes them to zero. Unlike malloc(), this function takes two arguments: the number of memory chunks to be allocated and the size of each memory chunk.

int *buffer = (int *) calloc(NUMBER_OF_ELEMENTS, sizeof(int));

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account
Support