函数的返回类型是静态数组的话,安全吗?
wget源中看到如下代码:
/* N, a byte quantity, is converted to a human-readable abberviated
form a la sizes printed by `ls -lh '. The result is written to a
static buffer, a pointer to which is returned.
Unlike `with_thousand_seps ', this approximates to the nearest unit.
Quoting GNU libit: "Most people visually process strings of 3-4
digits effectively, but longer strings of digits are more prone to
misinterpretation. Hence, converting to an abbreviated form
usually improves readability. "
This intentionally uses kilobyte (KB), megabyte (MB), etc. in their
original computer science meaning of "powers of 1024 ". Powers of
1000 would be useless since Wget already displays sizes with
thousand separators. We don 't use the "*bibyte " names invented in
1998, and seldom used in practice. Wikipedia 's entry on kilobyte
discusses this in some detail. */
char *
human_readable (int n)
{
/* These suffixes are compatible with those of GNU `ls -lh '. */
static char powers[] =
{
'K ',/* kilobyte, 2^10 bytes */
'M ',/* megabyte, 2^20 bytes */
'G ',/* gigabyte, 2^30 bytes */
'T ',/* terabyte, 2^40 bytes */
'P ',/* petabyte, 2^50 bytes */
'E ',/* exabyte, 2^60 bytes */
};
static char buf[8];
int i;
/* If the quantity is smaller than 1K, just print it. */
if (n < 1024)
{
snprintf (buf, sizeof (buf), "%d ", (int) n);
return buf; /*可以这样返回静态数组?*/
}
/* Loop over powers, dividing N with 1024 in each iteration. This
works unchanged for all sizes of wgint, while still avoiding
non-portable `long double ' arithmetic. */
for (i = 0; i < countof (powers); i++)
{
/* At each iteration N is greater than the *subsequent* power.
That way N/1024.0 produces a decimal number in the units of
*this* power. */
if ((n > > 10) < 1024 || i == countof (powers) - 1)
{
double val = n / 1024.0;
/* Print values smaller than 10 with one decimal digits, and
others without any decimals. */
snprintf (buf, sizeof (buf), "%.*f%c ",
val < 10 ? 1 : 0, val, powers[i]);
return buf;
}
n > > = 10;
}
return NULL;/* unreached */
}
不解的是,该函数返回的是内部声明的静态变量
static char buf[8];
下边有几处 return buf;
我知道把static去掉不安全,但这返回静态变量安全吗?
[解决办法]
单线程是安全的,多线程就不安全了
[解决办法]
函数内static数据的生存期是静态的,也就是整个程序的生存期。所以如果只是读buf数据,是安全的,也就是该数据不会被销毁。
不过,如果有 char *p = human_readable(10); p就是指向human_readable中的buf的,如果通过p向buf中写入长度超过8的字符数据,就不安全了 :P
[解决办法]
不管它是在函数内部定义的还是在那定义的.它的生命周期都是整个程序的,当然不能去掉static关键字,
至于安全与否:
看环境了.
多线程非常危险.控制好写入和读取还是可以的
[解决办法]
单线程是安全
多线程非常危险
[解决办法]
单线程是安全
多线程非常危险(注意控制写入和读取的操作)
[解决办法]
多线程不安全