参数传递给函数时,堆栈地址是不同的? [英] stack address is different when parameter is passed to function?

查看:54
本文介绍了参数传递给函数时,堆栈地址是不同的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,


因为程序的堆栈是在

程序中的所有函数之间共享的,

我只是做了一些R& D来看看是否使用相同的堆栈空间

对于不同函数内部的变量不是。

下面是我的代码

//程序开始

void fun1()

{int i;

cout<<& i; < br $>
}


void fun2()

{

int i;

cout<<& i;

}

int main()

{

fun1( );

fun2();

}

//程序结束


两者都给了相同的输出(局部变量i的地址),因为使用了相同的

堆栈空间。

//输出是

0x0012FF24 //来自fun1

0x0012FF24 //来自fun2的输出


但是当我在

之一中进行以下更改时,我得到了不同的结果函数。

void fun1(int i)

{

cout<<& i;

}


和里面主要我称之为fun1(4)

但现在输出不同。

//输出是

0x0012FF2C //从fun1输出

0x0012FF24 //来自fun2的输出


到目前为止,我一直在想,因为函数参数也是

传递到堆栈,我应该得到相同的输出(同一地址

本地版本的i)。

我使用的是MSVC6.0


这可能是造成这种不同输出的原因吗?


谢谢和问候,

Yogesh Joshi

解决方案

< yp ********* @ indiatimes.comschrieb im Newsbeitrag

news:11 ******* ***************@s13g2000cwa.googlegr oups.com ...


Hello All,


因为程序的堆栈在

程序中的所有函数之间共享,
我只是做了一些R& D来查看是否使用了相同的堆栈空间

不同功能中的变量不是。

以下是我的代码

//程序开始

void fun1()

{int i;

cout<< & i;

}


void fun2()

{

int i;

cout<<& i;

}

int main()

{

fun1();

fun2();

}

//程序结束


两者都给出了相同的输出(局部变量i的地址),因为使用了相同的

堆栈空间。

//输出是

0x0012FF24 //来自fun1的输出

0x0012FF24 //来自fun2的输出


但是当我在其中一个中进行以下操作时,我得到了不同的结果

函数。

void fun1(int i)

{

cout<<& i;

}


和内部主要我称之为fun1(4)

但现在输出是不同的。

//输出是

0x0012FF2C //从fun1输出

0x0012FF24 //从fun2输出


到目前为止我一直在想,既然函数参数也是传递到堆栈的
,我应该得到相同的输出(相同的地址为

两个本地版本的i)。



1.仅仅因为参数也在堆栈上传递,你应该期望

不同的局部变量地址。


2.即使在第一种情况下,编译器也可以自由地将这些本地

变量放在内存中的不同位置。


3.编译器不需要使用堆栈作为参数,本地

变量或其他任何东西。


4. IIRC,唯一需要的堆栈标准是类型std :: stack。


(标准提到堆栈展开在某些地方,但它是实现定义的b
,如何组织局部变量和参数。)


问候

亨氏


你好Heinz,

感谢您的快速回复。

但我仍然不完全相信。


1.仅仅因为参数也在堆栈上传递,你就是守护ld期望

不同的局部变量地址。



但是堆栈只有一个入口点,即在

it的顶部。除了顶部这些本地以外的地方变量将被存储?


2.即使在第一种情况下,编译器也可以自由地将这些本地

变量放在不同的地方在记忆中。



堆栈以外的内存在哪里?由于局部变量(auto)是

总是被压在堆栈上,并且同一程序中的所有

函数共享单个堆栈。


3.编译器不需要使用堆栈作为参数,本地

变量或其他任何东西。



那么调用约定呢。他们总是指定

将参数推送到堆栈上。在C的情况下,默认调用

约定为cdecl,它将参数从右向左推,并且

因为在这种情况下只有一个参数传递给

堆栈,堆栈地址应该相同。

谢谢和问候,

Yogesh Joshi


< yp ********* @ indiatimes.comschrieb im Newsbeitrag

news:11 ************* *********@p79g2000cwp.googlegr oups.com ...


Hi Heinz,

感谢您的快速回复。

但我还是不完全相信。


> 1。仅仅因为参数也在堆栈上传递,你应该期望
局部变量的不同地址。



但是堆栈只有一个入口点,即在

it的顶部。除了顶部这些本地以外的地方变量会被存储?



堆栈的唯一固定地址是它的底部。在执行一个

程序期间,数据被压入堆栈或弹出它并且堆栈顶部的地址

变化。


> 2。即使在第一种情况下,编译器也可以将这些局部变量放在内存中的不同位置。



堆栈以外的内存在哪里?由于局部变量(auto)是

总是被压在堆栈上,并且来自同一程序的所有

函数共享单个堆栈。



有几种可能的解决方案。我知道有一个为函数的所有局部变量分配

内存块,并在

链表中管理这些块。


> 3。编译器不需要使用堆栈作为参数,本地变量或其他任何东西。



那么调用约定呢。他们总是指定

将参数推送到堆栈上。在C的情况下,默认调用

约定为cdecl,它将参数从右向左推,并且

因为在这种情况下只有一个参数传递给

堆栈,堆栈地址应该相同。



这个小组专门用于C ++,而不是C.所以无论C做什么(或者有人做什么,

C)都没关系。另外,cdecl不是C ++标准的一部分

(也不是C,IIRC的一部分)。 C调用约定只指定两个项目。

