关于函数中静态变量的问题 [英] question on static variable in a function

查看:88
本文介绍了关于函数中静态变量的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下链接中,
http:// www.c-faq.com/malloc/retaggr.html


以下答案提出了一个问题(comp.lang.c常见问题列表·

问题7.5a):


每当函数返回指针时,请确保正确分配指向的

内存。例如,确保你没有完成

类似


#include< stdio.h>


char * itoa(int n)

{

char retbuf [20]; / *错误* /

sprintf(retbuf,"%d",n);

返回retbuf; / *错误* /

}


当一个函数返回时,它的自动局部变量被丢弃,

所以返回在这种情况下指针是无效的(它指向一个数组

不再存在)。


一个修复方法是将返回缓冲区声明为


static char retbuf [20];


此修复不完美,因为使用静态数据的函数不是
可重入。


我的问题是什么是可重入的意味着什么?为什么不打算使用

函数中的静态数据?

In the following link,
http://www.c-faq.com/malloc/retaggr.html

The following ANSWER is given to a question(comp.lang.c FAQ list ·
Question 7.5a) :

Whenever a function returns a pointer, make sure that the pointed-to
memory is properly allocated. For example, make sure you have not done
something like

#include <stdio.h>

char *itoa(int n)
{
char retbuf[20]; /* WRONG */
sprintf(retbuf, "%d", n);
return retbuf; /* WRONG */
}

When a function returns, its automatic, local variables are discarded,
so the returned pointer in this case is invalid (it points to an array
that no longer exists).

One fix would be to declare the return buffer as

static char retbuf[20];

This fix is imperfect, since a function using static data is not
reentrant.

My question is what "reentrant" means here? Why is static data inside
function discouraged ?

推荐答案

文章< 11 **** ****************@c51g2000cwc.googlegroups.c om>,
su ************** @ yahoo.com ,印度< su ************** @ yahoo.comwrote:

....
In article <11********************@c51g2000cwc.googlegroups.c om>,
su**************@yahoo.com, India <su**************@yahoo.comwrote:
....

>当一个函数返回时,它的自动局部变量被丢弃,
所以在这种情况下返回的指针是无效的(它指向一个不再存在的数组)。

一个修复方法是将返回缓冲区声明为

static char retbuf [20];

这个修复是不完美的,因为使用静态数据的函数不是可重入的。

我的问题是什么折返"意味着什么?为什么内部静态数据功能不受阻碍?
>When a function returns, its automatic, local variables are discarded,
so the returned pointer in this case is invalid (it points to an array
that no longer exists).

One fix would be to declare the return buffer as

static char retbuf[20];

This fix is imperfect, since a function using static data is not
reentrant.

My question is what "reentrant" means here? Why is static data inside
function discouraged ?



使用这种方法至少有3件事可能出错:

1)短语not reentrant的主要导入;是因为它不能在多线程应用程序中使用
。即,在一个

多线程中,可能会有多个活动的函数调用。

2)具有静态数据的函数不能递归调用。

3)以下代码虽然不涉及递归

或线程,但是没有按预期工作:

char * a,* b;

a = afunc(someparams);

b = afunc(someparams);

/ *用&做一些事情b * /


实际上#3以上是最烦人的(恕我直言)。

There are at least 3 things that can go wrong using this approach:
1) The primary import of the phrase "not reentrant" is that it
can''t be used in a multi-threaded application. I.e., in a
multi-thread, there could be more than one active
invocation of the function.
2) Functions with static data cannot be called recursively.
3) The following code, although not involving either recursion
or threading, doesn''t work as expected:

char *a,*b;
a = afunc(someparams);
b = afunc(someparams);
/* Do something with a & b */

It is actually #3 above that''s the most annoying (IMHO).


su ************** @ yahoo.com,India < su ************** @ yahoo.com>

写道:
"su**************@yahoo.com, India" <su**************@yahoo.com>
writes:

static char retbuf [20];


这个修复是不完美的,因为使用静态数据的函数不是
可重入。


我的问题是什么是可重入的意味着什么?为什么

函数里面的静态数据不鼓励?
static char retbuf[20];

This fix is imperfect, since a function using static data is not
reentrant.

My question is what "reentrant" means here? Why is static data inside
function discouraged ?



如果您再次拨打该功能,那么您在第二次通话时获得的答案将取代您在第一次通话时获得的答案。

除非你复制第一次返回的值,或者

你不需要在第二次调用后引用它,这可以是

非常令人惊讶。


严格来说,这不是可重入的内容。通常是

的意思,但这是你在

遇到的问题。

-

C有它的问题,但是从头开始设计的语言也会有一些,

我们知道C'的问题。

- Bjarne Stroustrup

If you call the function twice, then the answer you get on the
second call will replace the answer you get on the first call.
Unless you make a copy of the value returned the first time, or
you don''t need to refer to it after the second call, this can be
very surprising.

This is not, strictly speaking, what "reentrant" is generally
taken to mean, but it is the problem that you will encounter in
this situation.
--
"C has its problems, but a language designed from scratch would have some too,
and we know C''s problems."
--Bjarne Stroustrup


我理解你的观点1)和3)。但我在递归方法中使用了静态整数

计数器变量。


我无法理解为什么递归函数的原因

不能包含静态数据。

I understand your points 1) and 3). But I have used static integer
counter variable in a recursive method which works.

I cannot understand the reason as to why recursive function
cannot contain static data.


这篇关于关于函数中静态变量的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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