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++
#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.
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++
#include <iostream>
using namespace std;
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
Scope of Variables in C++
In C++, the scope of a variable is the extent in the code upto which the variable can be accessed or worked with. It is the region of the program where the variable is accessible using the name it was declared with. Let's take a look at an example: [GFGTABS] C++ #include <iostream> using names
7 min read
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
How to return local variables from a function in C++
In C++, returning a local variable from a function may result in an error due to the deallocation of variable memory after some value is returned from the function. But there are some ways using which we can safely return the local variables or the address of local variables and manipulate the data
4 min read
Static Member Function in C++
The static keyword is used with a variable to make the memory of the variable static once a static variable is declared its memory can't be changed. To know more about static keywords refer to the article static Keyword in C++. Static Member in C++ Static members of a class are not associated with t
4 min read
C++ Variable Templates
Over the years C++ has undergone advancements to improve its capabilities and coding efficiency. One notable addition in C++14 is the introduction of variable templates. These variable templates allow you to create a group of variables or static data members that can be customized providing flexibil
4 min read
Overflow of Values While Typecasting in C ++
C++ datatypes have a specific size, that is, the number of bits that each of them occupies is fixed. Sometimes while writing code, we may end up assigning a value that is greater than the maximum capacity of the datatype to store. In that case, an overflow occurs, and depending on the datatype the c
3 min read
Why do we need reference variables if we have pointers
Pointers: A pointer is a variable that holds memory address of another variable. A pointer needs to be de referenced with * operator to access the memory location it points to.References: A Reference can be called as a constant pointer that becomes de referenced implicitly. When we access the refere
4 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
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
C++ Program that will fill whole memory
NOTE:We strongly recommend to try this code in virtual machine because it may hang your computer within 5 second Dynamic memory allocation in C/C++ refers to performing memory allocation manually by programmer. Dynamically allocated memory is allocated on Heap and non-static and local variables get
2 min read