0

Am presently reading a book named C Primer 5th edition by Barbara E. Moo, Josée Lajoie, and Stanley B. Lippman. While I was reading a topic called array I encountered a problem related to the defining and initialization part of the array, and also a statement which confused me. The statement stated that,

The dimension of an array must be known at the compile time hence the dimension must be a constant expression

The code on the other hand was,

unsigned cnt = 42; // not a constant expression
constexpr unsigned sz = 42; // constant expression
// constexpr see § 2.4.4 (p. 66)
int arr[10]; // array of ten ints
int *parr[sz]; // array of 42 pointers to int
string bad[cnt]; // error: cnt is not a constant expression
string strs[get_size()]; // ok if get_size is constexpr, error otherwise

When I tried to run the code as,

#include <bits/stdc++.h>
#include<string>
using std::string;

int main()
{
     unsigned cnt = 42;          // not a constant expression
     string bad[cnt];         // error: cnt is not a constant expression
     return 0;
}

The following code threw no errors and run succesfully, with the output as,

PS E:\C++> cd "e:\C++\" ; if ($?) { g++ rough1.cpp -o rough1 } ; if ($?) { .\rough1 }

Well my question is that,

Is the following statement and the code error in the book related to the previous versions of cpp? Please explain in detail if possible, both the statement and the code part.

Reborn
  • 23
  • 4
  • In Standard C++, the size of an array *must be* a **compile time constant**. If your compiler compiles the above code(2nd code snippet), you're using a compiler **extension**. – Jason Jan 11 '22 at 17:26
  • Some compilers provide an unfortunate (and non-conforming) extension that allows this code without telling you that it's an extension. – Pete Becker Jan 11 '22 at 17:45
  • Side note: Use of `#include ` along with `#include` suggests you don't know what `#include ` does and how one is supposed to use it. [Here is some recommended reading on the subject](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). My general rule of thumb when I see `#include ` in code is to approach the code that follows as though it will be a mess that will be hard to learn from. I'm rarely disappointed. – user4581301 Jan 11 '22 at 17:57
  • 1
    Does this answer your question? [In C++ books, array bound must be constant expression, but why the following code works?](https://stackoverflow.com/questions/5947661/in-c-books-array-bound-must-be-constant-expression-but-why-the-following-cod) – BoP Jan 11 '22 at 17:58
  • Some reading on the why: [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – user4581301 Jan 11 '22 at 18:00
  • Thanks for the comment @AnoopRana and Pete Becker , as of now am using Visual Studio Code with MinGW compiler. Could you please suggest how to improve these kind of errors, or which compiler would be appropriate for the usage? – Reborn Jan 11 '22 at 18:03
  • @Reborn Just make the habit of compiling your program with different compilers. Some of the most commonly used compilers include: **gcc** and **clang**. Another suggestion would be to **enable all warnings** when compiling your program. You're welcome. – Jason Jan 11 '22 at 18:14
  • I'm not sure how you specify it in VS Code, but you want to add `-pedantic` to the command line. I'd also add `-Wall` and `-Wextra`, and since the more you optimize code the closer the compiler looks at the code and may find more things to warn about, adding `-O3` to get a highly optimized build sometimes shakes even more warnings loose. – user4581301 Jan 11 '22 at 19:46

0 Answers0