In C # and Java, objects are generally used through new. We know that the New Keyword actually opens up a piece of memory in heap (HEAP, the object on the right of the equal sign is actually a memory address. If you don't believe it, you can print it out. It's not just that simple. Objects of an indefinite size are allocated on the stack, but the size of the basic data type (INT, short) is not fixed on the stack. The object size on the stack is fixed, the object on the stack is also referenced on the stack. If the object on the stack is not referenced on the stack, it will be recycled by GC. When learning OO development, teachers usually make us remember this, which is also the basis of OO development.
In C ++, we know that C ++ is not completely object-oriented. In C ++, both objects and object pointers are used, but the difference between the two is quite big.
In C ++, the pointer must be received to create a Class Object using new, that is, the object pointer. When new is used, the memory space is opened in heap, then return the memory address to the object pointer variable. The new object must be deleted, that is, manually released, otherwiseProgramMemory leakage occurs when you exit.
- The new class object needs to be received by the pointer, initialized in one place, and used in multiple places
- Delete and destroy the new class object after it is used.
- The newly created object directly uses the heap space, while the stack space is used for partial definition of class objects without the new definition.
- New object pointers are widely used, such as function return values and function parameters.
If you directly declare the class variables, such:
Ctest mtest;
In this method, the object is created on the stack and does not need to be manually released after use. This type of destructor is automatically executed.
Summary:
- The new class object needs to be received by the pointer, initialized in one place, and used in multiple places
- Delete and destroy the new class object after it is used.
- The newly created object directly uses the heap space, while the stack space is used for partial definition of class objects without the new definition.
- New object pointers are widely used, such as function return values and function parameters.
- Frequent calls are not suitable for new systems, just as new applications and Memory releases
Finally, we recommend a blog: Workshop.