public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* "gcc" complains about a constant being non constant...(sorry for dup)
@ 2009-07-06  5:38 Linda A. Walsh
  2009-07-06  9:47 ` Andrew Haley
  2009-07-08 13:47 ` Kalle Olavi Niemitalo
  0 siblings, 2 replies; 4+ messages in thread
From: Linda A. Walsh @ 2009-07-06  5:38 UTC (permalink / raw)
  To: gcc-help

(Sorry if this is a dup, somehow my from address got mangled with the from
addr having this message being sent from my system's MAILER DAEMON!  Weird.)

I have a "proglet", included below (twice, in fact! :-), first with line numbering for referring to the error messages, and a 2nd time without line numbers to allow for easy cut & pasting to try it in your local environment.

The error output appears to indicate a problem with compile-time constant
folding.

gcc --version shows:
gcc (SUSE Linux) 4.3.2 [gcc-4_3-branch revision 141291]

Compile time output:
ct.c:10: error: initializer element is not constant
ct.c:11: error: initializer element is not constant

Here's the proglet w/line numbering for reference:
------
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <strings.h>
 4  5 typedef const char * String;
 6  7 static const String days [] ={"Sun", "Mon", "Tue", "Wed", "Thu" , "Fri", "Sat"};
 8 static const int sizeof_days            = sizeof(days);
 9 static const int sizeof_String          = sizeof(String);
10 static const int numdays                = sizeof_days / sizeof_String;
11 static const int last_index=numdays-1;
12 13 int main (){
14         printf("numdays=%d, lastidx=%d\n",numdays,last_index);
15 }
--------

if, in line 8, sizeof(days) is a constant,  AND in line 9, sizeof(String) is
a constant, then how can their division (in line 10) NOT be a constant?
Similarly, how would a "constant value" minus one, (as in line 11) also
not be a constant?

This seems too obvious for it not to have been caught before.  Is this a
designed feature deficit, or did I really stumble upon a bug in such
mature code that it wouldn't have been reported and fixed years ago?
:-)


I've also included same program, below, without line numbers (for easy
cut & paste to a test file).

Am I missing something about constant folding, or is it this some unlikely
fluke?


Thanks,
Linda


---------------------
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

typedef const char * String;

static const String days [] ={"Sun", "Mon", "Tue", "Wed", "Thu" , "Fri", "Sat"};
static const int sizeof_days            = sizeof(days);
static const int sizeof_String          = sizeof(String);
static const int numdays                = sizeof_days / sizeof_String;
static const int last_index=numdays-1;

int main (){
       printf("numdays=%d, lastidx=%d\n",numdays,last_index);
}
-----------------------------

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: "gcc" complains about a constant being non constant...(sorry  for dup)
  2009-07-06  5:38 "gcc" complains about a constant being non constant...(sorry for dup) Linda A. Walsh
@ 2009-07-06  9:47 ` Andrew Haley
  2009-07-08 13:47 ` Kalle Olavi Niemitalo
  1 sibling, 0 replies; 4+ messages in thread
From: Andrew Haley @ 2009-07-06  9:47 UTC (permalink / raw)
  To: Linda A. Walsh; +Cc: gcc-help

Linda A. Walsh wrote:
> (Sorry if this is a dup, somehow my from address got mangled with the from
> addr having this message being sent from my system's MAILER DAEMON! 
> Weird.)
> 
> I have a "proglet", included below (twice, in fact! :-), first with line
> numbering for referring to the error messages, and a 2nd time without
> line numbers to allow for easy cut & pasting to try it in your local
> environment.
> 
> The error output appears to indicate a problem with compile-time constant
> folding.
> 
> gcc --version shows:
> gcc (SUSE Linux) 4.3.2 [gcc-4_3-branch revision 141291]
> 
> Compile time output:
> ct.c:10: error: initializer element is not constant
> ct.c:11: error: initializer element is not constant
> 
> Here's the proglet w/line numbering for reference:
> ------
> 1 #include <stdio.h>
> 2 #include <stdlib.h>
> 3 #include <strings.h>
> 4  5 typedef const char * String;
> 6  7 static const String days [] ={"Sun", "Mon", "Tue", "Wed", "Thu" ,
> "Fri", "Sat"};
> 8 static const int sizeof_days            = sizeof(days);
> 9 static const int sizeof_String          = sizeof(String);
> 10 static const int numdays                = sizeof_days / sizeof_String;
> 11 static const int last_index=numdays-1;
> 12 13 int main (){
> 14         printf("numdays=%d, lastidx=%d\n",numdays,last_index);
> 15 }
> --------
> 
> if, in line 8, sizeof(days) is a constant,  AND in line 9,
> sizeof(String) is
> a constant, then how can their division (in line 10) NOT be a constant?

