返回堆栈变量的地址 [英] returning address of stack variable

查看:71
本文介绍了返回堆栈变量的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


根据我的知识,以下代码不起作用:


int * some()

{

int b = 10;

return& b;

}

int main (int argc,char ** argv)

{

int * test = NULL;


test = some();

printf(" test;%d \ n",* test);

* test = 20;


返回0;

}


因为它返回堆栈上的变量的地址,这不应该是

一旦有效功能返回。但是,这个代码似乎是在x86处理器上的VC ++ 6.0中完全正常工作。任何人都可以

请解释什么是正确或错误的,为什么会有效。


此外,在以下声明中

char * p =Hello world;

哪里是p分配的内存?它是堆栈,堆还是全局

区域,什么时候释放,如果是的话?


提前谢谢,

Manish

解决方案

Manish Tomar写道:

大家好,

根据我的知识,以下代码不应该起作用:

int * some()
{
int b = 10;
返回& b;
}

int main(int argc,char ** argv)
{* / int * test = NULL;

test = some();
printf(" test;%d \ n",* test);
* test = 20;

返回0;
}

因为它返回堆栈上变量的地址,一旦函数返回就不应该有效。但是,这个代码似乎在x86处理器上的VC ++ 6.0中运行得非常好。任何人都可以解释什么是正确或错误的,为什么它有效。

你有未定义的行为。该值仍然有效,因为没有什么

有机会覆盖它。在printf之前添加几个函数调用

,看看会发生什么。

另外,在下面的语句中
char * p =" Hello world" ;;
p分配的内存在哪里?它是在堆栈,堆或全局区域,何时被释放,如果是这样?



它不是,Hello world是一个字符串文字,在FAQ中查找。


-

Ian Collins。


< blockquote> Manish Tomar说:

大家好,

根据我的知识,以下代码不起作用:




但事实并非如此。不幸的是,不工作可以有各种各样的结果。

其中一个结果就是认为代码是正确的人会发生什么,b $ b预期会发生,直到它的代价不是工作超过了价值

继续雇用编写代码的人。


换句话说,代码将继续有效,直到你的老板

向重要客户展示该计划,此时它将以高度可见和令人尴尬的方式失败。


- -

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


在语句printf(" Hello world" ),printf函数接收

字符串文字作为char *,这意味着它接收一些内存地址。

因此应该使用一些内存来存储这个字符串。在哪里?


Hi All,

The following code as per my knowledge should not work:

int* some()
{
int b = 10;
return &b;
}
int main(int argc, char** argv)
{
int* test = NULL;

test = some();
printf("test ; %d\n", *test);
*test = 20;

return 0;
}

as it is returning the address of a variable on stack which should not
be valid once the function returns. However, this code seems to be
working absolutely fine in VC++ 6.0 on x86 processor. Could anyone
please explain what is correct or wrong and why is it working.

Also, in the following statement
char *p = "Hello world";
where is the memory for p allocated? is it on stack, heap or global
area and when is it freed, if so?

Thanks in advance,
Manish

解决方案

Manish Tomar wrote:

Hi All,

The following code as per my knowledge should not work:

int* some()
{
int b = 10;
return &b;
}
int main(int argc, char** argv)
{
int* test = NULL;

test = some();
printf("test ; %d\n", *test);
*test = 20;

return 0;
}

as it is returning the address of a variable on stack which should not
be valid once the function returns. However, this code seems to be
working absolutely fine in VC++ 6.0 on x86 processor. Could anyone
please explain what is correct or wrong and why is it working.
You have undefined behaviour. The value is still valid because nothing
has had a chance to overwrite it. Add a couple more function calls
before the printf and see what happens.
Also, in the following statement
char *p = "Hello world";
where is the memory for p allocated? is it on stack, heap or global
area and when is it freed, if so?


It isn''t, "Hello world" is a string literal, look it up in the FAQ.

--
Ian Collins.


Manish Tomar said:

Hi All,

The following code as per my knowledge should not work:



And it doesn''t. Unfortunately, "not work" can have a variety of outcomes.
One of those outcomes is "what someone who thinks the code is correct would
expect to happen, up until the cost of it not working exceeds the value of
continuing to hire the person writing the code".

In other words, the code will continue to work until your boss is
demonstrating the program to an important customer, at which point it will
fail in a highly visible and embarrassing way.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


In statement printf("Hello world"), the printf function receives the
string literal as char* which means it receives some memory address.
Hence some memory should be used to store this string. Where is it?


这篇关于返回堆栈变量的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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