1

文字列を double に変換する非常に小さなプログラムがあります。問題は、毎回 0.0000 を出力することです。私を助けてください。

前もって感謝します。

enter code here

$ export LT_LEAK_START=1.5
$ echo $LT_LEAK_START
  1.5

#include <stdio.h>

int main()
{
  double d;
  d=strtod(getenv("LT_LEAK_START"), NULL);
  printf("d = %lf\n",d);
}
Output:
d=0.0000000
4

2 に答える 2

6

含めてみてください

#include <stdlib.h>
于 2011-03-28T11:27:08.303 に答える
1

strtod decl ヘッダー (stdlib.h) を含めていないため、内部 strtod 実装を使用しています (これは単なるスタブのように見えますか?)

root@pinkpony:~# gcc -Wall -g  -o t t.c
t.c: In function ‘main’:
t.c:6: warning: implicit declaration of function ‘strtod’
t.c:6: warning: implicit declaration of function ‘getenv’
t.c:8: warning: control reaches end of non-void function
root@pinkpony:~# gdb ./t
Reading symbols from /root/t...done.
(gdb) run
Starting program: /root/t 

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a96b64 in ____strtod_l_internal (nptr=<value optimized out>, endptr=<value optimized out>, group=<value optimized out>, loc=0x7ffff7dd6580) at strtod_l.c:530
5    30     strtod_l.c: No such file or directory.
        in strtod_l.c
(gdb) bt
#0  0x00007ffff7a96b64 in ____strtod_l_internal (nptr=<value optimized out>, endptr=<value optimized out>, group=<value optimized out>, loc=0x7ffff7dd6580) at strtod_l.c:530
#1  0x00000000004005bc in main () at t.c:6
(gdb) 

sigsegv を無視します。私のプラットフォームでは、stdlib でも宣言されているが内部 gcc impl を持たない getenv() が原因です。

于 2011-03-28T12:02:12.093 に答える