In modern programming, the size of any data is very important, while the programmers are trying to minimize data types or stream packages, global data transfer is increasing by the requests of new technologies. Thus, we need to know each data type in programming. The sizeof()
function is very useful to get the size of variables in bytes. C++11 extends the functionality of sizeof
function so that class or class members can be sent as parameters even if no object has been instantiated. In this post we explain with examples, what is the sizeof function, what is the extended sizeof() function, and how does C++11 extend the sizeof()
operator on classes?.
Table of Contents
What is the sizeof() function in C++?
The sizeof()
function is very useful to get the size of variables in bytes. Generally, size of data packages ( structs, classes, objects, arrays) need to be checked. When you create a struct or class, and If you are using them in I/O operations, check how much size it is.
Syntaxes for the sizeof operator is,
1 2 3 |
sizeof( type ) |
and,
1 2 3 |
sizeof expression |
Checking the size of data is sometimes important in professional programming. These checks allow you to optimize your data packages thus your application use a lower amount of memory and it will have more efficient and thus higher speed I/O operations. Game data packages, video and audio packages, TCP/IP data packages, AI related data packages are these kinds of packages.
Is there a simple example of sizeof operator in C++??
Here is a simple sizeof()
example,
1 2 3 4 |
char s[5]; int i = sizeof(s); |
What is the extended sizeof operator in modern C++?
C++11 extends the functionality of sizeof
function so that class or class members can be sent as parameters even if no object has been instantiated. You can access to the members of a class by using the ::
specifier. For example, we can get the size of a member in a class as below.
1 2 3 4 5 6 7 8 9 10 11 12 |
class Ta { public: int i = 0; }; int main() { int x = sizeof(Ta::i); } |
Note that this member should be a public member.
Is there an extended sizeof operator in modern C++?
We can check the size of different class types as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <iostream> class Ta { }; class Tb { }; class Tc : Ta, Tb { }; int main() { char s[5]; int i = sizeof(s); std::cout << "Size of Ta:" << sizeof(Ta) << " bytes\n"; std::cout << "Size of Tb:" << sizeof(Tb) << " bytes\n"; std::cout << "Size of Tc:" << sizeof(Tc) << " bytes\n"; system("pause"); return 0; } |
and the output will be as below:
1 2 3 4 5 |
Size of Ta:1 bytes Size of Tb:1 bytes Size of Tc:1 bytes |
Are there more examples of the extended sizeof operator in modern C++?
Here is an example how you can use extended sizeof with the classes inside namespaces:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> namespace NS { class Tx { public: int i; }; } int main() { std::cout << "Size of Tx:" << sizeof(NS::Tx) << " bytes\n"; std::cout << "Size of Tx.i:" << sizeof(NS::Tx::i) << " bytes\n"; system("pause"); return 0; } |
Here is another example that we can get size of member of a template, or size of an object member created by a template.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> template <class T> class Tx { public: int i; }; int main() { Tx<int> o1; std::cout << "Size of Tx.i:" << sizeof(Tx<int>::i) << " bytes\n"; std::cout << "Size of o1.i:" << sizeof(o1.i) << " bytes\n"; system("pause"); return 0; } |
For more information on this feature, see Extending sizeof Proposal document.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition