Alex Rivera | Logout

I don't get this C/C++ Joke

Asked 2009-09-14T06:44:01.197
34

After reading this article on thedailywtf.com, I'm not sure that I really got the joke.

It says there that some guy changed the code from

int function() 
{ 
  int x;
  char data_string[15];
  ...
  x = 2;
  strcpy(data_string,"data data data");
  ...
}

to

int function() 
{
  int x = 2;
  char data_string[15] = "data data data";
  ...
}

everywhere in the code and that for some reason did inflate the size of the executable from 1 to 2 CDs (or maybe it didn't do that?).

Obviously I'm not familiar enough with C/C++ to get this joke, but what seems strangest is that the 2nd code listing seems "cleaner"—at least from what I've been told in school (that is that initializing variables is a good thing, not a bad one).

c++ c
Edit
Report

1 Answer

24

Depending on the compiler and compiler options, initialization like this

char data_string[15] = "data data data";

results in a lot of move instructions to copy the literal data to stack.

Calling strcpy requires less instructions.

Doing this kind of thing all over a large codebase can increase the binary size significantly.

And of course, he was not spending his time on adding any value.

answered 2009-09-14T07:06:10.330

Your Answer