In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to understand the problem of members and allocating memory in C++ class". In daily operation, I believe many people have doubts about how to understand the problem of members and allocating memory in C++ class. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the question of "how to understand the members and allocating memory in C++ class". Next, please follow the editor to study!
Catalogue
1. Memory completion
two。 Special case-- virtual function
3. Memory not to be considered
Summary
1. Memory completion
It is easy to manage the memory of the class (generated object). The total memory of the class is always a multiple of the maximum member byte size, and the insufficient memory will be made up.
The overall memory of a class is made up of areas with the largest member bytes of n blocks.
For member variables, the allocation of memory is in writing order (that is, construction order). If it is less than the maximum member size, it will be filled automatically.
If the latter variable cannot be placed in the memory of the previous variable, it will directly open up a maximum member byte size space to store itself, and the remaining space of the previous bit will be filled automatically.
Class A {/ / sizeof = 8 int a; char b; short c;}; class B {/ / sizeof = 12 char a; int b; short c;}
Most of the above class A _ int B is 4 bytes:
Class A, the first is int, 4 bytes are filled; the second char,1 byte, 3 bytes left; the third short,2 byte, after the second space, the remaining one byte is automatically completed, a total of 2'4'8 bytes
Class B, the first is a char,1 byte, with three bytes left; the second int,4 byte, the first one has not enough space left, so directly open up the next 4 bytes, and the remaining three bytes will be filled automatically; the third short,2 byte, with 2 bytes left, will be filled automatically, with a total of 3 bytes 4 bytes 12
two。 Special case-- virtual function
If there is a virtual function in a class, the class automatically generates a virtual function table pointer and 4 bytes to manage the virtual function.
No matter how many virtual functions a class has, it only has a four-byte pointer.
And this pointer is generated before other members, which has nothing to do with the order in which you write virtual functions.
For example:
Class A {/ / actually sizeof = 2 / 4 / 8 instead of 12 char a; virtual void f () {} short b;}
The above is equivalent to
Class A {T * table / / virtual function pointer char a; short b;}
According to the above method, we can get sizeof = 8.
Similarly, since the virtual function pointer is four bytes, if there are higher bytes in the member, the same memory is made up.
Class A {/ / sizeof = 8: 2 = 16 virtual void f () {} double a;}; 3. Memory not to be considered
The storage space occupied by each object is only the storage space occupied by the data part of the object (virtual function pointer and virtual base class pointer also belong to the data part), not including the storage space occupied by the function code.
Some seemingly members should pay attention to not counting memory.
Such as:
one。 Static functions and variables:
Static functions and variables do not belong to class management and are unified in the constant memory area.
two。 Member functions of non-virtual functions:
Not every object generates a member function. The member function of the class is in other memory areas. The address of the member function called by the object with the this pointer is fixed.
three。 The member function properties of friend function and non-virtual function are the same.
At this point, the study on "how to understand the problem of members and allocating memory in C++ class" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.