user1613769
user1613769

Reputation: 11

value of the variable after storage allocation in c++

Is it possible for the compiler to know the value of the variable after storage allocation in c++? And what is the case with c compiler if the following code is given

const int bufsize = 100;
char buf[bufsize];

The textual explanation is as follows: "In C, you will get an error, even though it seems like a rational thing to do. Because bufsize occupies storage somewhere, the C compiler cannot know the value at compile time." Why is it said so?

Upvotes: 0

Views: 133

Answers (5)

perilbrain
perilbrain

Reputation: 8207

History of this question traces back to 80's where Late Dennis Ritchie stated

By contrast, the apparently most obvious language generalization (that is, allowing general expressions instead of constants in array bounds) complicates the type structure of the language, causes difficulties with the calculus of datatypes, and causes nonuniformity in discovering the sizes of arrays.
(Extract from Journal of C Language Translation, vol 2 number 2,By Dennis Ritchie)

With this paper he appealed to the standards body to avoid the use of VLA.

However in later releases of C (C99) this was reviewed and they allowed the use of variable length array under non global scope.

In C++ this is valid as const declared variables are considered constant expression.

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490663

C89 doesn't allow this, because a const int is not considered a constant expression, and the size of an array must be specified as a constant expression.

In C99 (and newer), variable length arrays eliminate the requirement that the size of an array be given as a constant expression, so this is allowed (where a VLA is allowed, which is basically: with auto storage class -- not as a static or a global).

C++ requires that the size of an array be specified as a constant expression (like C89), but allows a const int as a constant expression1, so this is allowed.


1 As @JamesKanze pointed out, just being a const int doesn't guarantee that a variable's value will be considered a constant expression. Only a const int with a visible initializer that is itself a constant expression is allowed -- but those restrictions are met here.

Upvotes: 3

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361762

"In C, you will get an error, even though it seems like a rational thing to do. Because bufsize occupies storage somewhere, the C compiler cannot know the value at compile time." Why is it said so?

Because in C, bufsize is not const expression; it is a read-only value:

const int bufsize = 100; //read-only in C
                         //constant expression in C++

The difference between const expression and readonly is that the former means the value is known to the compiler (at compile-time itself) while the latter means the value is readonly, the value isn't known to the compiler.

Note that in C++, buffsize and 100 are const expressions, while in C, only 100 is const expression. The size of the declared array needs to be const expression; So the following is allowed only in C++ (and in C99 which allows VLA which is yet another story):

char buf[bufsize]; //ok in C++ (and C99)
                   //error in C89, C90

Upvotes: 1

Mike Seymour
Mike Seymour

Reputation: 254741

In C99, this is fine if the code is inside a function; the variable is treated as a run-time value, but arrays can have variable length. It's not allowed if it has static storage.

In C++, this is fine; a const integer variable can be used as a compile-time value.

In older versions of C, it wasn't allowed; the variable was a run-time value, and variable-length arrays did not exist.

Why is it said so?

Because that's how the language designers decided the languages should behave.

Upvotes: 3

Alok Save
Alok Save

Reputation: 206636

In C const declarations do not produce constant expressions, while they do in C++.
So,

const int bufsize = 100;
char buf[bufsize];

is valid in C++ but not in C.

However, note that Since c99 the C standard allows Variable Length Arrays(VLA), VLA are not a part of the C++ Standard but most compilers support them through compiler extensions.

Upvotes: 2

Related Questions