Geek class Live Lesson Note 1 C + + Object memory model (stack model)

Source: Internet
Author: User

Object Memory model

(stack) VS. Heaps (heap)

      • Stack
        • Automatically managed by the system to execute functions as units
        • Space size compile-time determination (parameters + local variables)
        • When the function executes, the system automatically assigns a stack
        • At the end of the function execution, the system automatically reclaims the stack immediately
      • Heap
        • Manually controlled by programmers in C + +
        • Manually assigning new and malloc
        • Manually release Delete and free
        • Global, no size limit overall
        • Easy to cause memory leaks

1. Myclass c (10); Stack object, space size is determined at compile time, function execution is finished, system is recovered immediately

2.

myclass* func () {    Myclass C (ten);     return // }

3.

Myclass func () {    Myclass C (ten);    AClass A (+);          = &a;      //     return  C;          

Conclusion: The pointer to the Stack object, you should be extremely careful, general will have problems!

4

myclass* func () {    new  Myclass ();     return PA;   // }

Summary return pointer problem:

Return stack pointer: full error

Return heap pointer: Release may be a problem, easy to cause memory leaks

Therefore, it is generally not recommended

5 Heap Object Memory model

6. Stack Object Memory model

Two. Variable model with use (object, pointer, reference; declaration, parameter, return value)

1. Declarations and symbols

"*": when declaring, the pointer

Used in front of the pointer, the solution pointer

"&": when declaring, reference

Before the object, take the address

MyClass C;//object, must be on the stackMyClass* PC;//pointers, to ask yourself if you are a stack pointer, or a heap pointerMyClass& c2 = C;//Reference, ask yourself if you are a stack reference, or a heap reference. This example is a stack reference
Example Heap reference Myclass *PC2 = new Myclass (); myclass& C3 = *PC2; C3 for heap Reference
C= *PC;//a pointer to a heap object, or to a stack object//where the PC points, *pc is the object (heap/stack) to which to point//and C calls the copy constructor on the stack to complete C = *pcPC= &c;//Fetch Address

2 parameter Analysis

// Object void func1 (MyClass c) {    //  objects tend to be large, overhead, and generally not!  }// pointer void//  cost OK, but cannot distinguish heap/stack, with delete problem } // References void // recommended, do not want bidirectional pass with const}

const& is a good substitute for pass by value

void func3 (const myclass& MC) {}

Call Method:

MyClass C1;func1 (C1);   // Call Copy Construction // do not call copy construction func3 (C1);  // do not call the copy construction, note that the parameter is written value can, refer to note 2, the delivery person does not need to know how to accept the recipient. The random sign may become an address.

3. Return value analysis

Return Object Analysis:

MyClass func1 () {    MyClass C1;     return C1;  // correct, call copy construct    New MyClass ();     return *PC2;  // Call copy construction on return, PC2 after end, memory cannot be freed, there must be a memory leak }

Return pointer analysis:

myclass* Func2 () {    MyClass C1;     return &c1;   // Extreme error, return stack object pointer    New MyClass ();     return PC2;   // not recommended, may memory leak, violate who calls who release principle }

Therefore, the return pointer is generally not recommended

Return Reference Analysis:

myclass& func3 () {    MyClass C1;     return C1;   //   Extreme Error, the end of the    stack object dies New MyClass ();     return *PC2;  // There may be memory leaks, there are ways to get to the PC2, but the average person will not do, do not know to do }

Returns a reference to an incoming parameter, OK and recommended, common with this pointer

myclass& Func4 (myclass& c) {    return  C;}

Geek class Live Lesson Note 1 C + + Object memory model (stack model)

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.