How to make C++ function returns work for you

  • 2020-06-03 07:20:15
  • OfStack

This article focuses on the return value of C++ functions, which you must pay attention to. Let's get down to business.

C++ is so complicated that you don't want to touch C++ before you touch python, because even the return value of a function is too large, which is the secret that even a skilled C++ who has worked for 3 years can't understand and use it accurately.

Ultimately, the problems C++ faces require it to provide a variety of mechanisms to ensure performance, and perhaps never in its lifetime will C++ be able to safely and effectively recycle its own memory garbage...

Old programmers always remind newbies to pay attention to the return value of a function, because, chances are, the data your function returns will be used incorrectly later. So what should a function pay attention to when it returns a value?

This blog tries to explain the return value of a function in plain English.

Apes C + + handed the memory to the program, however, please note that it can't give you all of the memory, just gave you the heap memory, is you through malloc function and new keyword apply to memory, in addition to these memory, other memory, you'd better don't touch, had better don't touch, don't touch the best, important things said 3 times.

If your function returns something wrong later, especially if it returns a local variable within a function, then you've almost certainly touched memory you shouldn't have. At this point, you feel like you're being wronged, and I'm not. But the truth is, you're not being wronged, so in order not to be sued by the bug Prosecutor's office, as an C++ programmer, you have to learn to discriminate between memory that can be touched and memory that cannot be touched.


char *pstr = "This is the buffer text";  
return pstr; 

If your function is written like this, then congratulations, it's returned correctly, because this pstr is pointing to constant storage, memory here, you can touch, but notice, this touch, just reading, you want to modify, you can't.


char buffer[] = "This is the buffer text";  
return buffer; 

If that's what your function says, congratulations, and wait for the FEDERAL prosecutor's office in bug to indict you. buffer pointing here is that the memory on the stack, this, that the untouchable pstr is in front of the park, park, everybody can come to play, but you can't have taken apart the rockery in the park, you can't cut the trees in the park, you can come to play, can't modify it, the memory on the stack, it's just like private garden, a stranger, you can't go in. Now, the way to do that is easy, if you see the brackets, you know, it's on the stack, you can't touch it outside of this function, if you touch it, the FEDERAL prosecutor's office in bug will Sue you.


static char buffer[] = "This is the buffer text";  
return buffer; 

If your function is so written, so congratulations you, return to the right, but not just now, this is a private garden, yes, but you look at, the front adds a static, just added the keyword, the requisition of the private gardens is equivalent to that country, so, it is transformed from private garden static storage area of a small garden, the memory, in the static storage zone countries, said the static storage area opening to the outside world, you can come.

The function returns a copy of the value, and the memory on the stack is retrieved at the end of the function. Inside the function, you can touch on the stack memory, it is because this time you are in the stack's home, the little garden of their memory, of course, allows you to access, but the end of the function, as you leave the home of the stack, stack the memory small garden door closed, how can you go in, you go in, will be bug federal court!!!

However, there is always a strange phenomenon that makes you think that you can still access the memory on the stack after the function is finished.

Let's define a structure


struct person 
{ 
  int age; 
} 

Let me write a function


person* getperson2() 
{ 
  person p; 
  p.age = 99; 
  return &p; 
} 

After getting the return value of the function, you can output the age of the object


person *p2 = getperson2(); 
cout<<p2->age<<endl; 

You'll notice that this code actually executes correctly! Inside the function getperson2, the variable p is a local variable that must be applied on the stack and returned is & p, it is the memory address on the stack, so why can output age outside the function?

Although, at the end of the function, the object is destroyed, but the destruction of the incomplete, also don't need that seems to be a computer in the management of memory thorough destruction of an object, you can output age, that is because the area, has not been completely destroyed, the 1 small piece of memory (storage age four byte) has not changed. You can touch this memory temporarily, but sooner or later something will go wrong. If at some point the computer tries to use this memory and finds out that you are using it illegally, it will call the police and the FEDERAL prosecutor's Office in bug will Sue you.

To make the problem more transparent, let's modify the structure 1


struct person 
{ 
  int age; 
  char* name; 
  person() 
  { 
    name = new char(10); 
    strcpy(name,"sheng"); 
  } 
  ~person() 
  { 
    name = NULL; 
  } 
}; 

person* getperson2() 
{ 
  person p; 
  p.age = 99; 
  return &p; 
} 

person *p2 = getperson2(); 
cout<<p2->age<<endl; 
cout<<p2->name<<endl; 

This 1 time, after the function, the object of the destruction of more than 1 times completely, though, age area or has not been completely destroyed, but name area was destroyed completely, if you visit name region, is bound to go wrong, it's like, private garden is closed, but the garden is so big, so not every 1 to install the cameras and alarm, such as age this area, so, you secretly from age sneaked in this area, the garden owner is not found, until the garden scouts scouts battalion to age area, I found you secretly here cauliflower, the result is to beat you collapse. name, on the other hand, has a camera and alarm in the ~person destructor, so you come in, you call the alarm, and you crash.

Function do not return the memory address pointing to the stack, remember, is the address, don't be scared all variables inside the function are afraid to return, as long as it is not the stack memory address, you feel free to return.

That's all I've learned in this article about how to make C++ function return values work for you, and I hope you found it helpful. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: