The difference between creating a class object with new and not new

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.