为指针指定局部变量的地址 [英] assigning a pointer the address of local variable

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

问题描述

假设我有这样的代码,


#include< stdio.h>


int * p;

void foo(int);


int main(无效){

foo(3);

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

返回0;

}


void foo(int r){

int s = r + 1;

p =& s;

}


在我使用的大多数编译器中(GCC,MSVC ++,lcc ..)这个程序运行

allright打印一个地址和正确的值4.但它是否正确

分配一个全局指针,函数结束后,本地变量的地址不存在?

Suppose I have a code like this,

#include <stdio.h>

int *p;
void foo(int);

int main(void){
foo(3);
printf("%p %d\n",p,*p);
return 0;
}

void foo(int r){
int s=r+1;
p=&s;
}

In most of the compilers I use (GCC, MSVC++, lcc..) this program runs
allright printing an address and the correct value 4. But is it correct
to assign a global pointer the address of a local variable which does
not exist after the function has ended?

推荐答案

" Sourav" < so ********* @ gmail.comwrites:
"Sourav" <so*********@gmail.comwrites:

假设我有这样的代码,


#include< stdio.h>


int * p;

void foo(int);


int main(void){

foo(3);

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

返回0;

}


void foo(int r){

int s = r +1;

p =& s;

}


在我使用的大多数编译器中(GCC,MSVC ++,lcc) ..)这个程序运行

allright打印一个地址和正确的值4.但它是否正确

为一个全局指针分配一个局部变量的地址<函数结束后,
不存在?
Suppose I have a code like this,

#include <stdio.h>

int *p;
void foo(int);

int main(void){
foo(3);
printf("%p %d\n",p,*p);
return 0;
}

void foo(int r){
int s=r+1;
p=&s;
}

In most of the compilers I use (GCC, MSVC++, lcc..) this program runs
allright printing an address and the correct value 4. But is it correct
to assign a global pointer the address of a local variable which does
not exist after the function has ended?



不,不是。当s到达其生命周期的末尾时(在
foo()结束时,p的值变得不确定。取消引用p,或者甚至

查看其值,调用未定义的行为。(后者不是
可能会导致大多数系统出现任何明显问题,但你应该

仍然可以避免它。)


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

No, it isn''t. When s reaches the end of its lifetime (at the end of
foo(), the value of p becomes indeterminate. Dereferencing p, or even
looking at its value, invokes undefined behavior. (The latter isn''t
likely to cause any visible problems on most systems, but you should
still avoid it.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.




Sourav写道:

Sourav wrote:

假设我有这样的代码,


#include< stdio.h>


int * p;

void foo(int);


int main(void){

foo(3) ;

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

返回0;

}


void foo(int r){

int s = r + 1;

p =& s;

}


在我使用的大多数编译器中(GCC,MSVC ++,lcc。 。)这个程序运行

allright打印一个地址和正确的值4.但它是否正确

为全局指针分配一个局部变量的地址不存在?
Suppose I have a code like this,

#include <stdio.h>

int *p;
void foo(int);

int main(void){
foo(3);
printf("%p %d\n",p,*p);
return 0;
}

void foo(int r){
int s=r+1;
p=&s;
}

In most of the compilers I use (GCC, MSVC++, lcc..) this program runs
allright printing an address and the correct value 4. But is it correct
to assign a global pointer the address of a local variable which does
not exist after the function has ended?



编号考虑:


#include< stdio.h>


int * p;

void foo(int);

int baz(int);


int main(void ){

foo(3);

baz(5);

printf("%p%d \ n", void *)p,* p);

返回0;


}


void foo(int r ){

int s = r + 1;

p =& s;


}

int baz(int r){

int t = 19;

int k = 4;


返回t + k + t;

}


打印:

[tmp]

No. consider:

#include <stdio.h>

int *p;
void foo(int);
int baz(int);

int main(void){
foo(3);
baz(5);
printf("%p %d\n",(void*)p,*p);
return 0;

}

void foo(int r){
int s=r+1;
p=&s;

}

int baz(int r) {
int t=19;
int k=4;

return t + k + t;
}

This prints:
[tmp]


./ a.out

0xfee768a4 19

./a.out
0xfee768a4 19


这篇关于为指针指定局部变量的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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