When are variables removed from memory in C ++?

I used C ++ a bit. I'm just not sure how memory management works, so here it is:

First of all, I don’t know how memory is not allocated in a function, for example:

int addTwo(int num)
{
    int temp = 2;
    num += temp;
    return num;
}

So, in this example, will a temporary error be deleted from memory after this function completes? If not, how is it done. In C #, a variable is deleted after its use. Are there other cases that I should be aware of?

thanks

+5
source share
11 answers

The local variable is temp“pushed” on the stack at the beginning of the function and “popped” out of the stack when the function is completed.

Here's a breakdown from a non-optimized version:

int addTwo(int num)
{
00411380  push        ebp  
00411381  mov         ebp,esp             //Store current stack pointer
00411383  sub         esp,0CCh            //Reserve space on stack for locals etc
00411389  push        ebx  
0041138A  push        esi  
0041138B  push        edi  
0041138C  lea         edi,[ebp-0CCh] 
00411392  mov         ecx,33h 
00411397  mov         eax,0CCCCCCCCh 
0041139C  rep stos    dword ptr es:[edi] 
    int temp = 2;
0041139E  mov         dword ptr [temp],2 
    num += temp;
004113A5  mov         eax,dword ptr [num] 
004113A8  add         eax,dword ptr [temp] 
004113AB  mov         dword ptr [num],eax 
    return num;
004113AE  mov         eax,dword ptr [num] 
}
004113B1  pop         edi  
004113B2  pop         esi  
004113B3  pop         ebx  
004113B4  mov         esp,ebp                 //Restore stack pointer
004113B6  pop         ebp  
004113B7  ret        

"" "" . , .. , .

+7

++ :

, , .

:

  • , new(), ().
  • , malloc(), - free().

++ RAII ( ), , .

RAII delete() free(), , " " .

+15

temp , . :

int *temp = new int(2);

,

delete temp;

, :

, " " - , , "" . , . , , , , , ", - ( ) . , .

, , , .

+11

.

addTwo, - ( ) .

temp - undefined.

+6

temp . , , .

++ #.

+4

, . . oyu.

++?

. - ( ), .

+1

,

new
malloc

++ . , .

int a = addTwo(3);

. . , malloc, .

,

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    
}

, , , .

.

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

, a b .

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

.

, refcounted objects.

+1

num, temp . , , , . Temp . num, , temp num, , .

0

C, ++ Stack.

, , , , u , .

, .

0

++ , - , , . , , , .. ~ MyClass.

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

If you declare a pointer to a function, then the pointer itself (4 bytes for 32-bit systems) gets corrected when it leaves the scope, but the memory that you could allocate using the new or malloc operator will be delayed - this is often known as memory leak.

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

If you really have to select an object through a new one that needs to be deleted when it leaves the scope, then you should use boost :: scoped_ptr, for example:

void foo() {
  boost::scoped_ptr<MyClass> object(new MyClass);
  object->makeWonders();
  // memory allocated by new gets automatically deleted here.
}
0
source

All Articles