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.

3
  • This has been asked before. See stackoverflow.com/questions/18918951/… Commented Sep 20, 2013 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. Commented Sep 20, 2013 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. Commented Jul 9, 2014 at 13:10

4 Answers 4

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Use -pedantic-errors or -pedantic -Werror to disable the extensions. Use -pedantic to get warnings about use of extensions.
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?)

Comments

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.

Comments

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.