malloc , calloc and realloc functions in c
Question
malloc , calloc and realloc functions in c
Solution
malloc(), calloc(), and realloc() are functions in C programming language that are used for dynamic memory allocation.
malloc(): This function is used to allocate a block of memory dynamically. The size of the memory block is specified in bytes as the argument. It returns a pointer to the first byte of the allocated space if the allocation is successful, or NULL if the allocation fails. The syntax is as follows:
void* malloc(size_t size);
calloc(): This function is similar tomalloc(), but it has two arguments: the number of elements and the size of each element. It also initializes the allocated memory to zero. The syntax is as follows:
void* calloc(size_t num, size_t size);
realloc(): This function is used to change the size of the memory block that was previously allocated usingmalloc()orcalloc(). It takes two arguments: a pointer to the memory previously allocated and the new size in bytes. The syntax is as follows:
void* realloc(void* ptr, size_t size);
Here is an example of how these functions can be used:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
ptr = (int*) malloc(5 * sizeof(int)); // Allocating memory for 5 integers using malloc()
if(ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
printf("Memory successfully allocated using malloc.\n");
// Get the elements of the array
for(int i = 0; i < 5; ++i) {
ptr[i] = i + 1;
}
// Print the elements of the array
printf("The elements of the array are: ");
for(int i = 0; i < 5; ++i) {
printf("%d, ", ptr[i]);
}
// Reallocate memory
ptr = realloc(ptr, 10 * sizeof(int));
printf("\nMemory successfully reallocated using realloc.\n");
// Get the new elements of the array
for(int i = 5; i < 10; ++i) {
ptr[i] = i + 1;
}
// Print the elements of the array
printf("The elements of the array are: ");
for(int i = 0; i < 10; ++i) {
printf("%d, ", ptr[i]);
}
free(ptr);
}
return 0;
}
In this example, we first allocate memory for 5 integers using malloc(), then we reallocate the memory to hold 10 integers using realloc(). Finally, we free the memory using free().
Similar Questions
What does malloc function do in C?
What is malloc
Specify the 2 library functions to dynamically allocate memory? malloc() and calloc() memalloc() and faralloc() malloc() and memalloc() alloc() and memalloc()
What does malloc function do in C?Marks : 1Negative Marks : 0Answer hereAllocates memory and initializes it to zeroAllocates memory without initializing itFrees allocated memoryReallocates memory
In terms of allocation failure, how does calloc behave compared to malloc?Marks : 1Negative Marks : 0Answer herecalloc returns NULL on failure, while malloc exits the program.Both calloc and malloc return NULL on failure.calloc exits the program on failure, while malloc returns NULL.Neither calloc nor malloc handles alloc
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.