全局指针指向本地定义的类型 [英] Global pointer pointing to locally defined type

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

问题描述

嗨!


全局定义指针会发生什么,例如


void * value;


指向函数中的特定类型:


int dummy_func(int a)

{

value =& a;

返回0;

}


函数返回main后?如果这不起作用,我怎么能让它工作?
呢?我是否必须为此int分配内存并将原始的

int复制到该值然后将值指向这个新分配的int?


感谢您的时间,


Andrej

Hi!

What happens to a globally defined pointer, e.g.

void *value;

that points to a particular type in a function:

int dummy_func (int a)
{
value = &a;
return 0;
}

after the function returns to main? If this doesn''t work, how could I make
it work? Do I have to allocate memory for this int and copy the original
int to that and then point value to this newly allocated int?

Thanks for your time,

Andrej

推荐答案

Andrej Prsa< an ********* @ guest.arnes.si>潦草地写道:
Andrej Prsa <an*********@guest.arnes.si> scribbled the following:
嗨!
全局定义的指针会发生什么,例如:
void * value;
指向函数中的特定类型:
int dummy_func(int a)
{
value =& a;
返回0;
函数返回main后



它指向一个不再在范围内的变量,因此不再可以访问
。任何间接通过该指针的尝试都将导致

未定义的行为。简而言之,指针指向垃圾并且

在重新分配之前不能使用。

如果这不起作用,我怎么能使它工作?我是否必须为此int分配内存并将原始的
int复制到该值然后将值指向这个新分配的int?
Hi! What happens to a globally defined pointer, e.g. void *value; that points to a particular type in a function: int dummy_func (int a)
{
value = &a;
return 0;
} after the function returns to main?
It points to a variable that is no longer in scope, thus no longer
accessible. Any attempt to indirect through that pointer will cause
undefined behaviour. In short, the pointer points to garbage and
cannot be used until reassigned.
If this doesn''t work, how could I make
it work? Do I have to allocate memory for this int and copy the original
int to that and then point value to this newly allocated int?




这可能是一个方式,是的。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)----- --------芬兰-------- \

\-- http://www.helsinki.fi/~palaste ---------------------规则! -------- /

"''可以很容易地证明'''的意思''我看过一次这样的证明(我没有这样做)
明白了)我不能再记得了''。"

- 数学老师



That could be one way, yes.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"''It can be easily shown that'' means ''I saw a proof of this once (which I didn''t
understand) which I can no longer remember''."
- A maths teacher


Andrej Prsa< an * ********@guest.arnes.si>潦草地写道:
Andrej Prsa <an*********@guest.arnes.si> scribbled the following:
嗨!
全局定义的指针会发生什么,例如:
void * value;
指向函数中的特定类型:
int dummy_func(int a)
{
value =& a;
返回0;
函数返回main后



它指向一个不再在范围内的变量,因此不再可以访问
。任何间接通过该指针的尝试都将导致

未定义的行为。简而言之,指针指向垃圾并且

在重新分配之前不能使用。

如果这不起作用,我怎么能使它工作?我是否必须为此int分配内存并将原始的
int复制到该值然后将值指向这个新分配的int?
Hi! What happens to a globally defined pointer, e.g. void *value; that points to a particular type in a function: int dummy_func (int a)
{
value = &a;
return 0;
} after the function returns to main?
It points to a variable that is no longer in scope, thus no longer
accessible. Any attempt to indirect through that pointer will cause
undefined behaviour. In short, the pointer points to garbage and
cannot be used until reassigned.
If this doesn''t work, how could I make
it work? Do I have to allocate memory for this int and copy the original
int to that and then point value to this newly allocated int?




这可能是一个方式,是的。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)----- --------芬兰-------- \

\-- http://www.helsinki.fi/~palaste ---------------------规则! -------- /

"''可以很容易地证明'''的意思''我看过一次这样的证明(我没有这样做)
明白)我不能再记得了''。

- 数学老师



That could be one way, yes.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"''It can be easily shown that'' means ''I saw a proof of this once (which I didn''t
understand) which I can no longer remember''."
- A maths teacher


嗨!
如果这不起作用,我怎么能让它工作?我是否必须为这个int分配内存并将原始的
int复制到那个然后将值指向这个新分配的int?
If this doesn''t work, how could I make
it work? Do I have to allocate memory for this int and copy the original
int to that and then point value to this newly allocated int?



这可能是一种方式,是的。



That could be one way, yes.




有没有更好的建议?


Andrej



Is there any better way you''d suggest?

Andrej


这篇关于全局指针指向本地定义的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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