gpt4 book ai didi

c++ - glibc sprintf 可以在可重入函数中使用吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:44:40 24 4
gpt4 key购买 nike

如果它写入本地缓冲区,我可以在可重入函数中使用 sprintf 吗?像这样:

void reentrant_function () { 
int i = 4;
char buffer[20];
snprintf(buffer, 20, "%d", i);
}

最佳答案

我来到这个页面是因为我也在问同样的问题。这是我从书上读到的:

UNIX 环境高级编程:第二版

Section 10.6

Most functions that are not in Figure 10.4 (a list of reentrant functions) are missing because (a) they are known to use static data structures, (b) they call malloc or free, or (c) they are part of the standard I/O library.

Linux 编程接口(interface)

Chapter 21.1.2

Functions can also be nonreentrant if they use static data structures for theirinternal bookkeeping. The most obvious examples of such functions are the membersof the stdio library (printf(), scanf(), and so on), which update internal datastructures for buffered I/O.

我想这取决于实现。这也取决于你如何实际使用 snprintf (缓冲区是必不可少的)。对该问题最安全的回答应该是“否”。

关于c++ - glibc sprintf 可以在可重入函数中使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4602574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com