A small question about passing values

Source: Internet
Author: User
# Include <iostream> # include <windows. h> int * Fun (int * A) {STD: cout <* A <STD: Endl; // is there a problem ???? Int temp = 100; Return & temp; // This is a typical error} int main () {int A = 9; int * temp = fun (& ); // pass & A. Will this address expire in fun? Fun is still in another scope. I am afraid that the memory on this stack is invalid STD: cout <* temp <"\ t" <* temp; getchar (); return 0;} 1. first, the address of the local variable will not expire before the function exits. Therefore, the address of a will not expire when you call fun. from the stack perspective, we should first set up the main stack, press a on the stack, and then create the stack for fun. At this time, the main stack is okay, so there is no problem with access, and then fun returns, and fun's stack is destroyed, there is no problem with main. When main returns, the stack of main will also be gone, but this stack does not seem to have much to do with the stack on the data structure.

 

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.