Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Explanation needed for const int "error: variably modified ... atfile scope."

Reply
Thread Tools

Explanation needed for const int "error: variably modified ... atfile scope."

 
 
Poster Matt
Guest
Posts: n/a
 
      06-15-2010
Hi,

The following code results in the following error with GCC (v.4.3.2):

const int eodSize = 8192;
char eodContents[eodSize];

error: variably modified 'eodContents' at file scope


It is, of course, easily solved by ditching the 'const int' and using a #define
instead like this:

#define eodSize 8192
char eodContents[eodSize];

I would understand the error if "const int eodSize = 8192;" was not a constant
but since it is a constant why does the compiler not allow it?

Thanks.
 
Reply With Quote
 
 
 
 
bart.c
Guest
Posts: n/a
 
      06-15-2010

"Poster Matt" <postermatt@no_spam_for_me.org> wrote in message
news:bbQRn.22678$(E-Mail Removed)2...
> Hi,
>
> The following code results in the following error with GCC (v.4.3.2):
>
> const int eodSize = 8192;
> char eodContents[eodSize];
>
> error: variably modified 'eodContents' at file scope
>
>
> It is, of course, easily solved by ditching the 'const int' and using a
> #define instead like this:
>
> #define eodSize 8192
> char eodContents[eodSize];
>
> I would understand the error if "const int eodSize = 8192;" was not a
> constant but since it is a constant why does the compiler not allow it?


const doesn't mean what you think.

Just use:

enum {eodSize=8192};

as #define has it's own problems.

--
Bartc

 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      06-15-2010
Poster Matt <postermatt@no_spam_for_me.org> writes:

> The following code results in the following error with GCC (v.4.3.2):
>
> const int eodSize = 8192;
> char eodContents[eodSize];


This is in the FAQ:

11.8: I don't understand why I can't use const values in initializers
and array dimensions, as in

const int n = 5;
int a[n];

A: The const qualifier really means "read-only"; an object so
qualified is a run-time object which cannot (normally) be
assigned to. The value of a const-qualified object is therefore
*not* a constant expression in the full sense of the term. (C
is unlike C++ in this regard.) When you need a true compile-
time constant, use a preprocessor #define (or perhaps an enum).

References: ISO Sec. 6.4; H&S Secs. 7.11.2,7.11.3 pp. 226-7.
--
"When in doubt, treat ``feature'' as a pejorative.
(Think of a hundred-bladed Swiss army knife.)"
--Kernighan and Plauger, _Software Tools_
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-15-2010
Poster Matt <postermatt@no_spam_for_me.org> writes:
> The following code results in the following error with GCC (v.4.3.2):
>
> const int eodSize = 8192;
> char eodContents[eodSize];
>
> error: variably modified 'eodContents' at file scope
>
>
> It is, of course, easily solved by ditching the 'const int' and using
> a #define instead like this:
>
> #define eodSize 8192
> char eodContents[eodSize];
>
> I would understand the error if "const int eodSize = 8192;" was not a
> constant but since it is a constant why does the compiler not allow
> it?


Because "const" doesn't mean "constant"; it really means "read-only".

The language *could* have stated that a const object with an
initializer that's a constant expression is a constant <OT>as C++
does</OT>, but it doesn't.

Note that this is legal (at block scope):

const int r = rand();

r obviously isn't constant (in the sense of being evaluable at compile
time), but it is "const", i.e., read-only, in the sense that you're
not allowed to modify it:

r = 42; /* constraint violation */

.... at least not directly:

*(int*)&r = 42; /* undefined behavior, not a constraint violation */

bart.c points out the enum trick, which I find preferable to using
a macro:

enum { eodSize = 8192 };

One drawback is that enumeration constants can only be of type int.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Poster Matt
Guest
Posts: n/a
 
      06-15-2010
bart.c wrote:
>
> "Poster Matt" <postermatt@no_spam_for_me.org> wrote in message
> news:bbQRn.22678$(E-Mail Removed)2...
>> Hi,
>>
>> The following code results in the following error with GCC (v.4.3.2):
>>
>> const int eodSize = 8192;
>> char eodContents[eodSize];
>>
>> error: variably modified 'eodContents' at file scope
>>
>>
>> It is, of course, easily solved by ditching the 'const int' and using
>> a #define instead like this:
>>
>> #define eodSize 8192
>> char eodContents[eodSize];
>>
>> I would understand the error if "const int eodSize = 8192;" was not a
>> constant but since it is a constant why does the compiler not allow it?

>
> const doesn't mean what you think.
>
> Just use:
>
> enum {eodSize=8192};
>
> as #define has it's own problems.


const means exactly what I think. It specifies that the value of the variable
will not be changed.

What on earth is wrong with using #define as I have used it above? It seems to
me to be perfectly fine. Note: "#define eodSize 8192" is not being defined
inside a function.

But I was looking for an explanation...

Cheers.
 
Reply With Quote
 
Poster Matt
Guest
Posts: n/a
 
      06-15-2010
Ben Pfaff wrote:
> Poster Matt <postermatt@no_spam_for_me.org> writes:
>
>> The following code results in the following error with GCC (v.4.3.2):
>>
>> const int eodSize = 8192;
>> char eodContents[eodSize];

>
> This is in the FAQ:
>
> 11.8: I don't understand why I can't use const values in initializers
> and array dimensions, as in
>
> const int n = 5;
> int a[n];
>
> A: The const qualifier really means "read-only"; an object so
> qualified is a run-time object which cannot (normally) be
> assigned to. The value of a const-qualified object is therefore
> *not* a constant expression in the full sense of the term. (C
> is unlike C++ in this regard.) When you need a true compile-
> time constant, use a preprocessor #define (or perhaps an enum).
>
> References: ISO Sec. 6.4; H&S Secs. 7.11.2,7.11.3 pp. 226-7.


