In C programming, efficient memory management and the use of pointers are crucial for creating robust and high-performance applications. This guide provides a comprehensive overview of different types of memory, pointers, references, dynamic memory allocation, and function pointers, complete with examples to help you master these concepts. Whether you are new to C or looking to deepen your understanding, this guide covers essential topics to enhance your coding skills.
There is 5 different types of memory that you will encounter when programming in C.
1. Text segment
Your compiled code is stored here. The machine code instructions for your program. The text segment is read only so you cannot change any data but you can access it. Further down we’ll talk about function pointers. Those pointers point to a function in this segment.
2. Initialized Data Segment
Global and static variables are stored here, with specific values before the program runs and stay accessible throughout the program.
The difference between static and global variables is the scope, static variables is accessible in the function or block, it is defined but global variables can be accessed from anywhere in your program.
Normal variables are removed after a function is done executing while static variables remain.
3. Unitialized Data Segment (BSS)
Basically the same as initialized data segment but consists of variables without a specific value assigned to it. They have a value of 0 by default.
4. Heap
Here you have dynamic memory that you as a programmer can manage at run time. You can allocate memory and free memory with functions like malloc, calloc, realloc and free.
5. Stack
You are probably somewhat familiar with the stack. The stack memory manages function executions by storing local variables, arguments, and return values. The memory in the stack is removed after the function is done executing.
In C you have different data types and the most common is int, float, double and char. I will not talk much about data types but the important thing is to know how many bytes a specific data type have in memory. Here is a list, keep it in mind.
Int: 4 bytes,
Float: 4 bytes,
Double: 8 bytes,
Char: 1 byte.
You can check the size of a data type with the sizeof() method.
When you assign a variable like;
int number = 5;
The system will store the value of 5 in memory. But where in memory?
In memory there is actually addresses, and that’s how you can keep track of the values you have stored.
A reference is a variable’s address. Quite cool, right?
To access the reference of a variable use & followed by the variable name.
To print the reference to the console, we use the p format specifier.
int number = 5; printf(“%d”, number); // prints out 5 printf(“%p”, &number); // prints out the ref: 0x7ffd8379a74c
You probably get another address printed out.
Now to keep track of that reference use a pointer to store the reference.
Create a pointer variable by using *.
int number; int* pointer = &number; printf(“%p”, pointer); // 0x7ffd8379a74c
To get the value from a pointer you can use dereference. To derefernce a value you use * before the pointer. So * is used both to create a pointer and dereference it, but in different contexts.
int number = 5; // create variable. int* pointer = &number //store the reference in the pointer printf(“%p”, pointer); // print the reference // 0x7ffd8379a74c printf(“%d”, *pointer); // dereference the value. // 5
This is powerful because with pointers you can pass values by reference and not copy values. This is memory efficient and performant.
When you pass values to a function as arguments in a high level language you copy the values but in C you can send a reference and manipulate the value directly in memory.
#includevoid flipValues(int *a, int *b) { // receives two pointers of type int int temp = *a; // dereference pointer a and store it’s value in temp *a = *b; // dereference pointer b and store in pointer a *b = temp; // dereference b and change value to the value of temp } int main(void) { int a = 20; int b = 10; flipValues(&a, &b); // pass the references of a and b printf("a is now %d and b is %d", a, b); // a is now 10 and b is 20 return 0; }
A pointer can be declared as int* name; or int *name; both styles are correct and interchangeable.
When you declare a variable like int num = 5; inside a function including the main function, that variable is stored in the stack, and when the function is done executing the variable is removed.
But now we will allocate memory dynamically in the heap. Then we have full control over how much memory we need, and it will persist until we deallocate it.
We can use the functions malloc or calloc to allocate memory.
Malloc takes one parameter representing the size of memory to allocate in bytes.
Calloc takes two parameters, the amount of items and how much memory in bytes each item occupies.
malloc(size)
calloc(amount, size)
calloc initializes all allocated memory to 0, while malloc leaves the memory uninitialized, making malloc slightly more efficient.
You can use sizeof to indicate how much memory you need. In the example we use malloc to allocate space for 4 integers.
int* data; data = malloc(sizeof(int) * 4);
Here is a visualization:
Pointer->[],[],[],[] [],[],[],[] [],[],[],[] [],[],[],[]
Therefore, pointer 1 moves the pointer one step to the right, referring to the next integer in memory, which is 4 bytes away.
int* data; data = malloc(sizeof(int) * 4); *data = 10; // change first value to 10. *(data 1) = 20; // change second value to 20. *(data 2) = 30; *(data 3) = 40; for(int i = 0; iThis is how an array work!
When you declare an array, the array name is a pointer to its first element.
int numbers[] = { 10, 20, 30, 40 }; printf("%p\n", &numbers); printf("%p", &numbers[0]); // 0x7ffe91c73c80 // 0x7ffe91c73c80As shown, the address of the array name is the same as the address of its first element.
Realloc
Sometimes you want to reallocate memory. A normal use case is when you need more memory then you initially allocated with malloc or calloc.
The realloc function takes 2 parameters. A pointer to where your data currently is located, and the size of memory you need.
int* pointer2 = realloc(*pointer1, size);The realloc function will first check if the size of data can be stored at the current address and otherwise it will find another address to store it.
It’s unlikely but if there is not enough memory to reallocate the function will return null.
int *ptr1, *ptr2; // Allocate memory ptr1 = malloc(4); // Attempt to resize the memory ptr2 = realloc(ptr1, 8); // Check whether realloc is able to resize the memory or not if (ptr2 == NULL) { // If reallocation fails printf("Failed. Unable to resize memory"); } else { // If reallocation is sucessful printf("Success. 8 bytes reallocated at address %p \n", ptr2); ptr1 = ptr2; // Update ptr1 to point to the newly allocated memory }Free memory
After you have allocated memory and don’t use it anymore. Deallocate it with the free() function with a pointer to the data to be freed.
After that, it is considered good practice to set the pointer to null so that the address is no longer referenced so that you don’t accidentally use that pointer again.
int *ptr; ptr = malloc(sizeof(*ptr)); free(ptr); ptr = NULL;Remember that the same memory is used in your whole program and other programs running on the computer.
If you don’t free the memory it’s called data leak and you occupy memory for nothing. And if you accidentally change a pointer you have freed you can delete data from another part of your program or another program.
Create a Vector (dynamic array).
You can use your current knowledge to create a dynamic array.
As you may know, you can use structs to group data and are powerful to create data structures.
#include#include struct List { int *data; // Pointer to the list data int elements; // Number of elements in the list int capacity; // How much memmory the list can hold }; void append(struct List *myList, int item); void print(struct List *myList); void free_list(struct List *myList); int main() { struct List list; // initialize the list with no elements and capazity of 5 elements list.elements = 0; list.capacity = 5; list.data = malloc(list.capacity * sizeof(int)); // Error handeling for allocation data if (list.data == NULL) { printf("Memory allocation failed"); return 1; // Exit the program with an error code } // append 10 elements for(int i = 0; i elements == list->capacity) { list->capacity *= 2; list->data = realloc( list->data, list->capacity * sizeof(int) ); } // Add the item to the end of the list list->data[list->elements] = item; list->elements ; } void print(struct List *list) { for (int i = 0; i elements; i ) { printf("%d ", list->data[i]); } } void free_list(struct List *list) { free(list->data); list->data = NULL; } Static data
Global variables.
When declaring a variable above the main function they are stored in the data segment and are allocated in memory before the program starts and persists throughout the program and is accessible from all functions and blocks.
#includeint globalVar = 10; // Stored in initilized data segment int unitilizedGlobalVar; // Stored in uninitialized data segment int main() { printf("global variable %d\n", globalVar); printf("global uninitilized variable %d", unitilizedGlobalVar); // global variable 10 // global uninitilized variable 0 return 0; } Static variables
They work the same as global ones but are defined in a certain block but they are also allocated before the program starts and persist throughout the program. This is a good way of keeping the state of a variable. Use the keyword static when declaring the variable.
Here is a silly example where you have a function keeping the state of how many fruits you find in a garden using a static variable.
#includevoid countingFruits(int n) { // the variable is static and will remain through function calls and you can store the state of the variable static int totalFruits; totalFruits = n; printf( "Total fruits: %d\n", totalFruits); } void pickingApples(int garden[], int size) { // search for apples int totalApples = 0; for(int i = 0; i Understanding Function Pointers in C.
Function pointers are a powerful feature in C that allow you to store the address of a function and call that function through the pointer. They are particularly useful for implementing callback functions and passing functions as arguments to other functions.
Function pointers reference functions in the text segment of memory. The text segment is where the compiled machine code of your program is stored.
Defining a Function Pointer
You define a function pointer by specifying the return type, followed by an asterisk * and the name of the pointer in parentheses, and finally the parameter types. This declaration specifies the signature of the function the pointer can point to.
int(*funcPointer)(int, int)Using a Function Pointer
To use a function pointer, you assign it the address of a function with a matching signature and then call the function through the pointer.
You can call a function of the same signature from the function pointer
#includevoid greet() { printf("Hello!\n"); } int main() { // Declare a function pointer and initialize it to point to the 'greet' function void (*funcPtr)(); funcPtr = greet; // Call the function using the function pointer funcPtr(); return 0; } Passing Function Pointers as Parameters
Function pointers can be passed as parameters to other functions, allowing for flexible and reusable code. This is commonly used for callbacks.
#include// Callback 1 void add(int a, int b) { int sum = a b; printf("%d %d = %d\n", a, b, sum); } // Callback 2 void multiply(int a, int b) { int sum = a * b; printf("%d x %d = %d\n", a, b, sum); } // Math function recieving a callback void math(int a, int b, void (*callback)(int, int)) { // Call the callback function callback(a, b); } int main() { int a = 2; int b = 3; // Call math with add callback math(a, b, add); // Call math with multiply callback math(a, b, multiply); return 0; } Explanation of the Callback Example
Callback Functions: Define two functions, add and multiply, that will be used as callbacks. Each function takes two integers as parameters and prints the result of their respective operations.
Math Function: Define a function math that takes two integers and a function pointer (callback) as parameters. This function calls the callback function with the provided integers.
Main Function: In the main function, call math with different callback functions (add and multiply) to demonstrate how different operations can be performed using the same math function.
The output of the program is:
2 3 = 5 2 x 3 = 6Thanks for reading and happy coding!
Отказ от ответственности: Все предоставленные ресурсы частично взяты из Интернета. В случае нарушения ваших авторских прав или других прав и интересов, пожалуйста, объясните подробные причины и предоставьте доказательства авторских прав или прав и интересов, а затем отправьте их по электронной почте: [email protected]. Мы сделаем это за вас как можно скорее.
Copyright© 2022 湘ICP备2022001581号-3