返回局部变量的地址 [英] returning address of a local variable

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

问题描述

大家好,

我有关于指针的这个奇怪的问题。我想知道如何

安全返回本地变量的地址!这不是一个

未经推荐的程序吗?难道没有可能从内存中取消分配

变量并且在其他地方取消引用

返回的指针会导致程序崩溃吗?

如果进行这样的操作并不安全,那么

strtok等功能如何工作???


-

Spitfire

Hi All,
I''ve this weird question about pointers. I would like to know how to
return the address of a local variable, safely!! Isn''t that a
unrecommended procedure? Doesn''t it have possibilities that the
variable could be deallocated from memory and the dereferencing of the
returned pointer somewhere else could crash the program?
If it''s not safer to do such an operation, how do functions like
strtok for example work???

--
Spitfire

推荐答案

Spitfireaécrit:
Spitfire a écrit :

大家好,

我有一个关于指针的奇怪问题。我想知道如何

安全返回本地变量的地址!这不是一个

未经推荐的程序吗?难道没有可能从内存中取消分配

变量并且在其他地方取消引用

返回的指针会导致程序崩溃吗?

如果进行这样的操作并不安全,那么

strtok等功能如何工作???


-

Spitfire
Hi All,
I''ve this weird question about pointers. I would like to know how to
return the address of a local variable, safely!! Isn''t that a
unrecommended procedure? Doesn''t it have possibilities that the
variable could be deallocated from memory and the dereferencing of the
returned pointer somewhere else could crash the program?
If it''s not safer to do such an operation, how do functions like
strtok for example work???

--
Spitfire



你不能返回一个局部变量的地址,因为当

函数在那里变量离开(退出)局部变量是

不再有效并指向无法访问的存储空间。


strtok使用静态变量,而不是自动变量局部变量

存储类。

You can''t return the address of a local variable since when the
function where that variable leaves (exits) the local variable is
no longer valid and points to unreachable storage.

strtok use a static variable, not a local variable with automatic
storage class.


Spitfire写道:
Spitfire wrote:

我有这个关于指针的奇怪问题。我想知道如何

返回局部变量的地址,
I''ve this weird question about pointers. I would like to know how to
return the address of a local variable,



void * get_local_address()

{

int i;

return& i;

}

void* get_local_address ()
{
int i;
return &i;
}


安全!!
safely!!



只要你不使用它就完全(几乎)安全。

it''s perfectly (well almost) safe as long as you don''t use it.


这不是一个未经推荐的程序吗?
Isn''t that a unrecommended procedure?



我只是不明白你为什么要这样做

I just don''t understand why you want to do this


Doesn是否有可能从内存中取消分配

变量,并且在其他地方解除引用

返回的指针可能会导致程序崩溃?
Doesn''t it have possibilities that the
variable could be deallocated from memory and the dereferencing of the
returned pointer somewhere else could crash the program?



所以不要取消引用它?

so don''t dereference it?


如果不是更安全的做这样的操作,如何工作如

strtok例如工作???
If it''s not safer to do such an operation, how do functions like
strtok for example work???



现在我很困惑。为什么stritok会返回本地

变量的地址? strtok()修改你传递给它的字符串。


-

Nick Keighley

now I''m confused. Why would stritok return the address of a local
variable? strtok() modifies the string you pass to it.

--
Nick Keighley

Spitfire说:
Spitfire said:

大家好,

我有一个关于指针这个奇怪的问题。我想知道如何

安全返回本地变量的地址!
Hi All,
I''ve this weird question about pointers. I would like to know how to
return the address of a local variable, safely!!



int * foo(void)

{

static int i;

return& i;

}

int *foo(void)
{
static int i;
return &i;
}


这不是一个未经推荐的程序吗?
Isn''t that a unrecommended procedure?



可以。这取决于你在做什么以及你是如何做的。

It can be. That depends on what you''re doing and how you do it.


难道没有可能

变量可以从内存中解除分配并取消引用

返回的指针在其他地方可能导致程序崩溃?
Doesn''t it have possibilities that the
variable could be deallocated from memory and the dereferencing of the
returned pointer somewhere else could crash the program?



如果你小心的话(见上文)。

Not if you''re careful (see above).


如果不是更安全的做这样的操作,如何工作如

strtok例如工作???
If it''s not safer to do such an operation, how do functions like
strtok for example work???



小心。首先,他们不会返回当地

变量的地址。


-

Richard Heathfield

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

电子邮件:rjh在上述域名中, - www。

Carefully. For one thing, they don''t return the addresses of local
variables.

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


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

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