1

I was trying to declare an int array in C++ and found this problem. The following code runs fine on g++ compiler but the compilation fails on Visual Studio. I was following Bruce Eckel and found this code.

#include<iostream>

int main()
{
    const int j = std::cin.get();
    char buf[j];
}

Keeping j just an int would be a problem, that I understand. Since the value of j would be const during the run-time, the program should get compiled. Please correct me if I am wrong anywhere.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Sumit Gera
  • 1,249
  • 4
  • 18
  • 34
  • This has been asked before. See http://stackoverflow.com/questions/18918951/trivial-c-code-why-does-this-compile – alex Sep 20 '13 at 15:07
  • const is a declaration of intention, not a creation of fact. The compiler cannot magically figure out at compile time how to size the buf array here. The idea of const is to be bondage on discipline on you by generating an error if you try to later modify a value you declared as const. Some argue that it is a crutch best avoided. – Tyler Durden Sep 20 '13 at 15:07
  • Note that `j` is not a constant expression if you had used `const int j = 10` it would have worked [since using a literal would have made it a constant integral expression](http://stackoverflow.com/a/21273849/1708801). – Shafik Yaghmour Jul 09 '14 at 13:10

4 Answers4

3

Since the value of j would be const during the run-time, the program should get compiled.

No, the const-ness of j is irrelevant here. C++ currently only supports statically-sized C-arrays. Its size must be a compile-time constant.

If you want an array of dynamic size, use std::vector.

The fact that g++ by default compiles this is a bit unfortunate (for compatibility). You should use the -pedantic flag when using g++ to ensure that such compiler extensions aren’t enabled (using compiler extensions of course isn’t bad in itself, but in this case there’s not really any advantage).

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Use `-pedantic-errors` or `-pedantic -Werror` to disable the extensions. Use `-pedantic` to get warnings about use of extensions. – Steve Jessop Sep 20 '13 at 15:08
2

Variable length arrays are C99 feature both gcc and clang supports them as an extension in C++ but Visual Studio never did and even though they recently added support for C99 is it not supported in C++

Since you are developing in C++ unless you have a good reason to not use it then std::vector or std::string should be sufficient.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
2

You are trying to define buf as a variable-length array. This is a feature of C (not C++) that is supported by g++ as a non-standard extension. Evidently your other compiler does not support it.

I would suggest turning buf into std::vector<char> (or indeed std::string?)

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

I had the same problem. It seems very difficult to create an array of which its length is stored as a variable. What I did is create an additional function:

void Class:: initMyArray(const int size) {
    myArray = new int[size]
}

Now, you just have to call that function and give it your variable. I'm not sure whether this is a proper solution though (I'm not a C++ expert).

Sebi
  • 53
  • 7