不同环境下同一段错误代码不同的提示

早上起床,翻开C Primer Plus,看了几页开始往屏幕上敲代码。。。然后杯具出现了。。。
附上我敲出来的代码:


//test.c源码

#include
int main (void)
{
float n1 = 3.0;
double n2 = 3.0;
long n3 = 2000000000;
long n4 = 1234567890;

printf ("%.1e %.1e %.1e %.1e\n",n1,n2,n3,n4);
printf ("%ld %ls\n",n3,n4);
printf ("%ld %ld %ld %ld\n",n1,n2,n3,n4);
system ("pause");
return 0;

}

我最常用的环境是Windows7+Visual C++ 2010,按F5启动调试之后提示:test.exe 中的 0x774c15de 处有未经处理的异常: 0xC0000005: 读取位置 0x499602d2 时发生访问冲突。此时命令行窗口显示的内容是3.0e+000 3.0e+000 3.1e+046 0.0e+000(即正确运行 printf (“%.1e %.1e %.1e %.1e\n”,n1,n2,n3,n4);这一行的结果)

     ntdll.dll!774c15de()
[下面的框架可能不正确和/或缺失,没有为 ntdll.dll 加载符号]
ntdll.dll!774c15de()
ntdll.dll!774b014e()
msvcr100d.dll!_cftoe_l(double pvalue, char buf, unsigned int sizeInBytes, int ndec, int caps, localeinfo_struct plocinfo) 行 438 + 0x1f 字节 C++
msvcr100d.dll!_cfltcvt_l(double
arg, char buffer, unsigned int sizeInBytes, int format, int precision, int caps, localeinfo_struct plocinfo) 行 970 + 0x1d 字节 C++
001bf53c()

msvcr100d.dll!lock_fhandle(int fh) 行 467 C
00000001()
stud04.exe!main() 行 10 + 0x15 字节 C
stud04.exe!
tmainCRTStartup() 行 555 + 0x19 字节 C
stud04.exe!mainCRTStartup() 行 371 C
kernel32.dll!7598339a()
ntdll.dll!774d9ef2()
ntdll.dll!774d9ec5()

错误定位在了osfinso.c中的


int cdecl lock_fhandle (
int fh
)
{
ioinfo *pio = _pioinfo(fh);
int retval=TRUE;

/*
 * Make sure the lock has been initialized.
 */
if ( pio->lockinitflag == 0 ) {

    _mlock( _LOCKTAB_LOCK );
    __TRY
        if ( pio->lockinitflag == 0 ) {
            if ( !InitializeCriticalSectionAndSpinCount( &(pio->lock), _CRT_SPINCOUNT )) {
                /*
                 * Failed to initialize the lock, so return failure code.
                 */
                retval=FALSE;
            }
            pio->lockinitflag++;
        }
    __FINALLY
        _munlock( _LOCKTAB_LOCK);
    __END_TRY_FINALLY
}

if(retval)
{
    EnterCriticalSection( &(_pioinfo(fh)->lock) );
}

return retval;

}

/** void _unlock_fhandle(int fh) - unlock file handle
Purpose:

  • Release the lock associated with passed file handle.
    Entry:
  • int fh - CRT file handle
    Exit:
    Exceptions:

    **
    /

    研究半天没看懂。。。于是开虚拟机,Ubuntu11.10,以下是编译/运行结果:

    tuccuay@tuccuay-vm12:~$ cd Desktop
    tuccuay@tuccuay-vm12:~/Desktop$ gcc test.c
    test.c: In function ‘main’:
    test.c:9:2: warning: format ‘%e’ expects argument of type ‘double’, but argument 4 has type ‘long int’ [-Wformat]
    test.c:9:2: warning: format ‘%e’ expects argument of type ‘double’, but argument 5 has type ‘long int’ [-Wformat]
    test.c:10:2: warning: format ‘%ls’ expects argument of type ‘wchar_t *’, but argument 3 has type ‘long int’ [-Wformat]
    test.c:11:2: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 2 has type ‘double’ [-Wformat]
    test.c:11:2: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 3 has type ‘double’ [-Wformat]
    tuccuay@tuccuay-vm12:~/Desktop$ ./a.out
    3.0e+00 3.0e+00 0.0e+00 -5.5e+303
    Segmentation fault
    tuccuay@tuccuay-vm12:~/Desktop$


很明显,在编译的时候已经报错了,在运行的时候显示的是“Segmentation fault”。Google之,还是没有找到有意义的解决方案。。。
再从Ubuntu11.10转战WindowsXP。。。以及Visual C++ 6.0。
Ctrl+F7编译,test.obj - 0 error(s), 0 warning(s)。
F7组建,test.exe - 0 error(s), 0 warning(s)。、
F5运行,在正确输出第一个printf之后直接弹出对话框:
test.exe - 应用程序错误
“0x00402c85”指令引用的”0x499602d2”内存。该内存不能为”read”。
好吧,我发现问题了,在所有环境下的相同点就是都能输出第一个printf,然后出错,于是检查了一下第二行,结果是我把%ld给敲成%ls了。。。