
Pointers and Dynamic Memory Allocation in C
Understanding Pointers and Addresses
Pointers are variables that store memory addresses of other variables. They are powerful tools in C programming for direct memory access and manipulation.
int var = 10;
int *ptr = &var; // ptr now holds the address of var
printf("Value of var: %d\n", var); // Outputs: 10
printf("Address of var: %p\n", &var); // Outputs: address of var
printf("Value stored in ptr: %p\n", ptr); // Outputs: address of var
printf("Value pointed to by ptr: %d\n", *ptr); // Outputs: 10
Pointer Arithmetic
Pointers can be incremented or decremented to point to the next or previous memory location. This is useful for iterating through arrays.
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr; // Points to the first element of arr
printf("First element: %d\n", *ptr); // Outputs: 10
ptr++;
printf("Second element: %d\n", *ptr); // Outputs: 20
ptr--;
printf("First element again: %d\n", *ptr); // Outputs: 10
Pointers and Arrays
Pointers and arrays are closely related. The name of an array acts like a pointer to its first element.
int arr[3] = {100, 200, 300};
int *ptr = arr;
printf("Using pointer syntax:\n");
for(int i = 0; i < 3; i++) {
printf("%d ", *(ptr + i)); // Outputs: 100 200 300
}
printf("\nUsing array syntax:\n");
for(int i = 0; i < 3; i++) {
printf("%d ", arr[i]); // Outputs: 100 200 300
}
Dynamic Memory Allocation
Dynamic memory allocation allows you to allocate memory at runtime using functions like malloc
, calloc
, realloc
, and free
.
// malloc: Allocates memory of given size and returns a pointer to it.
int *ptr = (int *)malloc(5 * sizeof(int));
if(ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// calloc: Allocates memory for an array and initializes all bytes to zero.
int *ptr2 = (int *)calloc(5, sizeof(int));
if(ptr2 == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// realloc: Resizes previously allocated memory.
ptr2 = (int *)realloc(ptr2, 10 * sizeof(int));
if(ptr2 == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// free: Frees the allocated memory.
free(ptr);
free(ptr2);
Memory Leaks and How to Avoid Them
Memory leaks occur when allocated memory is not freed. This can lead to excessive memory usage and program crashes. Always ensure to free dynamically allocated memory.
int *data = (int *)malloc(100 * sizeof(int));
if(data == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Use the allocated memory
for(int i = 0; i < 100; i++) {
data[i] = i;
}
// Free the allocated memory
free(data);
data = NULL; // Good practice to avoid dangling pointers
Summary
In this post, we covered the basics of pointers and addresses, pointer arithmetic, pointers and arrays, dynamic memory allocation using malloc
, calloc
, realloc
, and free
, and how to avoid memory leaks. Pointers are powerful tools that give C programmers control over memory and are essential for dynamic data structures.
For further reading, consider these resources:
0 Comments: