News for hackers. Stuff that matters.
Re:Who Needs Main() Anyway? by jeffreygoesto on Saturday January 21, 2023 @06:36AM (#34465721)
Sigh. Wait until she learns about the static initialization order fiasco...
Re:Who Needs Main() Anyway? by moloch-hai on Saturday January 21, 2023 @10:33AM (#34467082)
She knows. Code where it matters is wrong.
Re:Who Needs Main() Anyway? by sargstuff on Saturday January 21, 2023 @10:24PM (#34473924)
Sounds like a have to C it to believe it.
Re:Who Needs Main() Anyway? by xjwm on Saturday January 21, 2023 @10:05AM (#34466871)
I've written code like this in C to create library functions that will be linked in to other projects, but where there is no 'main' function in the C file. Do people use C++ the same way, or is it best practice to include 'main?'
Re:Who Needs Main() Anyway? by MathMonkeyMan on Saturday January 21, 2023 @12:58PM (#34468448)
It's the same as in C. The observation here is that variables with static storage duration can be initialized by running user code before main() begins.
Re:Who Needs Main() Anyway? by xjwm on Saturday January 21, 2023 @05:31PM (#34471371)
Oh! Thank you. That makes much more sense now.
Re:Who Needs Main() Anyway? by colejohnson66 on Sunday January 22, 2023 @01:48PM (#34480807)
It’s also the same in .NET. If your Main() function references types that have a static constructor, the constructors could run before Main().

It’s a surprising thing when one first discovers it, but it makes a lot of sense.

Re:Who Needs Main() Anyway? by moloch-hai on Saturday January 21, 2023 @10:32AM (#34467078)
It didn't crash until after its work was done. It could even have trapped the segfault and exited cleanly.
Re:Who Needs Main() Anyway? by marssaxman on Saturday January 21, 2023 @07:41PM (#34472717)
Couldn't just call exit(0)?
Re:Who Needs Main() Anyway? by moloch-hai on Saturday January 21, 2023 @09:21PM (#34473491)
Maybe, or _exit().
Re:Who Needs Main() Anyway? by 0xcafefood on Saturday January 21, 2023 @11:42AM (#34467658)
A more useful application of this same idea ("you can do stuff during static initialization") is for building a static registry.

https://artificial-mind.net/blog/2020/10/17/static-registrat... gives a good overview.

Re:Who Needs Main() Anyway? by sargstuff on Saturday January 21, 2023 @10:53PM (#34474067)
[1] p.99 information might be of interest

   cc -Wl,-m main.c > main.linker.map
related search terms: _crtStartUp crt0 crt1

[1] : https://progforperf.github.io/Expert_C_Programming.pdf

Re:Who Needs Main() Anyway? by rramadass on Sunday January 22, 2023 @12:41AM (#34474693)
Is this what passes for "technical know-how" nowadays?

Just read Hongjiu Lu's classic paper ELF: From The Programmer's Perspective to get an idea of how everything comes together in C/C++ land.

Re:Who Needs Main() Anyway? by sargstuff on Sunday January 22, 2023 @09:48AM (#34477849)
But in today's non-programmer post-covid world, ELF is seasonal, COFF is year round.

edit: COFF phoneticly eqivalent to cough.