2

I have a piece of code:

int CPUs = GetNumCPUs();
FILE *newFile[CPUs];

I got an error. It marks 'CPUs' on the second line and says: "expression must have a constant value".

I have tried to use const but it is not working.

3
  • 1
    C++ doesn't have variable length arrays. FILE **newFile = new FILE*[CPUs];, or better, use a std::vector. Commented Jul 23, 2013 at 16:59
  • You need a dynamic array since the number of CPUs isn't "constant"-can't be determined at compile time. Rather than futz around with new and malloc, learn std::vector now and get in the habit of using it. Commented Jul 23, 2013 at 16:59
  • 3
    Whenever you program in C++ and need a dynamic array, you should think of std::vector. Commented Jul 23, 2013 at 17:01

6 Answers 6

5

You can't have a varible sized array in C++. Adding const to CPUs doesn't help, it only makes the variable read-only, but it's still not a compile time constant because it's initialized by a function at run-time.

The usual solution is to use a vector:

std::vector<FILE*> newFile(CPUs);
Sign up to request clarification or add additional context in comments.

Comments

3

The value of GetNumCPUs() may or may not change every time you run the program - so it is not constant. If you want an array that has a variable amount of elements, try std::vector:

std::vector<FILE*> newFile(GetNumCPUs());

Comments

2

In your code, const doesn't mean "constant". In this context, it means the object is read-only — i.e you can't modify the object. You're trying to create a variable length array, which isn't allowed in C++. Use std::vector, use new to allocate memory, or write a C99 program where VLAs like the one you're trying to make are allowed.

5 Comments

Thx guys :) I used FILE **newFile = new FILE*[CPUs]; Now it is working :)
using new to allocate memory isn't really an option.
@user2320928: Don't do that
Nice edit. I had a problem at first with this because you said const didn't mean constant before
I'm not sure I follow. const doesn't mean constant.
0

Using a const doesn't fix all of your problems in this scenario. The problem is that arrays must be initialized at compile time, so if you have a function return a variable (GetNumCPUs()) and assign it to a constant (const int CPUs), the variable isn't known at compile time but runtime, and the compiler can't allocate data space for the array.

Using an std::vector, however, allows for variable storage space.

std::vector<FILE*> newFile(CPUs);

This should work fine. Here's a couple tutorials:

http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm

http://www.dreamincode.net/forums/topic/33631-c-vector-tutorial/

Comments

-2

CPUs is no known at compile time.

2 Comments

This answer doesn't explain really why this is a problem.
Why not? You can't assign an unknown value to something.
-2

You should dynamically create the array with new[]

int CPUs = GetNumCPUs();
FILE** newFile = new (FILE*)[CPUs];

After you're done with it, you're now responsible for deleting it as well:

delete[] newFile;

1 Comment

The poster wanted an array, I gave him array. I don't understand everyone pushing std::vector<> without an explanation / justification.

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.