Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #109616
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: error: initializer element is not constant| |
| Date | 2017-05-09 11:57 -0700 |
| Organization | None to speak of |
| Message-ID | <lny3u597uc.fsf@kst-u.example.com> (permalink) |
| References | (8 earlier) <oessve$f4k$1@gioia.aioe.org> <zKmQA.84548$jo6.54626@fx33.iad> <oestp7$gkd$1@gioia.aioe.org> <3dv3hc1ueu2en2hl1oprs3ui6j6bdoqmf1@4ax.com> <oet0bm$l6s$2@gioia.aioe.org> |
jak <please@nospam.tnx> writes:
>>> Ok. Then return to the first question. What information is missing from
>>> the compiler to solve this?
>>>
>>> char *Date = "01-01-80";
>>> char *Phrase[] =
>>> {
>>> NULL, /*1*/
>>> Date, /*2*/
>>> NULL, /*3*/
>>> NULL /*4*/
>>> };
>>>
>>> ...and why does it solve this?
>>>
>>> char *DescDate = "Date: ";
>>> char *Date = "01-01-80";
>>> char *CrLf = "\r\n";
>>> char *Text = "Dear friend...";
>>>
>>> char **Phrase[] =
>>> {
>>> &DescDate, &Date, &CrLf,
>>> &Text, &CrLf
>>> };
>>
>> Possibly because the address of a static variable is a compile time
>> const (note const, not constant) while the value of a static variable
>> is not.
>>
> You would be right if the compiler information was not the same in both
> cases. Also at runtime you can change any value in * Phrase [] and in **
> Phrase [], too, so why const? I'm afraid it's just a syntactic
> imposition. :(
The language standard has to have some consistent definition of what is
or is not a constant expression. It can't just say "anything whose
value the compiler can figure out is a constant expression"; that would
imply that the criteria could change depending on how clever the
compiler happens to be.
In general, the standard doesn't require compilers to perform data flow
analysis.
Let's take a simple example:
char *foo = "hello";
char *bar = foo;
In this case, sure, a compiler *could* perform an analysis that
determines that the value of foo, when it's used in an initializer
for the static object bar, cannot have changed from its initial
value. But although the compiler must generate code that will
cause foo to be properly initialized before entry to main(), it's
not required to remember what that initial value is. It would have
to do so to be able to implement those declarations.
On the other hand, given:
char foo[] = "hello";
char *bar = foo;
the expression `foo` in the initializer yields the address of a
static object, not the (current) value of that object. The compiler
has to be able to generate code that refers to the address of a
static object anyway, so there's no extra burden in requiring it
to treat such an address as a compile-time constant expression.
Given modern optimizing compiler technology, it might be possible to
loosen the rules for constant expressions by requiring *some* data
flow analysis. But (a) that would still impose an extra burden
on small non-optimizing compilers, and (b) defining the exact
new rules in Standardese would be a difficult task (and reaching
agreement on just what those rules should be would be even harder).
A small tweak to the rules, to cover just a case like this one,
might partially avoid those problems, but it's unlikely that it
would be considered worthwhile. The committee isn't going to change
the language to fix a problem that already has a workaround.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
error: initializer element is not constant| jak <please@nospam.tnx> - 2017-05-08 09:55 +0200
Re: error: initializer element is not constant| jak <please@nospam.tnx> - 2017-05-08 10:05 +0200
Re: error: initializer element is not constant| Noob <root@127.0.0.1> - 2017-05-08 10:38 +0200
Re: error: initializer element is not constant| jak <please@nospam.tnx> - 2017-05-08 11:55 +0200
Re: error: initializer element is not constant| bartc <bc@freeuk.com> - 2017-05-08 11:01 +0100
Re: error: initializer element is not constant| jak <please@nospam.tnx> - 2017-05-08 12:18 +0200
Re: error: initializer element is not constant| bartc <bc@freeuk.com> - 2017-05-08 12:02 +0100
Re: error: initializer element is not constant| Keith Thompson <kst-u@mib.org> - 2017-05-08 09:00 -0700
Re: error: initializer element is not constant| bartc <bc@freeuk.com> - 2017-05-08 17:50 +0100
Re: error: initializer element is not constant| Keith Thompson <kst-u@mib.org> - 2017-05-08 11:37 -0700
Re: error: initializer element is not constant| bartc <bc@freeuk.com> - 2017-05-08 19:49 +0100
Re: error: initializer element is not constant| Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-08 20:13 +0100
Re: error: initializer element is not constant| Keith Thompson <kst-u@mib.org> - 2017-05-08 12:44 -0700
Re: error: initializer element is not constant| scott@slp53.sl.home (Scott Lurndal) - 2017-05-08 20:24 +0000
Re: error: initializer element is not constant| jak <please@nospam.tnx> - 2017-05-09 09:54 +0200
Re: error: initializer element is not constant| jak <please@nospam.tnx> - 2017-05-09 10:15 +0200
Re: error: initializer element is not constant| GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-05-09 12:12 +0200
Re: error: initializer element is not constant| bartc <bc@freeuk.com> - 2017-05-09 11:47 +0100
Re: error: initializer element is not constant| jak <please@nospam.tnx> - 2017-05-09 19:06 +0200
Re: error: initializer element is not constant| scott@slp53.sl.home (Scott Lurndal) - 2017-05-09 17:10 +0000
Re: error: initializer element is not constant| jak <please@nospam.tnx> - 2017-05-09 19:20 +0200
Re: error: initializer element is not constant| Barry Schwarz <schwarzb@dqel.com> - 2017-05-09 10:35 -0700
Re: error: initializer element is not constant| jak <please@nospam.tnx> - 2017-05-09 20:04 +0200
Re: error: initializer element is not constant| Keith Thompson <kst-u@mib.org> - 2017-05-09 11:57 -0700
Re: error: initializer element is not constant| supercat@casperkitty.com - 2017-05-09 12:50 -0700
Re: error: initializer element is not constant| Keith Thompson <kst-u@mib.org> - 2017-05-09 12:54 -0700
Re: error: initializer element is not constant| bartc <bc@freeuk.com> - 2017-05-09 20:15 +0100
Re: error: initializer element is not constant| "James R. Kuyper" <jameskuyper@verizon.net> - 2017-05-09 14:13 -0400
Re: error: initializer element is not constant| Keith Thompson <kst-u@mib.org> - 2017-05-09 11:41 -0700
Re: error: initializer element is not constant| Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-09 12:16 +0100
Re: error: initializer element is not constant| Keith Thompson <kst-u@mib.org> - 2017-05-09 08:11 -0700
Re: error: initializer element is not constant| Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-08 12:12 +0100
Re: error: initializer element is not constant| bartc <bc@freeuk.com> - 2017-05-08 13:45 +0100
Re: error: initializer element is not constant| Keith Thompson <kst-u@mib.org> - 2017-05-08 09:04 -0700
Re: error: initializer element is not constant| Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-08 19:53 +0100
Re: error: initializer element is not constant| bartc <bc@freeuk.com> - 2017-05-08 20:44 +0100
Re: error: initializer element is not constant| Noob <root@127.0.0.1> - 2017-05-09 09:27 +0200
Re: error: initializer element is not constant| Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-09 07:10 -0700
Re: error: initializer element is not constant| Keith Thompson <kst-u@mib.org> - 2017-05-08 09:30 -0700
Re: error: initializer element is not constant| John Bode <jfbode1029@gmail.com> - 2017-05-08 08:05 -0700
csiph-web