1

Is using the main function in the form:

int main(int argc, const char* argv[])

non-portable and non-standard? The C++ standard says that argv is "pointer to pointer to char". I prefer to use the word const to prevent modification and out-of-bounds in an ​​unknown (to me) area of memory.

Is such a form wrong and non-portable?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Harry
  • 119
  • 1
  • 4
  • The correct signature of `main()` is `ìnt main(int argc, char **argv)`. (That's for historical reasons.) – Scheff's Cat Sep 08 '20 at 09:04
  • FYI: [SO: Are the strings in argv modifiable?](https://stackoverflow.com/q/35102922/7478597) (Uhh - it's for C. I try to find something for C++ - have read something just recently...) – Scheff's Cat Sep 08 '20 at 09:06
  • @Scheff OK, it's correct, but is signature `ìnt main(int argc, const char **argv)` uncorrect? – Harry Sep 08 '20 at 09:08
  • 1
    Yes. The possible signatures of `main()` are defined by standard: [Main function](https://en.cppreference.com/w/cpp/language/main_function) (There might be extensions but this is compiler dependent. Obviously, your compiler doesn't like `const char *argv[]`.) ;-) – Scheff's Cat Sep 08 '20 at 09:08
  • I've actually seen `const char* argv[]` several times. I've also seen `const char* const *argv`. I don't see why it shouldn't be portable, if someone knows why should add it as an answer :) – Christian Vincenzo Traina Sep 08 '20 at 09:13
  • 3
    _I prefer to use the word const to prevent modification and out-of-bounds in an ​​unknown to me area of memory._ There is a very easy solution for this: `namespace My { int main(int argc, const char *argv[]) { /* your code */ return 0; } } /* namespace My */ int main(int argc, char **argv) { return My::main(argc, argv); }` – Scheff's Cat Sep 08 '20 at 09:13
  • Both the C and C++ standards specify the signatures of `main()` that an implementation (aka compiler) is required to support. In both, `int main(int argc, char **argv)` (which is equivalent to `int main(int argc, char * argv[])`) is one of those. `int main(int, const char **)` is not, and cannot be relied on for portable code. – Peter Sep 08 '20 at 09:21
  • The historical reason is that the specified signature of `main()` in C++ was inherited from C. In early versions of C, there was no `const` keyword - so the allowed signatures of `main()` were originally specified before `const`, and the signatures of `main()` were not changed when the `const` keyword was subsequently introduced to C. – Peter Sep 08 '20 at 09:27

1 Answers1

3

Your version is indeed unportable.

The variants that the standard allows are

int main() 
int main(int argc, char* argv[])
int main(int argc, char** argv)

The standard allows implementation-defined possibilities, so const might be permitted on some platforms. But if the implementation doesn't define such an alternative then the behaviour of your program is undefined.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • I am surprised. I would have expected that if it is not implemented, then the compiler will reject it, which is better than UB. – Damien Sep 08 '20 at 09:23
  • @Damien: I guess that's why C++ is not for quiche eaters. One of my favourite undefined statements is `unsigned n = -1e0;` – Bathsheba Sep 08 '20 at 09:32
  • 1
    @Damien There's nothing stopping the compiler from issuing a diagnostic in this case. It just hasn't been considered worthwhile to do, I'm guessing. – cigien Sep 08 '20 at 12:25