C++プログラムから環境変数にアクセスしようとしています。だから私は正常に動作テストプログラムを作った:C/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'は' configure'の中に他にも呼び出すことができますか? 'getenv'が呼び出されるたびに、最後の呼び出しの結果が無効になることがあります。 –
@TheDark No. 'configure 'から' getenv() 'を呼び出すことはありません。 – PRP
C++の' new char [1000] 'には正当な理由はほとんどありません。 'std :: string'、' std :: vector '、' std :: array 'のいずれかが良い選択です。この場合、 'std :: string a = getenv();' –
MSalters