Is main() a callback function?
-
Interesting, I didn't know/have forgotten about the re-entrancy. Seems you can do it in C#, but that's with a capital 'M'ain, and is a language for wimps.
Regards, Rob Philpott.
-
I can't make up my mind.
Regards, Rob Philpott.
-
Interesting, I didn't know/have forgotten about the re-entrancy. Seems you can do it in C#, but that's with a capital 'M'ain, and is a language for wimps.
Regards, Rob Philpott.
The non-re-entrancy part comes about because you can't call it yourself. It's not a callback in the strict sense of the term but in effect it is if you think of it as the designated function for the OS to call to run the program. It is not specified in code (this is why it fails the strict definition) but it is implicitly known to the linker and can be overridden. In the case of programs for Windows, it IS overridden to be WinMain.
"They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"
-
Yes, but I kind of expect the OS to call my method at the given time of the process starting up...? And the address is passed to the OS to be called back on, just through an extra layer of module EXPORTS etc. It's a pedant's dream this. I should move on.
Regards, Rob Philpott.
The OS doesn't call your
main
method at all - there are three places it can start: the MZ Stub (which for Windows apps will just print "this program cannot be run in MSDOS mode" and quit the app), the NE or (for more modern apps) the PE: Portable Executable - Wikipedia[^]. And EXE files (even old MSDOS 16 bit apps) don't callmain
immediately anyway, they do allocation and static initialisation before they are ready to start running the code you wrote!"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
-
I can't make up my mind.
Regards, Rob Philpott.
It probably depends on the language, compiler, system, etc. As far as I know, in the languages I use, there nothing special about call-back functions, it's only about how an ordinary function is used. There's no reason to declare that no language will ever allow it. I seem to recall having a desire for a recursive main. : ) Just because.
-
Interesting, I didn't know/have forgotten about the re-entrancy. Seems you can do it in C#, but that's with a capital 'M'ain, and is a language for wimps.
Regards, Rob Philpott.
Rob Philpott wrote:
C# ... is a language for wimps
To quote my elderly cat, "Fight me, bitch."
Software Zen:
delete this;
-
Well it never called me back - and I thought we had such a connection... :(( :(( :mad: :(( :((
I, for one, like Roman Numerals.
It called me back once, but it was drunk at the time.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
-
Rob Philpott wrote:
C# ... is a language for wimps
To quote my elderly cat, "Fight me, bitch."
Software Zen:
delete this;
It's how I express my intent daily to my ungrateful computer - I'm hooked, but its still the kid's soft play of computer languages. :-D
Regards, Rob Philpott.
-
I can't make up my mind.
Regards, Rob Philpott.
How dare you ask programming questions in the lounge?!! I'm outraged. :mad:
Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.
-
How dare you ask programming questions in the lounge?!! I'm outraged. :mad:
Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.
Yeah, it's good to be reckless sometimes. Send me the codez and I'll go away. :)
Regards, Rob Philpott.
-
The OS doesn't call your
main
method at all - there are three places it can start: the MZ Stub (which for Windows apps will just print "this program cannot be run in MSDOS mode" and quit the app), the NE or (for more modern apps) the PE: Portable Executable - Wikipedia[^]. And EXE files (even old MSDOS 16 bit apps) don't callmain
immediately anyway, they do allocation and static initialisation before they are ready to start running the code you wrote!"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
That reminds me of a thing I read some time back, titled something along the lines of 'the 50 things Windows does before hitting main()'. Can't find it but it's out there somewhere, by one of the SysInternals lot I think. It was both interesting and really boring at the same time.
Regards, Rob Philpott.
-
The non-re-entrancy part comes about because you can't call it yourself. It's not a callback in the strict sense of the term but in effect it is if you think of it as the designated function for the OS to call to run the program. It is not specified in code (this is why it fails the strict definition) but it is implicitly known to the linker and can be overridden. In the case of programs for Windows, it IS overridden to be WinMain.
"They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"
Rick York wrote:
you can't call it yourself.
Indeed you can, but you need to know what you are doing.
Rick York wrote:
but it is implicitly known to the linker
Not quite, there is a reference to it in the run time libraries which must be satisfied at link time.
Rick York wrote:
in the case of programs for Windows, it IS overridden to be WinMain.
But there is a main() inside the Windows libraries, which again gets called by the run time (unless it has changed in the last 20+ years). And that then calls in to WinMain.
-
Interesting, I didn't know/have forgotten about the re-entrancy. Seems you can do it in C#, but that's with a capital 'M'ain, and is a language for wimps.
Regards, Rob Philpott.
C is for wimps Real programmers use butterflies
Real programmers use butterflies
-
It called me back once, but it was drunk at the time.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
-
I can't make up my mind.
Regards, Rob Philpott.
I figure every function is a callback function - it's just a matter of perspective. :-D
Real programmers use butterflies
-
I can't make up my mind.
Regards, Rob Philpott.
It's an "entry point"; "Main" wasn't something that was part of a "response", other than "call static Main in class x".
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food
-
I can't make up my mind.
Regards, Rob Philpott.
If you take the view that your application is an interruption to a busy operating system's schedule, but it figures it should let you do your thing before you whine and get all bitchy about it, then yes, it is a callback function.
Software Zen:
delete this;
-
I can't make up my mind.
Regards, Rob Philpott.
Are you asking if it's masculine or feminine? I've no idea, and I don't want to look, just in case.
I wanna be a eunuchs developer! Pass me a bread knife!
-
I was just reading this yesterday[^]
Quote:
All C++ programs must have a main function. If you try to compile a C++ .exe project without a main function, the compiler will raise an error. (Dynamic-link libraries and static libraries don't have a main function.) The main function is where your source code begins execution, but before a program enters the main function, all static class members without explicit initializers are set to zero. In Microsoft C++, global static objects are also initialized before entry to main. Several restrictions apply to the main function that do not apply to any other C++ functions. The main function: * Cannot be overloaded (see Function Overloading). * Cannot be declared as inline. * Cannot be declared as static. * Cannot have its address taken. * Cannot be called.
But, maybe you are thinking it is a callback from the OS? Or maybe you're just asking a rhetorical question? :)
raddevus wrote:
* Cannot have its address taken. * Cannot be called.
Lies. You can call main, and you can take its address. This compiles and runs in VS2019:
#include int main(int argc, char **argv)
{
if(argc <= 1) {
auto main_ptr{main};
std::cout << "pointer to main = " << main_ptr << std::endl;
std::cout << "exiting ..." << std::endl;
return 0;
}
std::cout << "argc = " << argc << std::endl;
main(--argc, argv);
}Interestingly, in linux you
auto main_ptr{main}
is 1, but for windows it looks like an address: 0008151E Update: I should also point out that the instances where you might need to call main from within you program are vanishingly small. In general, if you think you need to, you're almost certainly wrong. -
Did it say - "I want to C you again!" ? And did you reply #ly that you had moved on? :-D
I, for one, like Roman Numerals.
Difficult to tell, it was yelling a lot and slurring it's words.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!