Does the main one have a return address, dynamic link, or return value in C?

According to our book, each function has an activation entry in the run-time stack in C. Each of these activation entries has a return address, a dynamic link, and a return value. Do they also have basic functions?

+4
source share
4 answers

All of these terms are implementation details only. C does not have the concept of "return addresses" or "dynamic links." It does not even have the concept of "stack" at all. Most C implementations have these objects in them, and in these implementations it is possible that they exist for main . However, this is not necessary.

Hope this helps!

+5
source

If you parse functions, you will realize that most of the time the stack does not even contain a return value - often like EAX (intel x86) does. You can also look for “conventions calls” - all this depends a lot on the compiler. C is a language, as interpreted in machine code, it is not its "business".

+2
source

Although it depends on the implementation, it's worth looking at a C program compiled with gcc. If you run objdump -d executable , you will see it parsed, and you will see how main() behaves. Here is an example:

08048680 <_start>: ... 8048689: 54 push %esp 804868a: 52 push %edx 804868b: 68 a0 8b 04 08 push $0x8048ba0 8048690: 68 30 8b 04 08 push $0x8048b30 8048695: 51 push %ecx 8048696: 56 push %esi 8048697: 68 f1 88 04 08 push $0x80488f1 804869c: e8 9f ff ff ff call 8048640 < __libc_start_main@plt > 80486a1: f4 hlt ... 080488f1 <main>: 80488f1: 55 push %ebp 80488f2: 89 e5 mov %esp,%ebp 80488f4: 57 push %edi 80488f5: 56 push %esi 80488f6: 53 push %ebx ... 8048b2b: 5b pop %ebx 8048b2c: 5e pop %esi 8048b2d: 5f pop %edi 8048b2e: 5d pop %ebp 8048b2f: c3 ret

You can see that the main one behaves similarly to a regular function, since it returns normally. In fact, if you look at the linux base documentation, you will see that the __libc_start_main call that we see from _start actually requires main to behave like a regular function.

0
source

In C / C ++, main() written just like a function, but not one. For example, it is not allowed to call main() , it has several possible prototypes (cannot do this in C!). Regardless of the fact that return ed from it is transferred to the operating system (and the program ends).

Individual C implementations can treat main() as a function called “outside” for homogeneity, but no one forces them to do this (or prevent them from switching to any other form of this without telling anyone). There are traditional ways to implement C, but no one is forced to do so. This is simply the easiest way for our typical architectures.

-1
source

All Articles