c++ - 无法从 C/C++ 程序访问环境变量

标签 c++ c linux environment-variables

我正在尝试从 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");

最佳答案

getenv 返回一个指向库分配内存的指针,该内存不属于您的程序。你的

a = new char [1000] 

行显示您没有意识到这一点,并且似乎假设您需要提供内存。这不是真的,尤其是您可能永远不会释放 getenv 返回的内存。

(即使那是正确的,简单的指针赋值

a = getenv...

仍然是错误的,因为您只是交换指针而不是复制内存。该行是内存泄漏,因为您丢失了指向分配的 1000 个字符的指针)

如果您希望您的程序拥有该内存以便稍后释放它,您需要将它复制到我们的私有(private)内存空间。

a = new char [1000];
e = getenv (<whatever>);
strcpy (a, e);

不幸的是,我看不到您稍后在其他示例中对指针做了什么,尤其是当您尝试释放删除 它时。两者都会导致错误。

关于c++ - 无法从 C/C++ 程序访问环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39032313/

相关文章:

c++ - 获取指针指向的对象

c++ - SFINAE 检查继承的成员函数

c - 一些字符数组不以 '\0' 结尾

linux - 错误 : useradd: useradd command did not succeed in Yocto build

linux - 当存在多个用户进程时从 processid 获取用户

c++ - sort() - 没有匹配函数来调用 'swap'

c++ - C++ 动态结构中的动态结构

c 读取字符串导致崩溃

c++ - 我从源代码安装了 GCC 5.2,但我不知道如何在 Ubuntu 15.04 上卸载它

c++ - 如何在 Linux 上使用 Qt 读取 HID 设备 (/dev/hidrawX)?