Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

What C hello world actually does [Trigger Warning]

Name: Anonymous 2017-05-21 13:07

Name: Anonymous 2017-05-21 14:50

int write(int file, char* ptr, int len );
#define pstring(x) write(1,(x),sizeof(x)) //replaces puts for const size strings
int main(){pstring("Hello World");}

Name: Cudder !cXCudderUE 2017-05-21 14:58

Another example of how mainstream compilers are absolutely fucking idiotic.

Even for a "Hello world" GCC manages to emit 6 instructions, when only 2 are needed. (Set the pointer to the string and jump to puts. It's a tail-call so no need to return here.)

Name: Anonymous 2017-05-21 15:02

What glibc hello world actually does*
Fuck glibc.

Name: Anonymous 2017-05-21 17:41

Why do they need the compiler to turn printf into puts? Aren't C coders supposed to be ``expert hackers'' who cringe at wasting 3 more bytes to store a string length?

Name: Anonymous 2017-05-21 18:55

>>3
The return value of puts and the return value of main (implicitly 0 here) don't need to be the same.

Name: Anonymous 2017-05-21 18:58

>>3
Why do you post stuff like this? You know compilers are more complicated than that and it's not like they are going to have a special case for hello world.

Name: Anonymous 2017-05-21 19:50

>>6
The main() function is (incorrectly) declared as void in the example, so it's return value is undefined.

>>7
If a compiler is this inefficient on trivial programs, there's no reason to think it would do any better on complex ones. It still has an advantage in speed (compilers can generally run in less time than it would take a human to read the source code), but it's not exactly optimal code generation.

Name: Cudder !cXCudderUE 2017-05-21 23:21

>>5
I guess the printf-to-puts optimisation was simple enough to do, so they included it.

>>6
It is returning the value of puts in this case; otherwise there would be a xor eax, eax (and it would not be a tail call.)

Why they didn't choose to not emit the other 3 useless instructions is not so easy to explain, but I bet it has to do with some stupid design that dates back to RMS and the early days of non-dataflow compilers.

Name: Anonymous 2017-05-22 8:13

You would be surprised but iostream adds a whole new layer of abstract bullshit on top of it.

Name: Anonymous 2017-05-22 10:52

>>10
Why would that be a surprise?

Name: Anonymous 2017-05-22 12:44

>>11
Because its nearly half a megabyte of templates and classes.
check your /usr/include/c++

Name: Anonymous 2017-05-22 12:48

easier way to see the bloat:
g++ -E templatetest.cpp >result1.cpp
#include <iostream>
int main(){std::cout<<"Abc";}

Name: Anonymous 2017-05-22 13:01

>>12
You still haven't explained why that is surprising.

Don't change these.
Name: Email:
Entire Thread Thread List