Address _ free address space after function return

Source: Internet
Author: User

A few days ago, in the QQ group, I found some friends discussing why the value can be passed and the address can be passed after the function is returned. I am also curious,
So I followed it.

Int * sum (int x, int y)
{
Int;
A = x | y;

Return &;
} Obviously, this code has some problems. However, compiling in VC2008 is normal and running is normal.

The following is my test code:

# Include <stdio. h>

Int * sum (int x, int y)
{
Int;
A = x + y;

Return &;
}
Int getsum (int x, int y)
{
Return x + y;
}
Int main (void)
{
Int * x;
Int;
X = sum (10, 2 );

Printf ("* x = % d; x = % u \ n", * x, x );

A = getsum (1, 2 );
X = sum (10, 2 );
Printf ("* x = % d; x = % u \ n", * x, x );

* X = 100;
Printf ("* x = % d; x = % u \ n", * x, x );
Getchar ();
Return 0;
} The running result is as follows:

 

Is this the running result? But why? The two calls to the sum function return the same address.

For testing, I decided to change the code with exceptions.

# Include <stdio. h>

Int * sum (int x, int y)
{
Int;
A = x + y;

Return &;
}
Int getsum (int x, int y)
{
Int * p;
P = sum (10, 2 );

Printf ("* x = % d; x = % u \ n", * p, p );
Return * p;
}
Int main (void)
{
Int * x;
Int;

X = sum (10, 2 );
Printf ("* x = % d; x = % u \ n", * x, x );

A = getsum (1, 2 );

X = sum (10, 2 );
Printf ("* x = % d; x = % u \ n", * x, x );

* X = 100;
Printf ("* x = % d; x = % u \ n", * x, x );

Getchar ();
Return 0;
} The code is still strong and runs smoothly, as shown in the following code. It only calls the sum function in getsum, and the obtained address is different from that in the main function.

 

In addition, we can find that no matter how we call sum in the main function, the return address is always the same? What's going on?

If so, can I use this method to dynamically allocate addresses? Can we take a look at free. Unfortunately, what has failed?

 

Tip: the heap-Stack-exception interrupts the program.

Why?

The reason may be:

1. The address space of the C program is fixed during the running process. Although it can be redirected, the address space of the C program is fixed in the "he" linear space,

That is, the space of the Pressure stack called by the function, the base address of the stack is fixed.

2. Because the pressure stack sequence is consistent, and the offset of the variable is certain each time the stack is pressed. This result is returned when the stack is pressed from left to right or from right to left.

Therefore, the above situation occurs.

How can we explain that this process is abnormal? Although he seems to be able to compile, link, and run, the results are also normal.

# Include <stdio. h>

Int * x;

Int * sum (int x, int y)
{
Int;
A = x + y;

Return &;
}
Void getsum ()
{
X = sum (10, 2 );
Printf ("* x = % d; x = % u \ n", * x, x); // second output
}
Int main (void)
{
Int;

X = sum (10, 2 );
Printf ("* x = % d; x = % u \ n", * x, x); // The first output

Getsum ();
Printf ("* x = % d; x = % u \ n", * x, x); // The third output

* X = 100;
Printf ("* x = % d; x = % u \ n", * x, x); // The fourth output


Getchar ();
Return 0;
} We can find that the x values of the first output and the second output are the same, while those of the second, third, and fourth output are only the same. Why?

Let's change it:

# Include <stdio. h

Int * x;

Int * sum (int x, int y)
{
Int;
A = x + y;

Return &;
}


Void getsum ()
{
X = sum (10, 2 );
Printf ("* x = % d; x = % u \ n", * x, x); // second output
}
Int main (void)
{
Int;

X = sum (10, 2 );
Printf ("* x = % d; x = % u \ n", * x, x); // The first output

Getsum ();
Printf ("* x = % d; x = % u \ n", * x, x); // The third output

* X = 100;
Printf ("* x = % d; x = % u \ n", * x, x); // The fourth output


X = sum (10, 2 );
Printf ("* x = % d; x = % u \ n", * x, x); // The Fifth output

Getchar ();
Return 0;
} Try again like this:

  

# Include <stdio. h>

Int * x;

Int * sum (int x, int y)
{
Int;
A = x + y;

Return &;
}

Int * sum1 (int x, int y)
{
Int;
A = x + y;

Return &;
}
Void getsum ()
{
X = sum (10, 2 );
Printf ("* x = % d; x = % u \ n", * x, x); // second output
}
Int main (void)
{
Int;

X = sum (10, 2 );
Printf ("* x = % d; x = % u \ n", * x, x); // The first output

Getsum ();
Printf ("* x = % d; x = % u \ n", * x, x); // The third output

* X = 100;
Printf ("* x = % d; x = % u \ n", * x, x); // The fourth output


X = sum1 (10, 2 );
Printf ("* x = % d; x = % u \ n", * x, x); // The Fifth output

Getchar ();
Return 0;
} Let's make another change:

# Include <stdio. h>

Int * x;

Int * sum (int x, int y)
{
Int;
A = x + y;

Return &;
}

Int * sum1 (int c, int d)
{
Int;
A = c + d;
* X = 60;
Return x;
}
Void getsum ()
{
X = sum (10, 2 );
Printf ("* x = % d; x = % u \ n", * x, x); // second output
}
Int main (void)
{
Int;

X = sum (10, 2 );
Printf ("* x = % d; x = % u \ n", * x, x); // The first output

Getsum ();
Printf ("* x = % d; x = % u \ n", * x, x); // The third output

* X = 100;
Printf ("* x = % d; x = % u \ n", * x, x); // The fourth output


X = sum1 (10, 2 );
Printf ("* x = % d; x = % u \ n", * x, x); // The Fifth output

Getchar ();
Return 0;

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.