DEV Community

Vikram Chaudhary
Vikram Chaudhary

Posted on

Datatype size in C++

Hello friends, We all work with data types and variable while writing a code in any programming language. But to use any data type for the variable without knowing much about the memory implication is not good.

Though we don't care much about the memory and its implication these days. As we have computers with large memory. But still we should be careful if we are dealing with large number of data types in our program.

In this article I will note down all the datatype and their memory requirement with code examples in C++. Our decision to use any variable depends on the maximum number of value a variable can hold and the bytes it use to store the value in memory.

Datatype and its size

Below are some of the most commonly used datatype and their size in bytes.

The commented code contains value of the variable.

Unsigned Integer

    int numBytes = 0;
    unsigned int myUnsignedInt = UINT_MAX;

    numBytes = sizeof(myUnsignedInt);
    cout << "Maximum value: " << myUnsignedInt << endl; //4294967295
    cout <<"Bytes used: " <<    numBytes << endl; //4

Unsigned long Integer

In case if the above value is not enough for you and you suspect that your variable may contain even bigger value. You can use unsigned long integer.

As we can see in the below code snippet, this type of variable can store large number but it comes up with memory trade of as it uses 8 bytes of memory.

    unsigned long int myUnsignedLongInt = ULONG_MAX;
    numBytes = sizeof(myUnsignedLongInt);
    cout << "Maximum value: " << myUnsignedLongInt << endl; // 18446744073709551615
    cout <<"Bytes used: " <<  numBytes << endl;//8

Unsigned long long Integer

For some compiles Unsigned integer and unsigned long integer can store the same number of maximum value. For those this is an alternate option.

For my compile the unsigned long and unsigned long long have same value.

    unsigned long long int myUnsignedLongLongInt = ULONG_LONG_MAX;
    numBytes = sizeof(myUnsignedLongLongInt);
    cout << "Maximum value: " << myUnsignedLongLongInt << endl; // 18446744073709551615
    cout <<"Bytes used: " <<  numBytes << endl;//8

Unsigned short int

If you know your variable may contain very less number, in that case it is always better to use this datatype. As we can see there would not be any memory trade of as it need only 2 bytes.

 unsigned short int myUnsignedShortInt = USHRT_MAX;
    numBytes = sizeof(myUnsignedShortInt);
    cout << "Maximum value: " << myUnsignedShortInt << endl; // 4197360
    cout <<"Bytes used: " <<  numBytes << endl;//2

Unsigned char

If your requirement is to use numbers up to 255 and be very efficient you can use unsigned char as shown in the below code. It uses only 1 byte of memory space to store the numbers up to 255.

Though it is primarily used to store characters but we can use to store integers as well.

    unsigned char myUnsignedByte = UCHAR_MAX;
    numBytes = sizeof(myUnsignedByte);
    cout << "Maximum value: " << (int)myUnsignedByte << endl; // 255
    cout <<"Bytes used: " <<  myUnsignedByte << endl;//1

Conclusion:

If we are working on memory intensive application in that case we should be aware of the different type of variable type and use them accordingly.

Top comments (1)

Collapse
 
pgradot profile image
Pierre Gradot • Edited

Hello,

I think you are missing 3 points here.

First, C++ offers an awesome template class to get the maximum value that a type can held: std::numeric_limits. Example:

#include <limits>

 std::cout << std::numeric_limits<unsigned char>::max() << '\n';

Second, there is a area where we do care about memory footprint: embedded systems == IoT. Furthermore, this is an area where C and C++ are by far the most used languages.

Last, and by far the most important: the results of your code in the article depend on the compiler and target CPU. This is a critical point in C and C++. See for instance the section Data models and the first chart on this page. The outputs you get are typical on a computer nowadays. But on embedded systems you will get different results.

It is important to tell people that types have a memory footprint. But it is more important to tell them that it is also complicated.