Link: http://blog.csdn.net/zhangxaochen/article/details/8032758
Here, "EMPTY class" is a class that does not mean anything or only contains non-virtual functions.
In the past, I was only impressed that sizeof (empty class) would output "1", but I still don't know why. Today, I happened to see:
"An empty class also needs to be instantiated. The so-called class instantiation is to allocate an address in the memory. Each instance has a unique address in the memory. Similarly, the empty class will be instantiated, so the compiler will implicitly Add a byte to the empty class, so that the empty class will have a unique address after instantiation. Therefore, the empty sizeof class is 1."
The four-byte alignment of other classes. Virtual tables occupy space. Child classes inherit the space occupied by the parent class, and static classes do not occupy class space.
In addition, the following code is used for Demonstration:
class Base{public:Base(){};virtual ~Base(){};void set_num(int num){a=num;}virtual int get_num(){return a;}private: int a; char *p;};class Derive:public Base{public:Derive():Base(){};~Derive(){}; virtual int get_num(){return d;}private:static int st; int d; char *p;char c;};int main(){ cout<<sizeof(Base)<<endl;cout<<sizeof(Derive)<<endl;return 0;}
Output:
12
24
========================================================== ============================
2. Placement new
I have never touched on C ++ syntax before. I recently saw this syntax when I was reading the source code of box2d.
To put it bluntly, it is an overload and global version of operator new. Unlike the common new operator, placement new specifies a memory location and then calls the class constructor on this memory, divide memory.
Therefore, placement new accepts two parameters: one is the specified memory address and the other is the class name. The prototype is as follows:
void *operator new( size_t, void *p ) throw() { return p; }
The usage is as follows:
void* mem=malloc(sizeof(B));B* bb=new (mem) B;
At first glance, there is a bracket inserted in the middle of New B, which is really awkward, but the advantage of this usage is to avoid the time consumed by the "Search for available memory when dynamically allocating memory" action, so he
"Placement new is very suitable for applications that require high time requirements and do not want to be interrupted during long running."
The source code related to placement new in box2d is as follows:
b2Fixture* b2Body::CreateFixture(const b2FixtureDef* def){ ...... b2BlockAllocator* allocator = &m_world->m_blockAllocator; void* memory = allocator->Allocate(sizeof(b2Fixture)); b2Fixture* fixture = new (memory) b2Fixture; .....}
We can see that the allocate (sizeof (b2fixture) function is equivalent to malloc, but box2d does its own memory management work. Because the physical engine always requires a large number of floating point operations, it also complies with the asserted that "a high time requirement and long-running does not want to be interrupted.
Link: http://blog.csdn.net/zhangxaochen/article/details/8032758
{Over }}