Explain dynamic memory allocation functions with an example.
Answers
Answered by
3
Hey there!
--------
☆ What is Dynamic Memory Allocation? ☆
C language requires the number of elements in an array to be specified at compile time. But we may not be able to do always. Our initial judgement of size, if it is wrong, may cause failure of the program or wastage of memory space.
Many language permit a programmer to specify an array's size at run time. Such language have the ability to calculate and assign, during execution, the memory space required by the variables in a program. This process of allocating memory at run time is known as dynamic memory allocation.
✰ Memory Allocation Functions ✰
《▪》malloc -: A block of memory may be allocated using the function malloc. The malloc function reserves a block of memory of specified size and returns a pointer of type void. This means that we can assign it to any type of pointer. It takes the following form :-
ptr = (cast-type *) malloc(byte-size);
ptr is a pointer of type cast-type. The malloc returns a pointer (of cast-type) to an area of memory with size byte-size.
• Example :-
x = (int *) malloc (100 *sizeof(int));
On successful execution of this statement, a memory space equivalent to "100 times the size of an int" bytes is reserved and the address of the first byte of the memory allocated is assigned to the pointer of x of type of int.
Similarly, the statement
cptr = (char*) malloc(10);
allocates 10 bytes of space for thr pointer cptr of type char. We may also use malloc to allocate space for complex data types such as structures. Example :-
st_var = (struct store *)malloc(sizeof(struct store));
where, st_var is a pointer of type struct store.
[Remember, the malloc allocates a block of contiguous bytes. The allocation can fail if the space in the heap is not sufficient to satisfy the request. If it fails, it returns a NULL. We should therefore, check whether the allocation is successful before using the memory pointer.]
《▪》calloc -: calloc is another memory allocation function that is normally used for requesting memory space at run time for storing derived data types such as arrarys and structures. calloc allocates multiple blocks of storage, each of the same size, and then sets all bytes to zero. The general form of calloc is :-
ptr = (cast-type *) calloc (n, elem-size);
Thr above statement allocates contiguous space for n blocks, each of size elem-size bytes. All bytes are initialized to zero and a pointer to the first byte of the allocated region is returned. If there is not enough space, a NULL pointer is returned.
The following segment of a program allocates space for a structure variable.
• • • •
• • • •
struct student
{
char name[25];
float age;
long int id_num;
};
typedef struct student record;
record *st_ptr;
int class_size = 30;
st_ptr = (record *)calloc(class_size, sizeof(record));
• • • •
• • • •
record is of type struct student having three members: name,age, and id_num. The calloc allocates memory to hold for 30 such records. We must be sure that the requested memory has been allocated successfully before using thr st_ptr. This may be done as follows :-
if(st_ptr == NULL)
{
printf("Available memory not sufficient");
exit(1);
}
《▪》free -: Compile time storage of variable is allocated and released by the system in accordance with its storage class. With the dynamic run time allocation, it is our responsibility to release the space when it is not required. The release of storage space becomes important when the storage is limited.
When we no longer need the data we stored in a block of memory, and we do not intend to use that block for storing any other information, we may release that block of memory for future use, using the free function:
free (ptr);
ptr is a pointer to a memory block, which has already been created by malloc or calloc. Use of an invalid pointer in the call may create problems and cause system crash.
《▪》realloc -: The previous allocated memory is not sufficient and we need additional space for more elements. It is also possible that the memory allocated is much larger than necessary and we want to reduce it. In both the cases, we can change the memory size already allocated with the help of the function realloc. This process is called the reallocation of memory. For example, original allocation done by the statement
ptr = malloc(size);
then reallocation of space may be done by the statement
ptr = realloc(ptr, newsize);
This function allocates a new memory space of size newsize to the pointer variable ptr and returns a pointer to the first byte of the new memory block. The newsize may be larger or smaller than the size. Remember, the new memory block may or may not begin at the same place as the old one. In case, it is not able to find additional space in the same region, it will create the same in an entirely new region and move the contents of the old block into the new block. The function guarantees that the old data will remain intact.
--------
☆ What is Dynamic Memory Allocation? ☆
C language requires the number of elements in an array to be specified at compile time. But we may not be able to do always. Our initial judgement of size, if it is wrong, may cause failure of the program or wastage of memory space.
Many language permit a programmer to specify an array's size at run time. Such language have the ability to calculate and assign, during execution, the memory space required by the variables in a program. This process of allocating memory at run time is known as dynamic memory allocation.
✰ Memory Allocation Functions ✰
《▪》malloc -: A block of memory may be allocated using the function malloc. The malloc function reserves a block of memory of specified size and returns a pointer of type void. This means that we can assign it to any type of pointer. It takes the following form :-
ptr = (cast-type *) malloc(byte-size);
ptr is a pointer of type cast-type. The malloc returns a pointer (of cast-type) to an area of memory with size byte-size.
• Example :-
x = (int *) malloc (100 *sizeof(int));
On successful execution of this statement, a memory space equivalent to "100 times the size of an int" bytes is reserved and the address of the first byte of the memory allocated is assigned to the pointer of x of type of int.
Similarly, the statement
cptr = (char*) malloc(10);
allocates 10 bytes of space for thr pointer cptr of type char. We may also use malloc to allocate space for complex data types such as structures. Example :-
st_var = (struct store *)malloc(sizeof(struct store));
where, st_var is a pointer of type struct store.
[Remember, the malloc allocates a block of contiguous bytes. The allocation can fail if the space in the heap is not sufficient to satisfy the request. If it fails, it returns a NULL. We should therefore, check whether the allocation is successful before using the memory pointer.]
《▪》calloc -: calloc is another memory allocation function that is normally used for requesting memory space at run time for storing derived data types such as arrarys and structures. calloc allocates multiple blocks of storage, each of the same size, and then sets all bytes to zero. The general form of calloc is :-
ptr = (cast-type *) calloc (n, elem-size);
Thr above statement allocates contiguous space for n blocks, each of size elem-size bytes. All bytes are initialized to zero and a pointer to the first byte of the allocated region is returned. If there is not enough space, a NULL pointer is returned.
The following segment of a program allocates space for a structure variable.
• • • •
• • • •
struct student
{
char name[25];
float age;
long int id_num;
};
typedef struct student record;
record *st_ptr;
int class_size = 30;
st_ptr = (record *)calloc(class_size, sizeof(record));
• • • •
• • • •
record is of type struct student having three members: name,age, and id_num. The calloc allocates memory to hold for 30 such records. We must be sure that the requested memory has been allocated successfully before using thr st_ptr. This may be done as follows :-
if(st_ptr == NULL)
{
printf("Available memory not sufficient");
exit(1);
}
《▪》free -: Compile time storage of variable is allocated and released by the system in accordance with its storage class. With the dynamic run time allocation, it is our responsibility to release the space when it is not required. The release of storage space becomes important when the storage is limited.
When we no longer need the data we stored in a block of memory, and we do not intend to use that block for storing any other information, we may release that block of memory for future use, using the free function:
free (ptr);
ptr is a pointer to a memory block, which has already been created by malloc or calloc. Use of an invalid pointer in the call may create problems and cause system crash.
《▪》realloc -: The previous allocated memory is not sufficient and we need additional space for more elements. It is also possible that the memory allocated is much larger than necessary and we want to reduce it. In both the cases, we can change the memory size already allocated with the help of the function realloc. This process is called the reallocation of memory. For example, original allocation done by the statement
ptr = malloc(size);
then reallocation of space may be done by the statement
ptr = realloc(ptr, newsize);
This function allocates a new memory space of size newsize to the pointer variable ptr and returns a pointer to the first byte of the new memory block. The newsize may be larger or smaller than the size. Remember, the new memory block may or may not begin at the same place as the old one. In case, it is not able to find additional space in the same region, it will create the same in an entirely new region and move the contents of the old block into the new block. The function guarantees that the old data will remain intact.
QGP:
I don't know C or C++, but I would say:
Answered by
0
Answer:
i thing this can help you for example only not for definition....
Attachments:
Similar questions