问题描述
我正在使用 getenv("TEMP")
,但我收到一条警告,告诉我使用 _dupenv_s
.
I'm using getenv("TEMP")
, but I'm getting a warning telling me to use _dupenv_s
.
我在网上找不到 _dupenv_s 的例子.
I can't find an example of _dupenv_s on the net.
文档阅读:
errno_t _dupenv_s(
char **buffer,
size_t *numberOfElements,
const char *varname
);
但是他们指的是什么缓冲区?我只有varname.避免使用缓冲区不是更好吗?
But what buffer are they referring to? I only have varname. Wouldn't it be better to avoid using a buffer?
推荐答案
_dupenv_s
是 Microsoft 的一项功能,设计为一种更安全的 getenv
形式.
_dupenv_s
is a Microsoft function, designed as a more secure form of getenv
.
_dupenv_s
自己分配缓冲区;您必须向它传递一个指向指针的指针,并将其设置为新分配的缓冲区的地址.
_dupenv_s
allocates the buffer itself; you have to pass it a pointer to a pointer and it sets this to the address of the newly allocated buffer.
例如
char* buf = nullptr;
size_t sz = 0;
if (_dupenv_s(&buf, &sz, "EnvVarName") == 0 && buf != nullptr)
{
printf("EnvVarName = %s\n", buf);
free(buf);
}
请注意,您负责释放返回的缓冲区.
Note that you're responsible for freeing the returned buffer.
这篇关于谁能给我 _dupenv_s 的示例代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!