在C中,将struct作为参数传递给printf的预期行为是什么?

Bus*_*uss 2 c gcc struct code-generation

第二次尝试

好吧,也许我第一次尝试提问这个问题太混乱了.所以,我们再来一次......

在一个带有可变数量参数的函数中,比如printf,当你传递一个struct时,你应该期望生成什么样的代码?

基于这段代码,我问这个问题:

#include <stdio.h>

struct edge{
    int v1, v2;
};

int main(void){
    struct edge edges;

    edges.v1=5;
    edges.v2=20;

    printf("'%d'\n",  edges);

return 0;
}
Run Code Online (Sandbox Code Playgroud)

在我的Windows框中编译时,它传递2个整数参数:

    .file   "simple_test_case2.c"
    .def    ___main;    .scl    2;  .type   32; .endef
    .section .rdata,"dr"
LC0:
    .ascii "'%d'\12\0"
    .text
.globl _main
    .def    _main;  .scl    2;  .type   32; .endef
_main:
    pushl   %ebp
    movl    %esp, %ebp
    andl    $-16, %esp
    subl    $32, %esp
    call    ___main
    movl    $5, 24(%esp)
    movl    $20, 28(%esp)
    movl    24(%esp), %eax
    movl    28(%esp), %edx
    movl    %eax, 4(%esp)
    movl    %edx, 8(%esp)
    movl    $LC0, (%esp)
    call    _printf
    movl    $0, %eax
    leave
    ret
    .def    _printf;    .scl    2;  .type   32; .endef
Run Code Online (Sandbox Code Playgroud)

但是我的linux框生成的代码只是将一个内存地址传递给它:

    .file   "simple_test_case2.c"
    .section    .rodata
.LC0:
    .string "'%d'\n"
    .text
    .align 2
.globl main
    .type   main, @function
main:
.LFB2:
    pushq   %rbp
.LCFI0:
    movq    %rsp, %rbp
.LCFI1:
    subq    $16, %rsp
.LCFI2:
    movl    $5, -16(%rbp)
    movl    $20, -12(%rbp)
    movq    -16(%rbp), %rsi
    movl    $.LC0, %edi
    movl    $0, %eax
    call    printf
    movl    $0, %eax
    leave
    ret
.LFE2:
    .size   main, .-main
.globl __gxx_personality_v0
    .section    .eh_frame,"a",@progbits
.Lframe1:
    .long   .LECIE1-.LSCIE1
.LSCIE1:
    .long   0x0
    .byte   0x1
    .string "zPR"
    .uleb128 0x1
    .sleb128 -8
    .byte   0x10
    .uleb128 0x6
    .byte   0x3
    .long   __gxx_personality_v0
    .byte   0x3
    .byte   0xc
    .uleb128 0x7
    .uleb128 0x8
    .byte   0x90
    .uleb128 0x1
    .align 8
.LECIE1:
.LSFDE1:
    .long   .LEFDE1-.LASFDE1
.LASFDE1:
    .long   .LASFDE1-.Lframe1
    .long   .LFB2
    .long   .LFE2-.LFB2
    .uleb128 0x0
    .byte   0x4
    .long   .LCFI0-.LFB2
    .byte   0xe
    .uleb128 0x10
    .byte   0x86
    .uleb128 0x2
    .byte   0x4
    .long   .LCFI1-.LCFI0
    .byte   0xd
    .uleb128 0x6
    .align 8
.LEFDE1:
    .ident  "GCC: (GNU) 4.1.2 20080704 (Red Hat 4.1.2-48)"
    .section    .note.GNU-stack,"",@progbits
Run Code Online (Sandbox Code Playgroud)

所以这是我的问题,当你只将一个结构传递给一个函数时printf,生成的代码应该只传递一个参数或将所有结构成员推送给它?

第一次尝试

我的一个朋友在Windows上没有按预期工作的程序遇到一些问题.所以我看一下源代码并将测试用例修改为:

#include <stdio.h>

struct edge{
    int v1, v2;
};

int main(void){
    struct edge edges;

    edges.v1=5;
    edges.v2=20;

    //This is the expected behavior for me:
    printf("'%d' '%d' '%d' '%d'\n",  edges.v1,  edges.v1,  edges.v1,  edges.v1);

    //This is supposed to work like this? It should pass the whole struct to printf?
    printf("'%d' '%d' '%d' '%d'\n",  edges,  edges,  edges,  edges);
    printf("'%d' '%d' '%d' '%d'\n",  edges,  edges);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

所以在Windows 7的盒子上测试过

gcc (tdm-1) 4.5.0
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)

输出是这样的:

'5' '5' '5' '5'
'5' '20' '5' '20'
'5' '20' '5' '20'
Run Code Online (Sandbox Code Playgroud)

在一个带有Linux的盒子上

gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-48)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)

我得到了这个输出:

'5' '5' '5' '5'
'5' '5' '5' '5'
'5' '5' '229971840' '-641776368'
Run Code Online (Sandbox Code Playgroud)

linux box one的输出是我预期的...

然后,我看看每个编译器生成的汇编代码,并在我的Windows框中看到代码将整个结构传递给printf,v1和v2成员(这证明了结果).但是在Linux机器上,生成的代码只传递了结构的第一个成员,正如我所期望的那样.

那么在这种情况下应该是什么行为呢?

对我来说,预期的一个是Linux,但也许我错了,这是一个未定义的行为.

R..*_*R.. 8

预期的行为是未定义的行为.C语言要求您传递正确类型的参数(匹配格式字符串)printf,并且没有可以匹配struct类型的格式字符串.

您可以通过以这种方式传递结构来获取用于打印有意义数据的C实现,但它不可移植,取决于调用约定,并且不可靠.不要这样做.只需传递您要打印的个人成员即可.