Will wonders never cease? Kenny M has a real C question...

  • Thread starter Kenny McCormack
  • Start date
K

Kenny McCormack

Working with some old code - that has compiled (and run) successfully on
dozens of Unix systems over the years - I came across the following line
of code.

At file scope level:

FILE *outfile = stdout;

(After including stdio, of course). Today, gcc running on a Unix system
says that the initializer is not a constant. And, digging through
/usr/include/stdio.h shows that that does seem to be the case.

So, the question is: Does the standard have anything to say about this?
Now, obviously, whenever anyone ever asks that question, the answer is
"No", since the standard guarantees virtually nothing useful. But,
still I'm curious to see what flames, er, I mean, discussion I can
unleash on this subject.

Why should it be? Why shouldn't it be?

--
Is God willing to prevent evil, but not able? Then he is not omnipotent.
Is he able, but not willing? Then he is malevolent.
Is he both able and willing? Then whence cometh evil?
Is he neither able nor willing? Then why call him God?
~ Epicurus
 
F

Francois Grieu

Working with some old code - that has compiled (and run) successfully on
dozens of Unix systems over the years - I came across the following line
of code.

At file scope level:

FILE *outfile = stdout;

(After including stdio, of course). Today, gcc running on a Unix system
says that the initializer is not a constant. And, digging through
/usr/include/stdio.h shows that that does seem to be the case.

So, the question is: Does the standard have anything to say about this?

Yes. The above code is not required to compile because 7.19.1#3
defines stdout as an expression, when 6.7.8#4 says that "All the
expressions in an initializer for an object that has static storage
duration shall be *constant* expressions or string literals."

Francois Grieu
 
S

Shao Miller

Kenny said:
Working with some old code - that has compiled (and run) successfully on
dozens of Unix systems over the years - I came across the following line
of code.

At file scope level:

FILE *outfile = stdout;

(After including stdio, of course). Today, gcc running on a Unix system
says that the initializer is not a constant. And, digging through
/usr/include/stdio.h shows that that does seem to be the case.

So, the question is: Does the standard have anything to say about this?
Now, obviously, whenever anyone ever asks that question, the answer is
"No", since the standard guarantees virtually nothing useful. But,
still I'm curious to see what flames, er, I mean, discussion I can
unleash on this subject.

Why should it be? Why shouldn't it be?

You might be interested in "address constant" in 'n1256.pdf', section
6.6, point 9.

I think that a way to perceive the constraint is that static object
initialization occurs at program startup (I'm pretty sure that in most
cases, the initialization values are simply loaded from the program
image or during such loading, just the same as the object code that
constitutes the program). I think that the constraint for requiring
constant values helps to alleviate data dependencies such as "we need to
know this before we can know that". So basically constants at
translation-time are a fairly simple constraint to make to facilitate this.

Does that make any sense to you?
 

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

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,114
Members
46,702
Latest member
VernitaGow

Latest Threads

Top