gpt4 book ai didi

c - 不同编译器解析命令行的不同段错误

转载 作者:太空宇宙 更新时间:2023-11-04 01:05:05 24 4
gpt4 key购买 nike

我一直在按照 Zed Shaw 的教程学习 C,并在 this exercise 上遇到了一些问题。 .代码如下

#include <stdio.h>

int main(int argc, char *argv[])
{
int i = 0;
while(i < argc) {
printf("arg %d: %s\n", i, argv[i]);
i++;
}
// section removed for brevity
return 0;
}

我使用的是 Windows,不喜欢 virtualboxing 的麻烦,所以我一直在 Cygwin 中运行。我有两个编译器,一个是 Cygwin 自带的 gcc,另一个是 mingw 自带的(gcc)版本,所以我可以使用 DrMemory。

我这样制作文件(名为 ex11.c)

# Makefile
ex11: ex11.c
gcc -o ex11.exe ex11.c
i686-pc-mingw32-gcc.exe -static-libgcc -static-libstdc++ -ggdb -o ex11b.exe ex11.c

# Command Line
>>> make ex11
...
etc

我得到的第二个命令here .

$ gcc --version
gcc.exe (rubenvb-4.6.3) 4.6.3)
$ i686-pc-mingw32-gcc --version
i686-pc-mingw32-gcc (GCC) 4.7.3

然后,当我运行它们(./ex11./ex11b)时,我遇到了问题。在没有命令行参数的情况下运行普通版本(没有 b)会给我一个段错误。带参数运行给我这个输出:

$ ./ex11 a
arg 0: a
arg 1: a

运行mingw版本(带b)没有命令行参数我没有问题:

$ ./ex11b
arg 0: (null)

但随后使用命令行参数 ($ ./ex11b a) 运行相同的程序会出现段错误。

第一个的汇编器输出

    .file   "ex11.c"
.def __main; .scl 2; .type 32; .endef
.section .rdata,"dr"
.LC0:
.ascii "arg %d: %s\12\0"
.text
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
pushq %rbp
.seh_pushreg %rbp
movq %rsp, %rbp
subq $48, %rsp
.seh_stackalloc 48
.seh_setframe %rbp, 48
.seh_endprologue
movl %ecx, 16(%rbp)
movq %rdx, 24(%rbp)
call __main
movl $0, -4(%rbp)
jmp .L2
.L3:
movq 24(%rbp), %rax
addq $72, %rax
movq (%rax), %rcx
leaq .LC0(%rip), %rax
movl -4(%rbp), %edx
movq %rcx, %r8
movq %rax, %rcx
call printf
addl $1, -4(%rbp)
.L2:
movl -4(%rbp), %eax
cmpl 16(%rbp), %eax
jl .L3
movl $0, %eax
addq $48, %rsp
popq %rbp
ret
.seh_endproc
.def printf; .scl 2; .type 32; .endef

第二个的汇编器输出

    .file   "ex11.c"
.def ___main; .scl 2; .type 32; .endef
.section .rdata,"dr"
LC0:
.ascii "arg %d: %s\12\0"
.text
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
LFB6:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
andl $-16, %esp
subl $32, %esp
call ___main
movl $0, 28(%esp)
jmp L2
L3:
movl 12(%ebp), %eax
addl $36, %eax
movl (%eax), %eax
movl %eax, 8(%esp)
movl 28(%esp), %eax
movl %eax, 4(%esp)
movl $LC0, (%esp)
call _printf
addl $1, 28(%esp)
L2:
movl 28(%esp), %eax
cmpl 8(%ebp), %eax
jl L3
movl $0, %eax
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE6:
.def _printf; .scl 2; .type 32; .endef

我想我知道是什么导致了段错误。我已将 i 初始化为 0,因此我有时会尝试获取一个不喜欢的空值。我想知道的是,这些编译器有什么不同,它们会像这样崩溃。

我也很好奇如何重写它以便我可以从 i=0

开始

最佳答案

您的编译器或环境似乎以某种方式损坏。 argv[] 数组的所有元素都必须指向字符串,只有 argv[argc] 必须为 NULL。如果程序名称不可用,则 argv[0] 必须指向一个空字符串 ("")。

可以做的是在循环中测试NULL,但您真的不必:

while(i < argc) {
if (argv[i]) {
printf("arg %d: %s\n", i, argv[i]);
}
i++;
}

关于c - 不同编译器解析命令行的不同段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25660350/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com