const in place of #define

A

Amandil

Hi all. Now I'm getting back on topic ;)

I tried to use a const instead of a #define, and to my surprise, I
couldn't. Here is the code snippet:

const int CONST = 10;
char str[CONST];

The error I got (with gcc 3.4.2 on mingw) was:
assemble.c:25: error: variable-size type declared outside of any
function

Checking the C standard, str[] must be declared (at file scope) with
an integer constant expression. It seems that my declaration of CONST
does not fall into that category, though, of course, had I used a
#define CONST 10
I would have no trouble. It took me a couple of minutes to get the
idea that a const variable is not a constant expression. Is that the
case? Just want to make sure I got it right.

-- Marty Amandil (peeping through the periscope: is the coast clear?)
 
I

Ian Collins

Amandil said:
Hi all. Now I'm getting back on topic ;)

I tried to use a const instead of a #define, and to my surprise, I
couldn't. Here is the code snippet:

const int CONST = 10;
char str[CONST];

The error I got (with gcc 3.4.2 on mingw) was:
assemble.c:25: error: variable-size type declared outside of any
function

Checking the C standard, str[] must be declared (at file scope) with
an integer constant expression. It seems that my declaration of CONST
does not fall into that category, though, of course, had I used a
#define CONST 10
I would have no trouble. It took me a couple of minutes to get the
idea that a const variable is not a constant expression. Is that the
case? Just want to make sure I got it right.
Yes, unfortunately const is badly broken in C.
 
A

Andrey Tarasevich

Amandil said:
...
Checking the C standard, str[] must be declared (at file scope) with
an integer constant expression. It seems that my declaration of CONST
does not fall into that category, though, of course, had I used a
#define CONST 10
I would have no trouble. It took me a couple of minutes to get the
idea that a const variable is not a constant expression. Is that the
case? Just want to make sure I got it right.
...

Yes, you got it right. In C a 'const int' object does not form an Integral
Constant Expression (in C++, for example, it does). Therefore, any array
declaration with 'const int' size specifier is actually a VLA declaration in C99
(and simply illegal in C89/90), which has to obey the restrictions imposed on VLAs.
 
B

Ben Pfaff

Amandil said:
I tried to use a const instead of a #define, and to my surprise, I
couldn't. Here is the code snippet:

const int CONST = 10;
char str[CONST];

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

Amandil

Amandil said:
I tried to use a const instead of a #define, and to my surprise, I
couldn't. Here is the code snippet:
const int CONST = 10;
char str[CONST];

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.

You're right, as usual, of course. I read the FAQ a while back, when I
was a real newbie, but I didn't remember to check if my question was
there before posting. I guess that makes two bloopers in one day :( I
guess I'll just sit quiet a bit.

-- Marty Amandil (STILL hiding in my bunker...)
 
J

jacob navia

Amandil said:
Hi all. Now I'm getting back on topic ;)

I tried to use a const instead of a #define, and to my surprise, I
couldn't. Here is the code snippet:

const int CONST = 10;
char str[CONST];

The error I got (with gcc 3.4.2 on mingw) was:
assemble.c:25: error: variable-size type declared outside of any
function

Checking the C standard, str[] must be declared (at file scope) with
an integer constant expression. It seems that my declaration of CONST
does not fall into that category, though, of course, had I used a
#define CONST 10
I would have no trouble. It took me a couple of minutes to get the
idea that a const variable is not a constant expression. Is that the
case? Just want to make sure I got it right.

-- Marty Amandil (peeping through the periscope: is the coast clear?)

const is not const in standard C.

The lcc-win compilers implements this as an extension, and will accept
your code since (in my opinion) it should work.
 
I

Ian Collins

jacob said:
The lcc-win compilers implements this as an extension, and will accept
your code since (in my opinion) it should work.
<OT>
How does it differentiate between an array and a VLA in this case?
</OT>
 
R

Randy Howard

Amandil said:
Hi all. Now I'm getting back on topic ;)

I tried to use a const instead of a #define, and to my surprise, I
couldn't. Here is the code snippet:

const int CONST = 10;
char str[CONST];

The error I got (with gcc 3.4.2 on mingw) was:
assemble.c:25: error: variable-size type declared outside of any
function

Checking the C standard, str[] must be declared (at file scope) with
an integer constant expression. It seems that my declaration of CONST
does not fall into that category, though, of course, had I used a
#define CONST 10
I would have no trouble. It took me a couple of minutes to get the
idea that a const variable is not a constant expression. Is that the
case? Just want to make sure I got it right.

-- Marty Amandil (peeping through the periscope: is the coast clear?)

const is not const in standard C.

The lcc-win compilers implements this as an extension, and will accept
your code since (in my opinion) it should work.

Does it accept it when asking the compiler to compile only conforming
code? Is such an option even available?
 
J

jacob navia

Ian said:
<OT>
How does it differentiate between an array and a VLA in this case?
</OT>

The constant must be defined as a constant integer, and at the global
level for this optimization to work.

It should be also possible to do:

int fn(void)
{
const int siz = 64;
int tab[siz];

}

and that should work but I think I do a VLA in this case.
 
C

christian.bau

The lcc-win compilers implements this as an extension, and will accept
your code since (in my opinion) it should work.

Does anyone give a monkey's toss about your compiler?
 
I

Ian Collins

jacob said:
The constant must be defined as a constant integer, and at the global
level for this optimization to work.
Both the constant and the array?
It should be also possible to do:

int fn(void)
{
const int siz = 64;
int tab[siz];

}

and that should work but I think I do a VLA in this case.
I hope so...
 
C

CBFalconer

jacob said:
Amandil said:
I tried to use a const instead of a #define, and to my surprise, I
couldn't. Here is the code snippet:

const int CONST = 10;
char str[CONST];

The error I got (with gcc 3.4.2 on mingw) was:
assemble.c:25: error: variable-size type declared outside of any
function

Checking the C standard, str[] must be declared (at file scope) with
an integer constant expression. It seems that my declaration of CONST
does not fall into that category, though, of course, had I used a
#define CONST 10
I would have no trouble. It took me a couple of minutes to get the
idea that a const variable is not a constant expression. Is that the
case? Just want to make sure I got it right.

const is not const in standard C.

The lcc-win compilers implements this as an extension, and will accept
your code since (in my opinion) it should work.

Does your standard-C flag make it work as it should, i.e. as the C
standard describes? If not, your compiler is not really usable.
If so, your compiler should not be used without that standard-C
flag.

Believe it or not, many users of this newsgroup understand how
const works.
 
J

jacob navia

Ian said:
jacob said:
The constant must be defined as a constant integer, and at the global
level for this optimization to work.
Both the constant and the array?
It should be also possible to do:

int fn(void)
{
const int siz = 64;
int tab[siz];

}

and that should work but I think I do a VLA in this case.
I hope so...

In any case you will never be able to know.
I *can* calculate the size of the "tab" variable at
compile time since a const int can't change its value
specifically in the code above. Of course it is more difficult when I
can't prove that siz never changes between its definition and its usage
as a table size index.

Also, since I do constant propagation just acode like this

int fn(void)
{
int m = 67;
int tab[m];
}

can be optimized to eliminate the VLA since I can prove that
there is no way to modify m before is used as an array size.
 
D

David Thompson

jacob navia wrote:

Does your standard-C flag make it work as it should, i.e. as the C
standard describes? If not, your compiler is not really usable.
If so, your compiler should not be used without that standard-C
flag.
This extension doesn't change the meaning of any otherwise correct
(clc-conforming?) program; at most it drops a diagnostic that might be
required, and even that's arguable under 6.6p10.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top