调用者负责从任何内存中删除任何参数

已用于将它们传递给被调用函数和所有参数(除了

声明为寄存器参数的那些)必须存储在一个连续的块中

的内存中,第一个参数位于比以下更小的地址

那些。这些约定是以可预测的方式实现可变参数函数所必需的,但这不是唯一的方法。使用堆栈是一个方便的方法,但这不是唯一的方法。


Heinz


Hello All,

since the programs'' stack is shared among all the function inside the
program,
I was just doing some R&D to see whether the same stack space is used
for the variables inside the different functions are not.
below is my code
//program start
void fun1()
{int i;
cout<<&i;
}

void fun2()
{
int i;
cout<<&i;
}
int main()
{
fun1();
fun2();
}
//program end

Both gave the same output(address of local variable i) since the same
stack space is used.
//output is
0x0012FF24 //output from fun1
0x0012FF24 //output from fun2

But i get the different results when I make following chang in one of
the function.
void fun1(int i)
{
cout<<&i;
}

and inside main i call it as fun1(4)
but now the output is different.
//output is
0x0012FF2C //output from fun1
0x0012FF24 //output from fun2

So far I was thinking that since the function parameters are also
passed onto the stack, I should get the same output(same address for
both the local version of i).
I am using MSVC6.0

what could be the reason for this different output?

Thanks and Regards,
Yogesh Joshi

解决方案

<yp*********@indiatimes.comschrieb im Newsbeitrag
news:11**********************@s13g2000cwa.googlegr oups.com...

Hello All,

since the programs'' stack is shared among all the function inside the
program,
I was just doing some R&D to see whether the same stack space is used
for the variables inside the different functions are not.
below is my code
//program start
void fun1()
{int i;
cout<<&i;
}

void fun2()
{
int i;
cout<<&i;
}
int main()
{
fun1();
fun2();
}
//program end

Both gave the same output(address of local variable i) since the same
stack space is used.
//output is
0x0012FF24 //output from fun1
0x0012FF24 //output from fun2

But i get the different results when I make following chang in one of
the function.
void fun1(int i)
{
cout<<&i;
}

and inside main i call it as fun1(4)
but now the output is different.
//output is
0x0012FF2C //output from fun1
0x0012FF24 //output from fun2

So far I was thinking that since the function parameters are also
passed onto the stack, I should get the same output(same address for
both the local version of i).

1. Just because parameters are also passed on the stack, you should expect
different addresses of local variables.

2. Even in your first case, the compiler would be free to put those local
variables at different places in memory.

3. The compiler is not required to use a stack for parameters, local
variables or anything else.

4. IIRC, the only stack required by the standard is the type std::stack.

(The standard mentions "stack unwinding" at some places, but it is
implementation defined, how local variables and parameters are organized.)

Regards
Heinz


Hi Heinz,
Thanks for the quick reply.
But still i am not fully convinced.

1. Just because parameters are also passed on the stack, you should expect
different addresses of local variables.

But there is only one entry point for the stack and i.e at the top of
it.So where other than the top these local variables will get stored?

2. Even in your first case, the compiler would be free to put those local
variables at different places in memory.

Where in the memory other than stack? As local variables (auto)are
always pushed on the stack and there is single stack shared by all the
functions from a same program.

3. The compiler is not required to use a stack for parameters, local
variables or anything else.

Then what about the calling conventions.?They always specify the
pushing of parameters onto the stack. In case of C the default calling
convention is cdecl which pushes the parameters from right to left and
since in this case there is only one parameters to be passed onto the
stack,the stack address should be the same.
Thanks and Regards,
Yogesh Joshi


<yp*********@indiatimes.comschrieb im Newsbeitrag
news:11**********************@p79g2000cwp.googlegr oups.com...

Hi Heinz,
Thanks for the quick reply.
But still i am not fully convinced.

>1. Just because parameters are also passed on the stack, you should
expect
different addresses of local variables.

But there is only one entry point for the stack and i.e at the top of
it.So where other than the top these local variables will get stored?

The only fixed address of a stack is its bottom. During execution of a
program data are pushed onto the stack or popped off it and the the address
of top of the stack varies.

>2. Even in your first case, the compiler would be free to put those local
variables at different places in memory.

Where in the memory other than stack? As local variables (auto)are
always pushed on the stack and there is single stack shared by all the
functions from a same program.

There are several possible solutions. I know of one that allocates blocks of
memory for all local variables of a function and manages these blocks in a
linked list.

>3. The compiler is not required to use a stack for parameters, local
variables or anything else.

Then what about the calling conventions.?They always specify the
pushing of parameters onto the stack. In case of C the default calling
convention is cdecl which pushes the parameters from right to left and
since in this case there is only one parameters to be passed onto the
stack,the stack address should be the same.

This group is dedicated to C++, not C. So whatever C does (or people thing,
C does) is of little concern. Also, cdecl is not part of the C++ standard
(and neither part of C, IIRC). C calling conventions only specify two items.
The caller is responsible for removing any parameters from whatever memory
has been used to pass them to the called function and all parameters (except
those declared as register parameters) must be stored in a contignous block
of memory with the first parameter at a smaller address than the following
ones. These conventions are required to implement variadic functions in a
predictable manner, but it isn''t the only way to do so. Using a stack is a
convenient method to do so, but it isn''t the only one.

Heinz


这篇关于参数传递给函数时,堆栈地址是不同的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