Jump to content
The Dark Mod Forums
Sign in to follow this  
OrbWeaver

[c++] What Is The Point Of This Function?

Recommended Posts

I found this gem in DarkRadiant - I think its purpose is to remove null entries from argv[], but why they bothered shifting stuff around when they could just copy the valid items into a new array I have no idea.

 

void args_init(int argc, char* argv[])
{
 int i, j, k;
 for (i = 1; i < argc; i++)
 {
for (k = i; k < argc; k++)
  if (argv[k] != 0)
	break;
if (k > i)
{
  k -= i;
  for (j = i + k; j < argc; j++)
	argv[j-k] = argv[j];
  argc -= k;
}
 }
 g_argc = argc;
 g_argv = argv;
}

 

I would have done it like this:

 

void args_init(int argc, char* argv[]) {
  int inPtr, outPtr;
  char* outArgv[argc];

  for (inPtr = outPtr = 0; inPtr < argc; inPtr++) {
  if (argv[inPtr] == 0) // null pointer
	 break;
  else
	 outArgv[outPtr++] = argv[inPtr];
  }

  g_argc = outPtr;
  g_argv = outArgv;
}

 

Maybe some experienced programmer can point out why my version is crap, but it seems to me to do the same thing with more readability and fewer lines of code.

Share this post


Link to post
Share on other sites

i dont think in something that complex, anybody cares about 5 lines more code :P

and readability and understandablitiy is always in the eye of the beholder, and even more so in the eye of the original programmer ;)


fan.gif

Share this post


Link to post
Share on other sites
void args_init(int argc, char* argv[])
{
 int i, j, k;
 for (i = 1; i < argc; i++)
 {
for (k = i; k < argc; k++)
  if (argv[k] != 0)
	break;
if (k > i)
{
  k -= i;
  for (j = i + k; j < argc; j++)
	argv[j-k] = argv[j];
  argc -= k;
}
 }
 g_argc = argc;
 g_argv = argv;
}

 

Maybe some experienced programmer can point out why my version is crap, but it seems to me to do the same thing with more readability and fewer lines of code.

 

IMO this function is pretty dangerous. The value of k is undefined after the first for loop, because it depends on the compiler what value k has. It can be 'i', it can be 'i+1' or it can be some totally different value. You might already see different behaviours compiling this on Visual Studio 6 or gcc, so I would suggest rewriting it. According to the C standard the loopvariable is not defined when the loop ended and it is up to the compiler developer to what it contains.


Gerhard

Share this post


Link to post
Share on other sites
IMO this function is pretty dangerous. The value of k is undefined after the first for loop, because it depends on the compiler what value k has. It can be 'i', it can be 'i+1' or it can be some totally different value.

 

Good point, I didn't notice that. k is only defined within the for statement so it may not even be visible after the loop has finished.

 

You might already see different behaviours compiling this on Visual Studio 6 or gcc, so I would suggest rewriting it.

 

Yeah, I think I'll replace it. I don't understand why this is even necessary since argv[] shouldn't contain any null pointers.

 

Incidentally I am in the process of upgrading the build scripts so that DarkRadiant can be built with GCC on both Windows and Linux. Most of the work is done except for the OpenGL libraries, which require some more investigation since libGL is not available under Windows.

Share this post


Link to post
Share on other sites

k is defined outside, so it would be ok to use it, otherwise you couldn't even compile it. :) But I think replacing it is the best thing, because you can make sure that no such compiler dependencies exist.


Gerhard

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...