【问题标题】:Not able to access environment variable from C/C++ Program无法从 C/C++ 程序访问环境变量
【发布时间】:2016-12-26 05:13:24
【问题描述】:

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

【问题讨论】:

  • configure 内是否还有其他对getenv 的调用?每次调用getenv,可能会导致上次调用的结果无效。
  • @TheDark 没有。没有来自configuregetenv() 的呼叫
  • C++ 中的new char[1000] 几乎没有任何理由。 std::stringstd::vector&lt;char&gt;std::array&lt;char,1000&gt; 都是更好的选择。在这种情况下:std::string a = getenv( );
  • @MSalters 但函数 configure 需要 char* 而不是 string
  • @PRP:这就是 std::string::c_str() 存在的原因。

标签: c++ c linux environment-variables


【解决方案1】:

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

a = new char [1000] 

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

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

a = getenv...

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

如果您希望您的程序拥有该内存以便您以后可以free 它,您需要将其复制到我们的私有内存空间中。

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

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

【讨论】:

  • 您的解决方案是什么?有吗?
  • 顺便说一句....a = new char [1000]; e = getenv (&lt;whatever&gt;); strcpy (a, e); 不起作用
  • 复制到我自己的内存空间....我试过这样做....还是同样的错误
  • 在这种情况下,问题不在于您向我们展示的代码,而您还有另一个问题。请使用-g 标志进行编译,并使用新的堆栈转储更新您的问题。
  • 它已经用 -g 标志编译了。请参阅我正在编译/链接一组库...并使用一些基准运行模拟...使用 mpirun...
【解决方案2】:

代码中的第一个显式错误是 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;
}

如果您的变量未列出,那么很可能是您如何调用应用程序的问题。另一个选项是您的环境未设置。

【讨论】:

    猜你喜欢
    • 2018-11-18
    • 2010-10-12
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 2010-09-08
    • 1970-01-01
    • 2014-02-11
    • 2012-10-28
    相关资源
    最近更新 更多