繁体   English   中英

MinGW fprintf(vswprintf)导致分段错误

[英]MinGW fprintf (vswprintf) causing a segmentation fault

我有以下示例程序,该程序使用预处理器定义简单的调试功能。

main.c

#include <stdio.h>
#ifdef DEBUG
#define __DEBUG 1
#else
#define __DEBUG 0
#endif
#define dbg_out(fmt, ...) \
    do {if (__DEBUG) fprintf(stdout, "%s:%s:%s():" fmt "\n", __FILE__, \
                           __LINE__, __FUNCTION__, __VA_ARGS__); } while (0)

int main (int argc, char **argv)
{
    dbg_out ("Printing argc: %d", argc);
    return 0;
}

我用调试符号和定义的DEBUG进行编译,如下所示:

gcc main.c -g -DDEBUG -o test.exe

现在,当我编译该程序并运行它时,我得到了带有以下回溯的SIGSEGV

Program received signal SIGSEGV, Segmentation fault.
0x75b7b090 in vswprintf () from C:\Windows\SysWOW64\msvcrt.dll

(gdb) bt

#0  0x75b7b090 in vswprintf () from C:\Windows\SysWOW64\msvcrt.dll
#1  0x75b73633 in msvcrt!fprintf () from C:\Windows\SysWOW64\msvcrt.dll
#2  0x75bb1228 in msvcrt!_iob () from C:\Windows\SysWOW64\msvcrt.dll
#3  0x0040a070 in __register_frame_info ()
#4  0x00401425 in main (argc=1, argv=0x4a2f08) at src/main.c:16

GCC(MinGW)版本为4.8.1 为什么发生此崩溃? 如何解决?

预处理宏__LINE__不是char * ,它是一个int ,所以:

#define dbg_out(fmt, ...) \
    do {if (__DEBUG) fprintf(stdout, "%s:%s:%s():" fmt "\n", __FILE__, \
                           __LINE__, __FUNCTION__, __VA_ARGS__); } while (0)

需要是:

#define dbg_out(fmt, ...) \
    do {if (__DEBUG) fprintf(stdout, "%s:%d:%s():" fmt "\n", __FILE__, \
                           __LINE__, __FUNCTION__, __VA_ARGS__); } while (0)

请注意,如果在启用警告的情况下进行编译(例如gcc -Wall ... ), gcc会警告您。

Please refer the following code.

    #include <stdio.h>
#define DEBUG 1
#ifdef DEBUG
#define __DEBUG 1
#else
#define __DEBUG 0
#endif
#define dbg_out(fmt, ...) \
    do {if (__DEBUG) fprintf(stdout, "%s:%d:%s():" fmt "\n", __FILE__, \
                           __LINE__, __FUNCTION__, __VA_ARGS__); } while (0)

int main (int argc, char **argv)
{
    dbg_out ("Printing argc: %d", argc);
    return 0;
}
~                                                                                 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM