Crash Diary--C + + dangerous C_str ()

Source: Internet
Author: User

Author:zhxfl//date   : 2013.4.9//email  : [Email protected]//adress: http://blog.sina.com.cn/s/blog_a502f1a30101jqtp.html///////////////////////////////////#include <iostream > #include <cstdio>using namespace std;const std::string& Fun () {    string s = "myfuntion";    return S.c_str ();} int main () {    string s = Fun ();    printf ("%s", S.c_str ());    return true;}

1 C_str () is a temporary variableThis is a code that appears in our project, this is a code that does not necessarily crash, this kind of mistake may be unintentional, good looking up is not good to find. It can be explained here that the string c_str () is a temporary variable, and when the fun ends, he will be "cleared" (actually not erased). That is, return S.c_str () returns an invalid pointer. the 2 function returns whether the temporary variable is released first or the return value is copied first. Little headline is a little 2B, get some suspense .Obviously, the above already complete explanation of the main reasons for the error, write here if it is over, this blog will not make any sense, as a programmer, I still want to learn more about a problem (such as a small title). Fun in return is a temporary variable, but the fun argument is a string, you should do a deep copy, according to this, this is not a problem (the fact is that the window is not a problem, with the JNI on Android only to appear on the phenomenon of the will collapse). Now we're going to answer that question. Here's a hypothesis that you've figured out the difference between heaps and stacks. Everything here is related to the stack management of the function call process. 1) When entering the function, the function return address is pressed into the stack (at the end of the function according to this back to call the function of the place, you can understand it), then the program will apply for enough stack space.2) for the C_STR () variable, is placed in the stack inside the temporary variable, the function is finished, the stack space is recycled (if you understand a little assembly, you should know that the stack is recycled is actually the EBP becomes smaller only). The return parameter of the function is placed inside the EAX. This time back to the calling function, in accordance with the current eax to do a deep copy of the string, unfortunately the copy of the address has been invalidated.  

Crash Diary--C + + dangerous C_str ()

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.