3

According to my C++ textbook, the following expression:

cout << "Enter number of students\n";
cin >> number;
int score [number];

Is an ILLEGAL expression. I cannot use a variable for the array size.

Why can't I do this? (I'm not looking for an alternative, I haven't gotten into pointers, vectors etc, but I want to understand this behavior.)

13
  • 5
    Because C++ doesn't allow it. Commented Sep 29, 2014 at 3:04
  • 1
    In C int score[number]; is legal. Variable length arrays. Commented Sep 29, 2014 at 3:05
  • 3
    @MartinJames : Your reasoning is flawed, stack objects do not occupy space in the object file. Also ISO C99 supports variable length arrays. Commented Sep 29, 2014 at 3:05
  • 1
    @Martin James In C as the post is tagged C. int score[number]; would need number * sizeof(int) bytes. Thus is usually on the stack - quite easy for the compiler to generate code that allocates this space at run time. Commented Sep 29, 2014 at 3:09
  • 2
    Short answer: It's not allowed because the C++ standard committee in the '90s decided not to allow it. However it has been proposed that C++17 will add support for this. As to why they didn't permit it originally: there wasn't really a good reason to force compiler vendors to support it, as C++ provides better ways to achieve the same goal. Commented Sep 29, 2014 at 3:12

2 Answers 2

5

Variable length arrays were not supported in ISO C90/ANSI C89 from which C++ is derived. While VLAs were added in C99 which deviates from C++, they are arguably unnecessary in C++ which has STL container classes to provide more flexible methods of storing multiple objects.

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

2 Comments

On the other hand, regarding "they are arguably unnecessary in C++", they are also arguably useful in C++ since we have an active proposal and -- almost nothing at all is absolutely necessary.
@Cheersandhth.-Alf: That the thing about "arguable" - you can argue about it. I would not say "nothing at all is absolutely necessary", certain features in a programming language are necessary to make it "Turing Complete". It is proposed, but no one on the ISO committee seems in a hurry.
0

In C++, the compiler must know the amount of memory to allocate for an array at compile time. However, the value of a variable is not known until run time. This is why you are not allowed to use a variable for the size of an array.

If you are required to use arrays for a class project, I suggest using a const to define the maximum size that you will allow. Later on, you will learn how to user other techniques such as pointers and STL containers (such as std::vector).

9 Comments

Doesn't actually answer the question. Also, no mention of vectors.
Note that this is an antique style, since 1998 it is preferred to write std::vector<int> number;
@NeilKirk Technically you're right that it doesn't answer the question, but it does provide an alternative for what the OP wants to achieve
Not what I was looking for. I'm not looking for an alternative, I haven't gotten into pointers, vectors etc. I want to understand.
@MattMcNabb That might be acceptable if the question were "how can I have a dynamically sized array in C++". But in this case, the OP is clearly aware of the restriction and likely how to get around it, it's a why question. For that reason I find it hard to believe that this answer does anything to lead the asker on the path towards the ultimate answer.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.