Simple: sizeof(days) is a constant, but sizeof_days is not.  Reason: because
the C language standard says so.  A const variable is not a constant.  It'd be
nice if it were.

Andrew.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: "gcc" complains about a constant being non constant...(sorry for dup)
  2009-07-06  5:38 "gcc" complains about a constant being non constant...(sorry for dup) Linda A. Walsh
  2009-07-06  9:47 ` Andrew Haley
@ 2009-07-08 13:47 ` Kalle Olavi Niemitalo
  1 sibling, 0 replies; 4+ messages in thread
From: Kalle Olavi Niemitalo @ 2009-07-08 13:47 UTC (permalink / raw)
  To: Linda A. Walsh; +Cc: gcc-help

"Linda A. Walsh" <gcc@tlinx.org> writes:

> Compile time output:
> ct.c:10: error: initializer element is not constant
> ct.c:11: error: initializer element is not constant

If you compile the program as C++ rather than C, then these
errors go away, and GCC 4.3.1 propagates the constants to the
printf call:

	movl	$6, 8(%esp)
	movl	$7, 4(%esp)
	movl	$.LC0, (%esp)
	call	printf

This is one difference in the meaning of const between C and C++.
Another difference is that in C++, const variables defined
outside of functions are static by default, although one can
override that with extern.  That doesn't matter in your program
because the variables were static already.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: "gcc" complains about a constant being non constant...(sorry  for dup)
@ 2009-07-06 10:56 Bill McEnaney
  0 siblings, 0 replies; 4+ messages in thread
From: Bill McEnaney @ 2009-07-06 10:56 UTC (permalink / raw)
  To: Andrew Haley, Linda A. Walsh, gcc-help

Maybe we need to distinguish between a const variable and a literal.

> Linda A. Walsh wrote:
> > (Sorry if this is a dup, somehow my from address got mangled with
the from
> > addr having this message being sent from my system's MAILER DAEMON! 
> > Weird.)
> > 
> > I have a "proglet", included below (twice, in fact! :-), first with line
> > numbering for referring to the error messages, and a 2nd time without
> > line numbers to allow for easy cut & pasting to try it in your local
> > environment.
> > 
> > The error output appears to indicate a problem with compile-time
constant
> > folding.
> > 
> > gcc --version shows:
> > gcc (SUSE Linux) 4.3.2 [gcc-4_3-branch revision 141291]
> > 
> > Compile time output:
> > ct.c:10: error: initializer element is not constant
> > ct.c:11: error: initializer element is not constant
> > 
> > Here's the proglet w/line numbering for reference:
> > ------
> > 1 #include <stdio.h>
> > 2 #include <stdlib.h>
> > 3 #include <strings.h>
> > 4  5 typedef const char * String;
> > 6  7 static const String days [] ={"Sun", "Mon", "Tue", "Wed", "Thu" ,
> > "Fri", "Sat"};
> > 8 static const int sizeof_days            = sizeof(days);
> > 9 static const int sizeof_String          = sizeof(String);
> > 10 static const int numdays                = sizeof_days /
sizeof_String;
> > 11 static const int last_index=numdays-1;
> > 12 13 int main (){
> > 14         printf("numdays=%d, lastidx=%d\n",numdays,last_index);
> > 15 }
> > --------
> > 
> > if, in line 8, sizeof(days) is a constant,  AND in line 9,
> > sizeof(String) is
> > a constant, then how can their division (in line 10) NOT be a constant?
> 
> Simple: sizeof(days) is a constant, but sizeof_days is not.  Reason:
because
> the C language standard says so.  A const variable is not a constant.
 It'd be
> nice if it were.
> 
> Andrew.
> 
> 

________________________________________________________________
Please visit a saintly hero:
http://www.jakemoore.org

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-07-08 13:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-07-06  5:38 "gcc" complains about a constant being non constant...(sorry for dup) Linda A. Walsh
2009-07-06  9:47 ` Andrew Haley
2009-07-08 13:47 ` Kalle Olavi Niemitalo
2009-07-06 10:56 Bill McEnaney

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).