calloc and realloc, gotoIn C programming, dynamic memory allocation is used when you don’t know the amount of memory your program will need during its execution. This allows you to allocate memory at runtime, giving your programs more flexibility. The calloc() and realloc() functions are crucial for managing dynamic memory, while the goto statement offers an alternative flow control mechanism, although it's used less frequently due to concerns about readability and maintainability.
calloc() and realloc()Dynamic memory allocation in C is done using functions like malloc(), calloc(), realloc(), and free(), all of which are part of the C standard library defined in <stdlib.h>. Let's dive deeper into the two functions you're asking about: calloc() and realloc().
calloc() FunctionThe calloc() function allocates memory for an array of elements, initializing all of them to zero.
void* calloc(size_t num_elements, size_t element_size);
num_elements: Number of elements to allocate.element_size: Size of each element in bytes.calloc() allocates memory for the specified number of elements of the specified size, and also initializes each byte to zero.malloc().#include <stdio.h>
#include <stdlib.h>
int main() {
int* arr;
size_t n = 5;
// Allocating memory for an array of 5 integers
arr = (int*)calloc(n, sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed.\n");
return 1; // Exit if memory allocation fails
}
// Print the initialized array (should print 0 for each element)
for (size_t i = 0; i < n; i++) {
printf("arr[%zu] = %d\n", i, arr[i]);
}
free(arr); // Free allocated memory
return 0;
}
calloc() allocates memory for an array of 5 integers (n = 5), each of size sizeof(int).0, and the program prints the value of each element (which will be 0).free() is called to release the allocated memory.calloc()?malloc() and calloc() is that malloc() allocates memory but does not initialize it, whereas calloc() both allocates and initializes it to zero.realloc() FunctionThe realloc() function changes the size of a previously allocated memory block. It’s useful when you need to resize an array or a dynamically allocated block of memory.
void* realloc(void* ptr, size_t new_size);
ptr: A pointer to the memory block you want to resize.new_size: The new size (in bytes) to which the memory block should be resized.realloc() adjusts the size of the memory block that was previously allocated (using malloc(), calloc(), or realloc()).realloc() returns a new memory address.realloc() may change if the memory block is moved to a different location.#include <stdio.h>
#include <stdlib.h>
int main() {
int* arr;
size_t n = 5;
// Allocate memory for 5 integers
arr = (int*)malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
// Initialize the array
for (size_t i = 0; i < n; i++) {
arr[i] = i + 1;
}
// Resize the array to hold 10 integers
n = 10;
arr = (int*)realloc(arr, n * sizeof(int));
if (arr == NULL) {
printf("Memory reallocation failed.\n");
return 1;
}
// Print the resized array (new elements will be uninitialized or garbage)
for (size_t i = 0; i < n; i++) {
printf("arr[%zu] = %d\n", i, arr[i]);
}
free(arr); // Free allocated memory
return 0;
}
malloc().realloc().realloc()?goto StatementThe goto statement is used for unconditional branching in C. It allows the program to jump to a specific label within the function. However, goto should be used cautiously, as it can make code harder to read and maintain.
goto label;
...
label:
// Code to jump to
label: A user-defined label where the program control will jump.goto statement is often used for error handling or breaking out of deeply nested loops.goto for Error Handling#include <stdio.h>
int main() {
int num1 = 10, num2 = 0;
int result;
// Simple division with error handling using goto
if (num2 == 0) {
printf("Error: Division by zero!\n");
goto end; // Jump to the end of the program
}
result = num1 / num2;
printf("Result: %d\n", result);
end:
printf("Program ended.\n");
return 0;
}
goto is used to jump to the end label when a division by zero is detected, effectively avoiding further execution of the code after the error is caught.goto:if, for, while, and switch, instead of goto.| Concept | Description | Example Use Case |
|---|---|---|
calloc() |
Allocates memory for an array of elements, initializing them to zero. | Used when you need zero-initialized memory. |
realloc() |
Resizes an existing memory block to a new size. | Used to dynamically resize an array or memory block. |
goto |
Provides an unconditional jump to a labeled part of the program. | Used for error handling or breaking out of nested loops. |
Disadvantages of goto |
Makes the code harder to read and maintain; often leads to "spaghetti code." | Avoid using unless absolutely necessary. |
Dynamic memory allocation using calloc() and realloc() is essential for managing memory efficiently in C programs. calloc() is useful when you need initialized memory, while realloc() allows resizing dynamically allocated memory. On the other hand, the goto statement can be used for direct program flow control, but it should be used sparingly, as it can complicate the readability of your code. For most cases, structured control flow statements should be preferred over goto.
Open this section to load past papers