I recently saw something curious. In the HHVM source code, the very first 3 lines of the main()
function read as follows:
if (!argc) {
return 0;
}
It's a bit silly, but still, I just can't help wondering... why return 0!? It's not that I think there's some correct way to handle this, but returning 0, usually associated with success, seems particularly inappropriate.
Besides not crashing, is there ever a case where there's an appropriate response to argc
being 0? (Or even less than 0?) Does it ever matter?
The only way I know of to end up in a case with argc
of 0 is with exec()
and friends. If for some reason that does happen, it's almost certainly a bug in the caller and the callee can't do much about it.
(tagged as C and C++ because I expect that the answer is the same for the two languages)
Edit: To try and make the question less vague and philosophical, I'll offer an alternative.
if (!argc) {
puts("Error: argc == 0");
return 1;
}
The key points are that there's an indication of the error and a non-zero value is returned. It's extremely unlikely this would be needed, but if it was you might as well try to indicate the error. On the other hand, if the detected error is as serious as argc
equal to 0, maybe there's a reason it would be bad to try and access stdout or the C standard library.
argv(ARGument Vector) is array of character pointers listing all the arguments. If argc is greater than zero,the array elements from argv[0] to argv[argc-1] will contain pointers to strings. Argv[0] is the name of the program , After that till argv[argc-1] every element is command -line arguments.
Yes, it can be zero, meaning that argv[0] == NULL . It's a convention that argv[0] is the name of the program. You can have argc == 0 if you launch yourself the binary, like with execve family and don't give any argument.
argc is the count of arguments, and argv is an array of the strings. The program itself is the first argument, argv[0] , so argc is always at least 1. So, argc is 2 when the program is run with one command-line argument.
Note that the C11 standard explicitly allows for argc == 0
:
5.1.2.2.1 Program startup
¶1 The function called at program startup is named
main
. The implementation declares no prototype for this function. It shall be defined with a return type ofint
and with no parameters:int main(void) { /* ... */ }
or with two parameters (referred to here as
argc
andargv
, though any names may be used, as they are local to the function in which they are declared):int main(int argc, char *argv[]) { /* ... */ }
or equivalent;10) or in some other implementation-defined manner.
¶2 If they are declared, the parameters to the
main
function shall obey the following constraints:
- The value of argc shall be nonnegative.
argv[argc]
shall be a null pointer.- If the value of
argc
is greater than zero, the array membersargv[0]
throughargv[argc-1]
inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup. The intent is to supply to the program information determined prior to program startup from elsewhere in the hosted environment. If the host environment is not capable of supplying strings with letters in both uppercase and lowercase, the implementation shall ensure that the strings are received in lowercase.- If the value of
argc
is greater than zero, the string pointed to byargv[0]
represents the program name;argv[0][0]
shall be the null character if the program name is not available from the host environment. If the value ofargc
is greater than one, the strings pointed to byargv[1]
throughargv[argc-1]
represent the program parameters.- The parameters
argc
andargv
and the strings pointed to by theargv
array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.10) Thus,
int
can be replaced by a typedef name defined asint
, or the type ofargv
can be written aschar ** argv
, and so on.
The two bullet points saying 'if the value of argc
is greater than zero' clearly allow argc == 0
, though it would be unusual for that to be the case.
Theoretically, therefore, a program could take precautions against it, though argv[0] == 0
even if argc == 0
, so as long as the code doesn't dereference a null pointer, it should be fine. Many programs, perhaps even most, do not take such precautions; they assume that argv[0]
will not be a null pointer.
I think it's just a case of Defensive programming due to the following snippet in the HHVM's sorce code (file hphp/hhvm/main.cpp):
int main(int argc, char** argv) {
if (!argc) {
return 0;
}
HPHP::checkBuild();
int len = strlen(argv[0]);
In the line:
int len = strlen(argv[0]);
if argc == 0
-> argv[0] == NULL
and strlen(argv[0])
will cause a segmentation fault.
I'm not familiar with HHVM but they can just suppose some program can call the program without arguments (not even the program name).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With