When a variable is created in C, a memory address is assigned to the variable. The memory address is the location of where the variable is stored on the computer. When we assign a value to the variable, it is stored in this memory address.
To access it, use the reference operator (&), and the result represents where the variable is stored:
Example :- int myAge = 43;
printf(“%p”, &myAge); // Outputs 0x7ffe5367e044
Note :-The memory address is in hexadecimal form (0x..). You will probably not get the same result in your program, as this depends on where the variable is stored on your computer.
Why We Uses memory address:- Pointers are important in C, because they allow us to manipulate the data in the computer’s memory – this can reduce the code and improve the performance.
The Importance of Memory Management in C :-
This chapter explains dynamic memory management in C. The C programming language provides several functions for memory allocation and management. These functions can be found in the <stdlib.h> header file.
If you want more control over all this, you need dynamic storage allocation. C supports dynamic storage allocation, which is the ability to reserve memory as you need it and free that memory as soon as you’re finished using it. Many programming languages have automatic memory allocation and garbage collection that handle these memory management tasks. C, though, allows (and in some cases requires) you to be explicit about memory allocation with the following key functions from the standard C library:
Sr.No. | Function | Description |
1 | void *calloc(int num, int size); | · Alternate memory allocation options in C are calloc, which also clears the memory when it’s reserved. |
2 | void free(void *address); | This function releases a block of memory block specified by address. |
3 | void *malloc(size_t size); | This function allocates an array of num bytes and leave them uninitialized |
4 | void *realloc(void *address, int newsize); | This function re-allocates memory extending it upto newsize. |
Example :- #include <stdio.h>
int main(void)
{
// declare variables
int a;
float b;
char c;
printf(“Address of a: %p\n”, &a);
printf(“Address of b: %p\n”, &b);
printf(“Address of c: %p\n”, &c);
return 0;
}
Output :- Address of a: 0x7ffee63c8620
Address of b: 0x7ffee63c8624
Address of c: 0x7ffee63c861f