简体   繁体   中英

C function with incomplete declaration

I can build this example with no errors with gcc -std=C99 -Wall :

void dummy() {}

int main(void) {
    dummy(1, 2, 3);
    dummy(120, 144);
}

The disassembly shows that the function is indeed called twice:

main:
.LFB1:
        .cfi_startproc
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register 6
        movl    $15, %edx
        movl    $14, %esi
        movl    $12, %edi
        movl    $0, %eax
        call    foo
        movl    $300, %esi
        movl    $200, %edi
        movl    $0, %eax
        call    foo
        movl    $0, %eax
        popq    %rbp
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc

I admit that this code should not exist, but, it is allowed and I am wondering in which special case it would be useful.

Any clues?

EDIT

The question calling-c-functions-with-too-many-arguments does not answer this question. It gives information about how to use varadic but it does not provide an example in which an incomplete declaration can be useful.

The question func-vs-funcvoid-in-c99 also does not answer the question. It explains the difference between incomplete and complete prototype which is not my question.

So, it seems my question is not clear enough and I am going to give another example:

Let's imagine I would really leverage an incomplete declaration to use variable arguments without using the varadic method so I wrote my example as:

int main(void) {
    dummy(1, 2, 3);
    dummy(1, 2, 3, 4, 5, 6, 7, 8);
}

Accoding to the calling conventions, the first function will use the CPU registers to pass the parameters while the second call will use the stack.

Now in my dummy function, how can I read these arguements and know whether or not the stack was used?

From C11 standard 6.7.5.3p14 :

An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters.

The void dummy() {} is a function declarator and it is a function definition (it has the {} function body definition). The dummy function has an empty identifier list - there is nothing inside ( ) braces inside the function declaration. That means that the function takes no parameters.

int main(void) {
    dummy(1, 2, 3);
    dummy(120, 144);
}

Calling dummy with any parameters is invalid, because the parameters do not match function declaration. This is undefined behavior.

Now in my dummy function, how can I read these arguments and know whether or not the stack was used?

You cannot. Unless you do some hardware/compiler specific assembly/pointer tricks. C standard does not use a term "stack" and the usage of stack, if it exists and/or is used at all and how it is used, this is architecture and compiler dependent. The stack may not exists on a architecture. To read function arguments you have to declare them in the parameter list inside the function definition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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