Subscribe Us

Dynamic Memory in C - example - exercise

Array - Definition Declaring Initializing advantage disadvantage

What is Dynamic Memory Allocation in c programming ?

In some programming contexts, you want to process data but don't know what size of it is. For example, you read a list of students from file but don't know how many student records in the file.
Yes, you can specify the enough maximum size for the the array but it is not efficient in memory management.
C provides you a powerful and flexible way to manage memory allocation at runtime. It is called dynamic memory allocation.

Dynamic means you can specify the size of data at runtime. C programming language provides a set of standard APIs to allow you to handle memory effectively. With dynamic memory allocation, you can allocate and free memory as needed.

dynamic memory allocation in c

When to use dynamic memory allocation in c ?

In the run time of a program dynamic memory allocation is use in C program.

Getting to Know the Size of Data

Before allocating memory, you need to know the way to identify the size of each data so we can allocate memory accordingly. You can get the size of data by using sizeof() function. The sizeof() function returns a size_t an unsigned constant integer. For example to get the size of integer type you can do as follows:

sizeof(int);

It returns 4 bytes in typical 32 bit machines. Here is a simple program which prints out the size of almost common C data type.


#include
typedef struct __address{
int house_number;
char street[50];
int zip_code;
char country[20];
} address;
void main()
{
printf("size of int is %d byte(s)\n",sizeof(int));
printf("size of unsigned int is %d byte(s)\n",sizeof(unsigned int));
printf("size of short is %d byte(s)\n",sizeof(short));
printf("size of unsigned short is %d byte(s)\n",sizeof(unsigned short));
printf("size of long is %d byte(s)\n",sizeof(long));
printf("size of char is %d byte(s)\n",sizeof(char));
printf("size of float is %d byte(s)\n",sizeof(float));
printf("size of double is %d byte(s)\n",sizeof(double));
printf("size of address is %d byte(s)\n",sizeof(address));
}

And you can see the output:

size of int is 4 byte(s)
size of unsigned int is 4 byte(s)
size of short is 2 byte(s)
size of unsigned short is 2 byte(s)
size of long is 4 byte(s)
size of char is 1 byte(s)
size of float is 4 byte(s)
size of double is 8 byte(s)
size of address is 80 byte(s)

Allocating Memory Using Malloc Function

We use malloc() function to allocate memory. Here is the function interface:

void * malloc(size_t size);

The malloc() function takes size_t as its argument and returns a void pointer. The void pointer is used because it can allocate memory for any type. The malloc() function returns NULL if requested memory couldn't be allocated or size argument is equal 0.
Here is an example of using the malloc() function to allocate memory:

int* pi;
int size = 5;
pi = (int*)malloc(size * sizeof(int));

sizeof(int) return size of integer (4 bytes) and multiply with size which equals 5 so pi pointer now points to the first byte of 5 * 4 = 20 bytes memory block. We can check whether the malloc function allocate memory space is successful or not by checking the return value.

if(pi == NULL)
{
fprintf(stderr,"error occurred: out of memory");
exit(EXIT_FAILURE);
}

Beside malloc function, C also provides two other functions which make convenient ways to allocate memory:

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

The calloc() function not only allocates memory like malloc() function but also allocates memory for a group of objects which are specified by the first argument num.
The realloc() function takes in the pointer to the original area of memory to extend and how much the total size of memory should be.

How to Free Dynamic Memory Allocation ?

When you use malloc() function to allocate memory, you implicitly get the memory from a dynamic memory pool which is known as heap. The heap is limited so you have to deallocate or free the memory, you requested when you don't use it in any more. C provides function free() to allow you to free memory.

Below is the function prototype:

void free(void *ptr);

You should always use malloc() and free() functions as a pair in your program to a void memory leak.

Post a Comment

0 Comments