Open In App

Representation of Int Variable in Memory in C++

Last Updated : 21 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

To understand the Representation of int variable in memory in C/C++ below is the basic program to create a variable and its size. Usually, the value of an int variable is stored in a series of bytes that are represented as the variable’s representation as memory. Depending on the hardware and compiler being used, an int variable may be represented by a different number of bytes.

Example:

C++




// C++ Program to create a variable and show its size.
#include <iostream>
using namespace std;
  
int main()
{
  
    int x = 10;
  
    cout << x << endl;
    cout << "size of x is : " << sizeof(x) << endl;
    return 0;
}


Output

10
size of x is : 4

Explanation of the above program

In the above program, we just created an int variable and assigned a value of 10 to it, then we just printed its value and the size of a variable into the output stream. Int variable allocates 4 bytes or 32 bits of memory by default. Out of these 32 bits, only one bit is a sign bit and the other 31 bits are data bits (this is only valid for signed int but in the case of unsigned int all 32 bits are data bits as it only stores positive numbers). The architecture of the int variable is shown below.

S              

                   A

               

                      B

               

                        C

               

                      D

Out of this 32-bit, the first bit of the first byte is the sign bit. if the signed bit is 0 then the data stored in these other 31 bits are positive but if the signed bit is 1 then the data is negative, so the computer will convert stored data in 2’s complement while accessing it.

How data is stored in 32-bit? 

Let’s consider we need to store 10 in memory. First, we need to convert 10 into 32-bit binary. The binary value of 10 (decimal) is 1010. Now to convert it to 32-bit binary add 28 zeros before 1010.

        10 (decimal) = 00000000   00000000   00000000   00001010
                          A          B          C          D

These 32 bits will not be stored in the allocated 4-byte memory directly. it will be stored according to endianness.

What is endianness?

Endianness is a term that describes the order in which a sequence of bytes is stored in computer memory.  Endianness is of two types :

  • Little Endianness
  • Big Endianness

1. Little Endianness

In little endianness, bytes of int variables are stored in reverse order ( DCBA order ). A computer with a little-endian microprocessor will work according to little-endianness. example of little-endian processors includes Intel, AMD, etc.

2. Big Endianness

In Big endianness, bytes of int variable is stored in the same order ( ABCD order ). A computer with a Big-endian microprocessor will work according to Big-endianness. example of big-endian processors includes Motrolla, power PC, etc. Considering our 4 bytes of data as A, B, C, and D . these 4 bytes will be stored in ABCD order or DCBA order will be decided by endianness.

For Big-endian processors, 10 will be stored like the below representation:

0 0 0 0 0 0 0 0

                   A

0 0 0 0 0 0 0 0

                       B

0 0 0 0 0 0 0 0

                       C

0 0 0 0 1 0 1 0

                          D

But for little-endian processors, 10 will be stored in memory as shown below:

0 0 0 0 1 0 1 0

                       D

0 0 0 0 0 0 0 0

                          C

0 0 0 0 0 0 0 0

                          B

0 0 0 0 0 0 0 0

                           A

Example:

C++




// C++ Program to Display Bytes of an Integer Variable
#include <iostream>
using namespace std;
  
// Main function
int main()
{
    int num;
    
    cout << "Enter a Number : ";
    cin >> num;
  
    unsigned char* p = (unsigned char*)#
  
    cout << "First Byte of variable num : " << *p << endl;
    p++;
  
    cout << "Second Byte of variable num : " << *p << endl;
    p++;
  
    cout << "Third Byte of variable num : " << *p << endl;
    p++;
  
    cout << "Fourth Byte of variable num : " << *p << endl;
    return 0;
}


Output:

// for Big-endian 
Enter a Number : -534
First Byte of Variable num :  255
Second Byte of Variable num :  255
Third Byte of Variable num :  253
Fourth Byte of Variable num :  234

Output:

// for Little-endian
Enter a Number : -534
First Byte of Variable num :  234
Second Byte of Variable num :  253
Third Byte of Variable num :  255
Fourth Byte of Variable num :  255
(-534)10  = ( 11111111  11111111  11111101  11101010 )2 

Above is the 2’s complement of 534

Memory  representation according to Big-endianness:

1 1 1 1 1 1 1 1

                       A

1 1 1 1 1 1 1 1

                         B

1 1 1 1 1 1 0 1

                           C

1 1 1 0 1 0 1 0

                            D

Memory representation according to little-endianness:

1 1 1 0 1 0 1 0

                       D

1 1 1 1 1 1 0 1

                        C

1 1 1 1 1 1 1 1

                            B

1 1 1 1 1 1 1 1

                          A

Explanation

In the above program, we just took an integer variable as user input then we assigned the address of the integer variable to an unsigned character type pointer which will only point to the first byte of the integer then we print the pointer value and increase the pointer by one unit which will point to the second address of the integer variable and again print the value present in the second byte. Just repeat the process till the last or fourth byte of the integer variable. We will get data present in every byte of the integer one by one separately. If we get the bytes in reverse order then our computer has a little-endian processor otherwise a big-endian processor.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads