Go to C + + function return value, you must pay attention to the problem

Source: Internet
Author: User

In the final analysis, the problems faced by C + + require it to provide a variety of mechanisms to ensure performance, perhaps, in this lifetime can not see C + + safe and efficient self-memory garbage collection .....

Old program Ape will remind rookie, pay attention to the function return value, because, it is possible that your function returned data in subsequent use will be wrong. So what do functions pay attention to when returning a value?

This blog attempts to use the most concise general plain English, explaining the function return value problem.

C + + put memory to the program ape, but, please note that it does not put all the memory to you, to you only the heap of memory, that is, you through the malloc function and the new keyword application to the memory, in addition to these memory, other memory, you better not touch, better not touch, better not touch, The important thing to say three times.

If your function return value goes wrong in subsequent use, especially if the local variable inside the function is returned, then it is almost certain that you have touched the memory that should not be touched. At this time, you will feel very wronged ah, I did not ah. But the fact is, not wronged you, so, in order not to be sued by the bug Procuratorate, as a C + + program APE, you must learn to identify those memory can be touched, those memory is not touch.

[CPP]View PlainCopy
    1. Char *pstr = "This is the buffer text";
    2. return pstr;

If your function is so written, then congratulations, return the correct, because this pstr point to the constant storage area, where the memory, you can touch, but notice, this touch, just read, you want to modify, it is absolutely not.

[CPP]View PlainCopy
    1. Char buffer[] = "This is the buffer text";
    2. return buffer;

If your function is written like this, congratulations, wait for the bug in the federal prosecutor's office to sue you. Here the buffer point is the memory on the stack, this, you can not touch, the front of the pstr is like a park, park, everyone may come to play, but you can not park the rockery, you can not cut the park tree, you can only play, can not modify it, the memory on the stack, like a private garden, You can't go in without an outsider. So how to discriminate, the method is also simple, you see with the brackets, you should understand that this thing is on the stack, out of this function, you do not want to touch, you just dare to touch, the bug the federal Procuratorate will sue you.

[CPP]View PlainCopy
    1. Static char buffer[] = "This is the buffer text";
    2. return buffer;

If your function is so written, then congratulations, return to the correct, but just now not plainly said, here is a private garden, yes, but you notice, the front also added a static, as long as the addition of this keyword, it is equivalent to say the country has this private garden expropriation, then, It has changed from a private garden to a small garden in a static storage area, memory in the static storage area, and the state says that the static storage area is open to all of you.

The function returns a copy of the value, and the memory on the stack will be retracted at the end of the function. Inside the function, you can touch the memory on the stack, it is because this time you are in the home of the stack, that their home memory of the small garden of course allow you to visit, but the function is over, it is equivalent to you left the stack of the house, the stack of memory small garden door closed, how can you go in, you go in, you

However, there are always some strange phenomena that make you think you can still access the memory on the stack after the function ends.

We define a structural body

[CPP]View PlainCopy
    1. struct person
    2. {
    3. int age;
    4. }


Write a function

[CPP]View PlainCopy
    1. person* Getperson2 ()
    2. {
    3. Person p;
    4. P.age = 99;
    5. return &p;
    6. }


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

[CPP]View PlainCopy
    1. Person *p2 = Getperson2 ();
    2. cout<<p2->age<<endl;


You will find that this code can actually execute correctly! Inside the function Getperson2, p this variable is a local variable, must be applied on the stack, the return is &p, this is not the memory address on the stack, then why outside the function, but still can output age?

Although, after the end of the function, the object is destroyed, but not completely destroyed, it seems that the computer in the management of memory does not need to destroy an object so completely, you can output age, it is because that area, not completely destroyed, this small piece of memory (storage age of 4 bytes) has not changed. You can temporarily touch the memory, but sooner or later it is going to be a problem, if a moment, the computer intends to use this memory, found that you are illegal use, then will inevitably alarm, then the bug will sue you.

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

[CPP]View PlainCopy
  1. struct person
  2. {
  3. int age;
  4. char* name;
  5. Person ()
  6. {
  7. Name = new char (10);
  8. strcpy (name,"Sheng");
  9. }
  10. ~person ()
  11. {
  12. name = NULL;
  13. }
  14. };


[CPP]View PlainCopy
    1. person* Getperson2 ()
    2. {
    3. Person p;
    4. P.age = 99;
    5. return &p;
    6. }



[CPP]View PlainCopy
    1. Person *p2 = Getperson2 ();
    2. cout<<p2->age<<endl;
    3. cout<<p2->name<<endl;


This time, after the function is finished, the object is destroyed more thoroughly than the last time, although the age zone is not completely destroyed, but the name area is completely destroyed, if you visit the name of the area, it is inevitable error, it is like Ah, the private garden is closed, but the garden is very big, So not every place has installed cameras and alarms, such as the age of this area, so, you sneak in from the age of the area, the owner of the garden did not find, until the Garden Patrol Brigade to the age area patrol, found that you are here secretly cauliflower, the result is you hit the crash. and the name side area, in the ~person this destructor installed the camera and alarm, you just come, immediately call the police, and then you hit the crash.

Thousands of words, the function does not return to the memory address of the stack, remember, is the address, do not be frightened of all the variables inside the function is not afraid to return, as long as not the memory address of the stack, you can safely return.

Go to C + + function return value, you must pay attention to the problem

Related Article

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.