• C Programming Video Tutorials

Memory Address in C



Memory Address in C

The memory address is assigned to a variable when a variable is declared in C language. C compiler stores the value of the variable in the different segments of the memory.

Segments of Memory

Different elements of a C program are stored in different segments of the computer’s memory, which has the following segments −

  • Text segment − A text segment, also known as a code segment or simply as text, is one of the sections of a progris used to store the object version of the C program.
  • Initialized data segment − The global variables and static variables that are initialized by the programmer are allocated the memory in the initialized data segment.
  • Uninitialized data segment − An uninitialized data segment also called as bss (stands for block started by symbol). The program allocates memory for this segment when it loads. Every data in bss is initialized to arithmetic "0" and pointers to null pointer by the kernel before the C program executes.
  • Stack − Stack is a LIFO (last in first out) data structure. Stack segment stores the value of local variables and values of parameters passed to a function. It also maintains the pointer to which a function call returns.
  • Heap − Heap is used for allocating memory during the runtime. All the functions that perform dynamic memory allocation deal with heap.

Accessing Memory Address

The memory addresses in C can be accessed or specified through the Address of (&) operator. To print a memory address using the printf() function, you need to use %p format specifier.

Syntax

Below is the syntax to access memory address −

&variable_name

Example

In the following example, we are declaring two variables and printing their memory addresses −

#include <stdio.h>

int main() {
   // Declaring two variables
   int a;
   int b;

   // Accessing their memory
   // addresses and print them
   printf("Memory address of a is %p\n", &a);
   printf("Memory address of b is %p\n", &b);

   return 0;
}

How Does C Compiler Allocate Memory?

Memory can be considered of as an array of bytes where each address is on index in the array and holds 1 byte.

When you declare a variable in a C program, the C compiler allocates a random memory location to it, depending on the size requirement, which depends on the type.

When an int variable is declared −

int x = 10;

The compiler assigns the value in a random byte address. Since an int type needs 4 bytes, the next four addresses are earmarked for it.

C allows you to find out which address has been allocated to a variable. You can use the %p format specifier to print the hexadecimal address of the memory location.

char x = 'A';
printf ("address of x: %p\n", &x);

This prints the address of "x" in hexadecimal format −

Address of x: 000000000061FE1F

Example

Arrays in C are contiguous memory areas that hold a number of values of the same data type (int, long, *char, etc.).

#include <stdio.h>

int main() {

   // initialize an array of ints
   int numbers[5] = {1,2,3,4,5};
   int i = 0;

   // print the address of the array variable
   printf("numbers = %p\n", numbers);

   // print addresses of each array index
   do {
      printf("numbers[%u] = %p\n", i, (void *)(&numbers[i]));
      i++;
   } while(i < 5);


   // print the size of the array
   printf("sizeof(numbers) = %lu\n", sizeof(numbers));
}

Output

When you run this code, it will produce the following output −

numbers = 0x7fff0815c0e0
numbers[0] = 0x7fff0815c0e0
numbers[1] = 0x7fff0815c0e4
numbers[2] = 0x7fff0815c0e8
numbers[3] = 0x7fff0815c0ec
numbers[4] = 0x7fff0815c0f0
sizeof(numbers) = 20
Advertisements