Oops, sorry. Thanks for the info.
 
Reply With Quote
 
Poster Matt
Guest
Posts: n/a
 
      06-15-2010
Poster Matt wrote:
> bart.c wrote:
>>
>> "Poster Matt" <postermatt@no_spam_for_me.org> wrote in message
>> news:bbQRn.22678$(E-Mail Removed)2...
>>> Hi,
>>>
>>> The following code results in the following error with GCC (v.4.3.2):
>>>
>>> const int eodSize = 8192;
>>> char eodContents[eodSize];
>>>
>>> error: variably modified 'eodContents' at file scope
>>>
>>>
>>> It is, of course, easily solved by ditching the 'const int' and using
>>> a #define instead like this:
>>>
>>> #define eodSize 8192
>>> char eodContents[eodSize];
>>>
>>> I would understand the error if "const int eodSize = 8192;" was not a
>>> constant but since it is a constant why does the compiler not allow it?

>>
>> const doesn't mean what you think.
>>
>> Just use:
>>
>> enum {eodSize=8192};
>>
>> as #define has it's own problems.

>
> const means exactly what I think. It specifies that the value of the
> variable will not be changed.


Ok I was a bit quick with my reply - it doesn't mean exactly what I thought it
meant. <Head held in shame smiley>

K&R is a bit unclear about it to say the least - see my reply to Keith.

Cheers.
 
Reply With Quote
 
bart.c
Guest
Posts: n/a
 
      06-15-2010
"Poster Matt" <postermatt@no_spam_for_me.org> wrote in message
news:hEQRn.17414$US6.2344@hurricane...
> bart.c wrote:


>> "Poster Matt" <postermatt@no_spam_for_me.org> wrote in message
>> news:bbQRn.22678$(E-Mail Removed)2...


>>> const int eodSize = 8192;
>>> char eodContents[eodSize];
>>>
>>> error: variably modified 'eodContents' at file scope


>>> It is, of course, easily solved by ditching the 'const int' and using a
>>> #define instead like this:
>>>
>>> #define eodSize 8192
>>> char eodContents[eodSize];
>>>
>>> I would understand the error if "const int eodSize = 8192;" was not a
>>> constant but since it is a constant why does the compiler not allow it?

>>
>> const doesn't mean what you think.
>>
>> Just use:
>>
>> enum {eodSize=8192};
>>
>> as #define has it's own problems.

>
> const means exactly what I think. It specifies that the value of the
> variable will not be changed.


Most people who ask that assume that 'const' defines an actual constant (not
a memory location containing a value). Your wording ("...but since it is a
constant ...") suggested you thought the same.

> What on earth is wrong with using #define as I have used it above? It
> seems to me to be perfectly fine. Note: "#define eodSize 8192" is not
> being defined inside a function.


Yes, #define names have too wide a visibility for many cases; enum doesn't
have that problem and comes closest to what some people expect 'const' to
do, at least for int values. But you seem to know all the answers already..

> But I was looking for an explanation...


I expected someone else to provide that; I just gave an alternative
workaround..

--
Bartc

>
> Cheers.


 
Reply With Quote
 
Poster Matt
Guest
Posts: n/a
 
      06-15-2010
Keith Thompson wrote:
> Poster Matt <postermatt@no_spam_for_me.org> writes:
>> The following code results in the following error with GCC (v.4.3.2):
>>
>> const int eodSize = 8192;
>> char eodContents[eodSize];
>>
>> error: variably modified 'eodContents' at file scope
>>
>>
>> It is, of course, easily solved by ditching the 'const int' and using
>> a #define instead like this:
>>
>> #define eodSize 8192
>> char eodContents[eodSize];
>>
>> I would understand the error if "const int eodSize = 8192;" was not a
>> constant but since it is a constant why does the compiler not allow
>> it?

>
> Because "const" doesn't mean "constant"; it really means "read-only".
>
> The language *could* have stated that a const object with an
> initializer that's a constant expression is a constant <OT>as C++
> does</OT>, but it doesn't.


K&R is somewhat misleading then when it says:

"The qualifier const can be applied to any variable to specify that its value
will not be changed."

> Note that this is legal (at block scope):
>
> const int r = rand();
>
> r obviously isn't constant (in the sense of being evaluable at compile
> time), but it is "const", i.e., read-only, in the sense that you're
> not allowed to modify it:
>
> r = 42; /* constraint violation */


Ok got it.


> ... at least not directly:
>
> *(int*)&r = 42; /* undefined behavior, not a constraint violation */
>
> bart.c points out the enum trick, which I find preferable to using
> a macro:
>
> enum { eodSize = 8192 };
>
> One drawback is that enumeration constants can only be of type int.


Why is it in any way preferable to use this 'enum trick' to a #define?

Thanks Keith.
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      06-15-2010
On 2010-06-15, Poster Matt <postermatt@no_spam_for_me.org> wrote:
> I would understand the error if "const int eodSize = 8192;" was not a constant
> but since it is a constant why does the compiler not allow it?


It is not a constant.

An object you can't write to is not a constant. You still have to look up
the value at runtime (since nothing prevents some other aspect of the system
from modifying the "constant" value).

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Variably modified arrays James Kuyper C Programming 1 01-24-2013 04:47 PM
Re: Variably modified arrays Shao Miller C Programming 0 01-24-2013 04:17 PM
error: parameter may not have variably modified type Helmut Jarausch C++ 20 10-22-2009 11:09 AM
Variably modified struct/union and C99 Foo C Programming 2 10-08-2009 11:54 AM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments