When are variables removed from memory in C++?

前端 未结 11 1042
梦如初夏
梦如初夏 2020-12-16 06:54

I\'ve been using C++ for a bit now. I\'m just never sure how the memory management works, so here it goes:

I\'m first of all unsure how memory is unallocated in a fu

相关标签:
11条回答
  • 2020-12-16 07:08

    In C++, any object that you declare in any scope will get deleted when the scoped exits. In the example below, the default constructor is called when you declare the object and the destructor is called on exit, ie, ~MyClass.

    void foo() {
      MyClass object;
      object.makeWonders();
    }
    

    If you declare a pointer in a function, then the pointer itself (the 4 bytes for 32 bit systems) gets reclaimed when the scoped exits, but the memory you might have allocated using operator new or malloc will linger - this is often known as memory leak.

    void foo() {
      MyClass* object = new MyClass;
      object->makeWonders();
      // leaking sizeof(MyClass) bytes.
    }
    

    If you really must allocate an object via new that needs to be deleted when it exits the scope then you should use boost::scoped_ptr like so:

    void foo() {
      boost::scoped_ptr<MyClass> object(new MyClass);
      object->makeWonders();
      // memory allocated by new gets automatically deleted here.
    }
    
    0 讨论(0)
  • 2020-12-16 07:11

    In this case both num and temp are local to this function. When the function is called the number passed into num is copied from the caller to a variable on the stack. Temp is then created on the stack. When you return the value of num is copied back to the caller, and the temp and num variables used in the function are dropped.

    0 讨论(0)
  • 2020-12-16 07:13

    Its not removed from memory once the function exits.

    It remains in memory, in addTwo's stack frame, until some other process (or the same) re uses that portion of memory.

    Until that point, accessing temp is undefined behaviour.

    0 讨论(0)
  • 2020-12-16 07:16

    In C++ there is a very simple rule of thumb:

    All memory is automatically freed when it runs out of scope unless it has been allocated manually.

    Manual allocations:

    • Any object allocated by new() MUST be de-allocated by a matching delete().
    • Any memory allocated by malloc() MUST be de-allocated by a matching free().

    A very useful design pattern in C++ is called RAII (Resource Acquisition Is Initialization) which binds dynamic allocations to a scoped object that frees the allocation in its destructor.

    In RAII code you do not have to worry anymore about calling delete() or free() because they are automatically called whenever the "anchor object" runs out of scope.

    0 讨论(0)
  • 2020-12-16 07:18

    In C,C++ local variables have automatic storage class and are stored in Stack.

    When function returns then stack gets unwound and locals are no more accessible, but they still persist in memory and that's the reason when u define a variable in function it may contain garbage value.

    It is just stack pointer manipulation in stack and on memory for local is removed actually.

    0 讨论(0)
  • 2020-12-16 07:20

    Normally memory managment is used in the context of dynamic memory that is created by

    new
    malloc
    

    In the normal code C++ behaves like every other language. If you create a variable or return it, it is copied and accessible on the target side.

    int a = addTwo(3);
    

    gets a copy of your returned value. If the returned value is a class copy operator called. So as long as you do not work with new and malloc you do not have to care about memory managment that much.

    One additional remark which is important

    void func(std::string abc)
    {
      // method gets a copy of abc
    }
    
    void func(std::string& abc)
    {
      // method gets the original string object which can be modified without having to return it
    }
    
    void func(const std::string& abc)
    {
      // method gets the original string object abc but is not able to modify it    
    }
    

    The difference of the three lines is very important because your program may spare a lot of time creating copies of input parameters that you normally didn't want to create.

    e.g.

    bool CmpString(std::string a, std::string b)
    {
      return a.compare(b);
    }
    

    is really expensive because the strings a and b are always copied. Use

    bool CmpString(const std::string& a, const std::string& b)
    

    instead.

    This is important because no refcounted objects are used by default.

    0 讨论(0)
提交回复
热议问题