Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
622 views
in Technique[技术] by (71.8m points)

c - Call function without parameter and parenthesis

In the following code snippet, the main function calls foo function without any parameter and parenthesis. It is strange that this code can be compiled by gcc. I actually check the assembly code and find out that the compiler just ignore this line. So my question is in which situation this kind of code is used? Or the support of gcc is just a coincidence and actually it is totally useless.

int foo(int a,int b)
{
    return a+b;
}
int main()
{
    foo;      // call foo without parameter and parenthesis
    return 0;
}

Its assembly code dumped by objdump -d

00000000004004c0 <main>:
  4004c0:   55                      push   %rbp
  4004c1:   48 89 e5                mov    %rsp,%rbp
  4004c4:   b8 00 00 00 00          mov    $0x0,%eax
  4004c9:   5d                      pop    %rbp
  4004ca:   c3                      retq   
  4004cb:   0f 1f 44 00 00          nopl   0x0(%rax,%rax,1)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is no different than having any other type of expression and ignoring its value, like:

int main(void)
{
  42;
  return 0;
}

there's nothing special, this is not calling the function since the function-call operators () are not being used. All you're doing is "computing" the functions' address, then ignoring it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...