gpt4 book ai didi

c++ - 如何使用 putenv 系统调用维护内存?

转载 作者:可可西里 更新时间:2023-11-01 16:30:00 25 4
gpt4 key购买 nike

POSIX 系统调用 putenv 声明分配的内存字符串在调用 putenv 后不能被调用者释放。因此,您不能使用自动变量调用 putenv

例子:

#include <cstdlib>
#include <cstring>
#include <unistd.h>

int main()
{
char envVar[] = "MYVAR=Value";
// putenv(envVar); //ERROR!
char *memory = static_cast<char*>(std::malloc(sizeof(envVar)));
std::strcpy(memory, envVar);
putenv(memory); //OK!
}

此时我的问题是……环境变量内存是如何释放的?是否需要在单独的存储设备上维护它们并不断从环境中移除东西?即

#include <cstdlib>
#include <cstring>
#include <string>
#include <map>

static std::map<std::string, char*> environmentBlock;

static struct EnvironmentBlockFreer
{
~EnvironmentBlockFreer()
{
for(std::map<std::string, char*>::iterator it = environmentBlock.begin()
it != environmentBlock.end(); ++it)
{
putenv(it->first.c_str()); //Remove entry from the environment
std::free(static_cast<void *>(it->second)); //Nuke the memory
}
}
} EnvironmentBlockFreer_ENTRY;

int main()
{
char envVar[] = "MYVAR=Value";
char *memory = static_cast<char*>(std::malloc(sizeof(envVar)));
std::strcpy(memory, envVar);
putenv(memory); //OK!
environmentBlock.insert(std::pair<std::string, char*>(
"MYVAR", memory)); //Remember the values for later!
}

编辑:看起来我确实需要自己跟踪这个,至少根据 Valgrind 的说法:

/* This program: */

#include <stdlib.h>
#include <string.h>

int main()
{
char str[] = "MYVAR=Example";
char *mem = malloc(sizeof(str));
strcpy(mem, str);
putenv(mem);
}

/* Produced output:

==4219== Memcheck, a memory error detector
==4219== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==4219== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==4219== Command: ./a.out
==4219==
==4219==
==4219== HEAP SUMMARY:
==4219== in use at exit: 14 bytes in 1 blocks
==4219== total heap usage: 2 allocs, 1 frees, 194 bytes allocated
==4219==
==4219== LEAK SUMMARY:
==4219== definitely lost: 14 bytes in 1 blocks
==4219== indirectly lost: 0 bytes in 0 blocks
==4219== possibly lost: 0 bytes in 0 blocks
==4219== still reachable: 0 bytes in 0 blocks
==4219== suppressed: 0 bytes in 0 blocks
==4219== Rerun with --leak-check=full to see details of leaked memory
==4219==
==4219== For counts of detected and suppressed errors, rerun with: -v
==4219== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 8)
*/

最佳答案

不要使用 putenv()如果您担心内存泄漏。

这就是 POSIX 提供 setenv() 的原因和 unsetenv()以及 - 那些控制和管理内存的人。


引用 putenv() 手册页(上面的 URL):

The putenv() function shall use the string argument to set environment variable values. The string argument should point to a string of the form "name= value". The putenv() function shall make the value of the environment variable name equal to value by altering an existing variable or creating a new one. In either case, the string pointed to by string shall become part of the environment, so altering the string shall change the environment. The space used by string is no longer used once a new string which defines name is passed to putenv().

关于c++ - 如何使用 putenv 系统调用维护内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4777909/

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