Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Working with Pointers and Memory Management in C

August 7, 2024

Working with Pointers and Memory Management in C

Pointers are a powerful feature in the C programming language that allows you to work with memory directly. Understanding how pointers work and how to manage memory is crucial for writing efficient and bug-free code in C.

1. What are Pointers?

A pointer is a variable that stores the memory address of another variable. It “points” to the location in memory where the data is stored. In C, pointers are denoted by using the * symbol.

1
2
int x = 5;
int* ptr = &x;

In the example above, we declare an integer variable x and initialize it with the value of 5. Then, we declare a pointer variable ptr and assign it the memory address of x using the & operator.

2. Accessing Values using Pointers

To access the value stored at the memory location pointed to by a pointer, we can use the * operator.

1
printf("%d", *ptr); // Output: 5

In the code snippet above, *ptr dereferences the pointer, which means it gives you the value stored at the memory location it points to.

3. Pointers and Arrays

Pointers and arrays have a close relationship in C. When an array is declared, it is automatically converted into a pointer pointing to the first element of the array.

1
2
int nums[] = {1, 2, 3, 4, 5};
int* ptr = nums;

In the example above, ptr is a pointer to the first element of the nums array. We didn’t need to use the & operator because the array name itself represents the memory address of its first element.

4. Dynamic Memory Allocation

C provides functions like malloc, calloc, and realloc to allocate memory dynamically at runtime. Dynamic memory allocation is useful when you don’t know the required memory size at compile time.

malloc: Allocating Memory

The malloc function in C is used to allocate memory dynamically. It takes the size in bytes as an argument and returns a void pointer (void*) to the allocated memory block.

1
int* ptr = (int*) malloc(5 * sizeof(int));

In the code snippet above, we allocate memory for 5 integers and store the memory address in ptr. Note the cast from void* to int* to specify the type of data we’re working with.

calloc: Allocating and Initializing Memory

The calloc function is similar to malloc, but it also initializes the allocated memory to zero.

1
int* ptr = (int*) calloc(5, sizeof(int));

In the code snippet above, we allocate memory for 5 integers and initialize them with zero. Again, we cast the result to the appropriate pointer type.

realloc: Reallocating Memory

With realloc, you can resize previously allocated memory. It takes a pointer to the previously allocated memory, the new size in bytes, and returns a pointer to the reallocated memory block.

1
int* resizedPtr = (int*) realloc(ptr, 10 * sizeof(int));

In the code snippet above, we resize the memory block pointed to by ptr to accommodate 10 integers. The contents of the original memory are preserved, and the new memory block is returned.

5. Memory Deallocation

To release dynamically allocated memory, you should use the free function. Forgetting to deallocate memory can lead to memory leaks, where memory is wasted and not available for further use.

1
free(ptr);

In the code snippet above, we deallocate the memory pointed to by ptr. After calling free, the memory is returned to the system and can be reused.

Conclusion

Working with pointers and understanding memory management is an essential skill for C programmers. With pointers, you can manipulate memory directly, access values efficiently, and dynamically allocate and deallocate memory as needed. Proper memory management is vital for writing efficient and bug-free code. Remember to always deallocate dynamically allocated memory to avoid memory leaks.

I hope this blog post has provided you with a solid understanding of working with pointers and memory management in C. Happy coding!


➡️ Overview of nRF52 Development Tools


⬅️ ATmega-328 vs nRF52 vs ESP32: Choosing the Right MCU


Go back to Posts.