Thursday, January 20, 2011

12.Dynamic Memory Allocation


Introduce to dynamic memory allocation

In some programming contexts, you want to process data but dont know what size of it is. For example, you read a list of students from file but dont know how many students record there. 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 functions prototype to allow you to handle memory effectively. With dynamic memory allocation you can allocate and free memory as needed.

Getting to know the size of data

Before allocating memory, we need to know the way to identify the size of each data so we can allocate memory correctly. We can get the size of data by using sizeof() function. sizeof() function returns a size_t an unsigned constant integer. For example to get the size of integer type you can do as follows:
1sizeof(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.
01#include <stdio.h>
02  
03typedef struct __address{
04    int house_number;
05    char street[50];
06    int zip_code;
07    char country[20];
08  
09} address;
10  
11void main()
12{
13  
14    printf("size of int is %d byte(s)\n",sizeof(int));
15    printf("size of unsigned int is %d byte(s)\n",sizeof(unsigned int));
16    printf("size of short is %d byte(s)\n",sizeof(short));
17    printf("size of unsigned short is %d byte(s)\n",sizeof(unsigned short));
18    printf("size of long is %d byte(s)\n",sizeof(long));
19  
20    printf("size of char is %d byte(s)\n",sizeof(char));
21  
22    printf("size of float is %d byte(s)\n",sizeof(float));
23    printf("size of double is %d byte(s)\n",sizeof(double));
24  
25    printf("size of address is %d byte(s)\n",sizeof(address));
26}
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)
byte(s)
size of long is 4 byte(s)
size of unsigned short is
2size of char is 1 byte(s)
size of float is 4 byte(s)
(s)
size of double is 8 byte(s)
size of address is 80 byt
e

Allocating memory

We use malloc() function to allocate memory. Here is the function interface:
void * malloc(size_t size);
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 will return NULL if requested memory couldnt be allocated or size argument is equal 0. Here is an example of using malloc function to allocate memory:
1int* pi;
2int size = 5;
3pi = (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.
1if(pi == NULL)
2{
3  fprintf(stderr,"error occurred: out of memory");
4  exit(EXIT_FAILURE);
5}
Beside malloc function, C also provides two other functions which make convenient ways to allocate memory:
1void *calloc(size_t num, size_t size);
2void *realloc(void *ptr, size_t size);
calloc function not only allocates memory like malloc but also allocates memory for a group of objects which is specified by num argument.
realloc function takes in the pointer to the original area of memory to extend and how much the total size of memory should be.

Freeing memory

When you use malloc to allocate memory you implicitly get the memory from a dynamic memory pool which is called 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 free function to free memory. Here is the function prototype:
1void free(void *ptr);
You should always use malloc and free as a pair in your program to a void memory leak.

Dont Miss Another Post Connect With Us !

Enter your email address:

0 comments: