M
MartinBroadhurst
I've never used a negative number of command line arguments.
Martin
Martin
MartinBroadhurst said:I've never used a negative number of command line arguments.
I think the argc/argv convention predates the introduction of unsigned
int into the language. (In ancient versions of C, I think it was common
to use pointers when you needed to do unsigned arithmetic.)
Using a signed type might also make some idioms a bit easier; for
example, a test for argc >= 0 would always (appear to) succeed if argc
were unsigned.
Finally, have you ever used more than INT_MAX command line arguments?
MartinBroadhurst said:That seems a likely explanation.
Are there implementations in which argc can be 0 then?
Not unless I've perpetrated some sort of console pasting disaster. I
wasn't so concerned about the efficiency of using an unnecessarily
large type, more the semantics.
Yes. UNIX. Just have a program call, for example:
execl(path, (char *) NULL);
On most modern Unix systems that won't initialise main()'s argc to
_anything_. Program invocation with a null argv is generally taken
as an instruction to the dynamic linker. You'll get a listing of
requested and linked in libraries and the program will terminate
without ever calling main().
Dr Nick said:That's gives me a warning about not enough variable arguments. execl
needs a null terminated list and clearly the compiler "knows" to expect
a real one in there.
With an extra NULL, or with execv I get a segfault from:
#include <unistd.h>
int main(void) {
execv("/bin/ls",(char * const *)NULL);
/* or, and ignore the warning */
execl("/bin/ls",(char *)NULL);
/* or */
execl("/bin/ls",(const *)NULL,(char *)NULL);
return 0;
}
and debugging does seem to show it was in the dynamic linker.
execl("/bin/ls","",(char * const *)NULL);
works but, of course, has an argument.
So I don't think it's as easy as we thought!
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.