我正在尝试从C++程序访问一个环境变量。所以我做了一个测试程序,它运行得很好:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
printf("MANIFOLD : %s\n", getenv("MANIFOLD_DIRECTORY"));
return(0);
}输出:MANIFOLD : /home/n1603031f/Desktop/manifold-0.12.1/kitfox_configuration/input.config
注: getenv的签名是:
char *getenv(const char *name);但是,当我使用它作为一个更大的程序的一部分,与许多文件链接:
energy_introspector->configure (getenv("MANIFOLD_DIRECTORY"));上面的不起作用.
char *a = new char [1000];
a = getenv("MANIFOLD_DIRECTORY");
energy_introspector->configure (a);上面的也不起作用.
注释:配置函数的签名:
void configure(const char *ConfigFile);错误消息:
Number of LPs = 1
[Ubuntu10:18455] *** Process received signal ***
[Ubuntu10:18455] Signal: Segmentation fault (11)
[Ubuntu10:18455] Signal code: Address not mapped (1)
[Ubuntu10:18455] Failing at address: (nil)
[Ubuntu10:18455] [ 0] /lib/x86_64-linux-gnu/libpthread.so.0(+0x10330) [0x7f9a38149330]
[Ubuntu10:18455] [ 1] /lib/x86_64-linux-gnu/libc.so.6(strlen+0x2a) [0x7f9a37dfc9da]
[Ubuntu10:18455] [ 2] /home/n1603031f/Desktop/manifold-0.12.1/simulator/smp/QsimLib/smp_llp() [0x5bf8c4]
[Ubuntu10:18455] [ 3] /home/n1603031f/Desktop/manifold-0.12.1/simulator/smp/QsimLib/smp_llp() [0x5a4ac6]
[Ubuntu10:18455] [ 4] /home/n1603031f/Desktop/manifold-0.12.1/simulator/smp/QsimLib/smp_llp() [0x5a4df8]
[Ubuntu10:18455] [ 5] /home/n1603031f/Desktop/manifold-0.12.1/simulator/smp/QsimLib/smp_llp() [0x4283b6]
[Ubuntu10:18455] [ 6] /home/n1603031f/Desktop/manifold-0.12.1/simulator/smp/QsimLib/smp_llp() [0x41e197]
[Ubuntu10:18455] [ 7] /home/n1603031f/Desktop/manifold-0.12.1/simulator/smp/QsimLib/smp_llp() [0x41de7a]
[Ubuntu10:18455] [ 8] /home/n1603031f/Desktop/manifold-0.12.1/simulator/smp/QsimLib/smp_llp() [0x41d906]
[Ubuntu10:18455] [ 9] /home/n1603031f/Desktop/manifold-0.12.1/simulator/smp/QsimLib/smp_llp() [0x41710b]
[Ubuntu10:18455] [10] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5) [0x7f9a37d95f45]
[Ubuntu10:18455] [11] /home/n1603031f/Desktop/manifold-0.12.1/simulator/smp/QsimLib/smp_llp() [0x41697f]
[Ubuntu10:18455] *** End of error message ***
--------------------------------------------------------------------------
mpirun noticed that process rank 0 with PID 18455 on node Ubuntu10 exited on signal 11 (Segmentation fault).
--------------------------------------------------------------------------,但这是可行的:
energy_introspector->configure ("/home/n1603031f/Desktop/manifold-0.12.1/kitfox_configuration/input.config");发布于 2016-08-19 07:22:46
getenv返回一个指向不属于程序的库分配内存的指针。你的
a = new char [1000] 行显示您没有意识到这一点,并且似乎假设您需要提供内存。这不是真的,特别是您可能永远不会释放getenv返回的内存。
(即使这是正确的,简单的指针分配也是正确的)
a = getenv...仍然是错误的,因为您只是交换一个指针,而不是复制内存。这一行是内存泄漏,因为您松开了指向分配的1000个字符的指针)
如果您希望您的程序拥有该内存,以便以后可以对其进行free,则需要将其复制到我们的私有内存空间中。
a = new char [1000];
e = getenv (<whatever>);
strcpy (a, e);不幸的是,我无法在后面的示例中看到如何处理指针,特别是当您尝试使用free或delete时。两者都会导致错误。
发布于 2016-08-19 08:48:31
首先,代码中的显式错误是char数组分配,然后分配getenv的结果。这会导致内存泄漏。在您的案例中使用:
std::string a = getenv("MANIFOLD_DIRECTORY");这样可以将结果保存在变量a中,并使代码对取消设置环境变量免疫。
如果getenv返回NULL,那么指定名称的变量不在传递给应用程序的环境中。尝试用下面的代码列出所有可用的环境变量。
extern char** environ;
for (int i = 0; environ[i] != NULL; ++i) {
std::cout << environ[i] << std::endl;
}如果您的变量没有列出,那么很可能就是如何调用应用程序的问题。另一种选择是您的环境已被取消设置。
https://stackoverflow.com/questions/39032313
复制相似问题