Representation of Int Variable in Memory in C++
Last Updated :
21 Mar, 2024
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;
}
Output10
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.
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:
But for little-endian processors, 10 will be stored in memory as shown below:
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:
Memory representation according to little-endianness:
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.
Similar Reads
How to Read and Print an Integer value in C++ The given task is to take an integer as input from the user and print that integer in C++ language. In this article, we will learn how to read and print an integer value. In the below program, the syntax and procedures to take the integer as input from the user is shown in C++ language. Standard Inp
2 min read
Pointers vs References in C++ Prerequisite: Pointers, References C and C++ support pointers, which is different from most other programming languages such as Java, Python, Ruby, Perl and PHP as they only support references. But interestingly, C++, along with pointers, also supports references. On the surface, both references and
5 min read
C++17 - <memory_resource> Header C++17 introduced the <memory_resource> header, which provides a mechanism for customizing the allocation and deallocation of memory in C++ programs. This header defines the memory_resource class and several derived classes that implement different memory allocation strategies. The C++ Standard
4 min read
Memory Allocation in Static Data Members in C++ C++ allows defining static data members within a class using the static keyword. When a data member is declared as static, then we must keep the following note in mind: Irrespective of the number of objects created, only a single copy of the static member is created in memory. All objects of a class
4 min read
Iteration Over std::vector: Unsigned vs Signed Index Variable When we iterate over a std::vector in C++, we need to choose the right type for the index variable to maintain both the correctness and clarity of our code. A common question arises: Should we use an unsigned or signed integer for the index variable?In this article, we will learn the differences bet
4 min read