stdout not open?

R

Randy Yates

Eric Sosman said:
That's when the program is compiled in your complicated
build configuration, right? What happens if you just fire
up gcc by hand, omitting all the jiggery-pokery?

"jiggery-pokery"???

Yep, you're right - it works without either jiggery or pokery.
I imagine
you'll get either

- It runs fine, suggesting that there's something awry
in your build environment, or

- It also crashes, suggesting that there's something
wrong with your gcc installation.

The only error I can spot in the C code is that you're
missing a comma after "Hello" ;-)

Oh, sorry! I'll correct that ASAP... :)

Is this the facility in Mountain View on Shoreline Blvd? If so, I
used to work about a mile from there (GTE Government Systems, which
is now General Dynamics, I believe).
 
R

Randy Yates

Aha!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

I think I found it.

If I build with

gcc -c -o test.o test.c
gcc -o tes test.o

it works. If I build with

gcc -c -fpack-struct -o test.o test.c
gcc -o tes test.o

it dumps.

Now, is this a compiler bug or my bug or what?


--RY
 
B

Ben Pfaff

Randy Yates said:
If I build with

gcc -c -fpack-struct -o test.o test.c
gcc -o tes test.o

it dumps.

If you read what the GCC manual says about -fpack-struct,
you would know.
 
G

Giorgos Keramidas

Also, you mentioned that you're using gcc, and I seem to recall
reading somewhere that gcc likes to modify the system-provided include
files to suit its own purposes; I don't know whether this is true for
the particular gcc version you're using (and maybe what I read was
just an ill-informed rumor) -- anyhow, there's plenty of room for
glitches.

Gcc shouldn't overwrite system headers if installed with a --prefix of
/usr/local or /opt/gnu. I'm using both cc and gcc on several Solaris 8
installations without problems.

Overwriting of system headers is probably a side-effect of building gcc
with --prefix=usr, but you're right that we don't know if this is true
for the particular installation.

HTH,
Giorgos
 
C

CBFalconer

Randy said:
Christopher, Eric, et al.,

I apologize for my outburst. It was probably a bad idea to
post at 3am in the morning after awaking from a bad dream!

Fine, accepted. Now please learn how to post. Top posting is
frowned upon here. Your answer belongs after, or intermixed with,
the portion that you quote, after snipping anything not germane to
your reply.
 
C

CBFalconer

Randy said:
If I build with

gcc -c -o test.o test.c
gcc -o tes test.o

it works. If I build with

gcc -c -fpack-struct -o test.o test.c
gcc -o tes test.o

it dumps.

Now, is this a compiler bug or my bug or what?

.... snip 80 odd useless lines ...

Please don't toppost. Do snip.

Yours. You are redefining the layout of a FILE structure, whatever
that may be. The library code doesn't know about it.
 
C

Chris Barts

Aha!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Bah. Top-posting is rather annoying. No, scratch that, it's very annoying.
Please, just post like everyone else in this newsgroup.

Second, I snipped most of this because the context isn't essential for
understanding this post. You should probably have snipped a bit, yourself.
I think I found it.

If I build with

gcc -c -o test.o test.c
gcc -o tes test.o

it works.

Why the two-step? I can invoke gcc like so:

gcc -o foo foo.c

and foo will work as expected.

(I'm not saying it's wrong, it's simply more typing.)
If I build with

gcc -c -fpack-struct -o test.o test.c
gcc -o tes test.o

it dumps.

Which is odd. As near as I can figure, FILE* points to a struct which
really should not be packed on your machine.

I could be very wrong. But that's as far as you'll probably get in
comp.lang.c.
Now, is this a compiler bug or my bug or what?

I'm leaning towards compiler bug, simply because your code has no structs.
Post in a gcc-specific group and see what they have to say once you have
looked over the section of the gcc documentation that lists known bugs.
 
C

Chris Torek

Aha! ... [the crash happens when compiling with "gcc -fpack-struct"
and not when compiling with gcc without "-fpack-struct"].

Which is odd. As near as I can figure, FILE* points to a struct which
really should not be packed on your machine.

Yes, but this could be precisely the problem.

The C standard tells us only that "FILE" is an object type defined
in <stdio.h>, and "stdin", "stdout", and "stderr" are macros that
expand to expressions that have type "pointer to FILE".

On traditional systems, though, this object type is indeed a
structure type, defined using something along the lines:

typedef struct __sFILE FILE;
struct __sFILE {
various integers and pointers and other fields;
};

Now, if -- as eventually became the case for BSD/OS -- the three
macros for stdin, stdout, and stderr are, say:

extern FILE __sstdin, __sstdout, __sstderr;
#define stdin (&__sstdin)
#define stdout (&__sstdout)
#define stdout (&__sstderr)

then "packing" the __sFILE structure might cause *some* problems,
but not the one encountered here, because (on these traditional
systems) the three separate "extern' variables are resolved correctly
by the linker, and the offsets of structure members appear nowhere
in the sample code (which I snipped, but it does not use the getc()
and putc() macros).

On the other hand, suppose that the three macros are defined like
this:

extern FILE __sstd[3]; /* FILEs for fd's 0 through 2 inclusive */
#define stdin (&__sstd[0])
#define stdout (&__sstd[1])
#define stderr (&__sstd[2])

(as they were originally in BSD/OS, because I did something just
like this for 4.4BSD). Now the size of the struct *does* matter,
because stdout's value happens to be the same byte-level pointer
(on these traditional machines) as ((char *)stdin + sizeof(FILE)).
If you pack the structure, and if this in turn changes the result
of sizeof(FILE), the first value passed to fprintf(stdout, "...\n")
changes bit patterns and is no longer the correct value.
I'm leaning towards compiler bug, simply because your code has no structs.

No directly-visible ones, perhaps, but I would bet money that the
above is the correct explanation for the problem. :)
 
C

Chris Barts

Aha! ... [the crash happens when compiling with "gcc -fpack-struct"
and not when compiling with gcc without "-fpack-struct"].

Which is odd. As near as I can figure, FILE* points to a struct which
really should not be packed on your machine.

Yes, but this could be precisely the problem.

The C standard tells us only that "FILE" is an object type defined
in <stdio.h>, and "stdin", "stdout", and "stderr" are macros that
expand to expressions that have type "pointer to FILE".

Macros? They don't expand to anything with my compiler. Can't they just be
pre-assigned variables?

Or is the Standard saying they could be macros, and so you shouldn't try
to assign to them?

[snip cogent explanation of what could be going wrong here]
No directly-visible ones, perhaps, but I would bet money that the
above is the correct explanation for the problem. :)

Well, the code in the headers presumably is not his code, in that he did
not write it. (I knew someone would call me on that statement.)
 
P

pete

Chris said:
[the crash happens when compiling with "gcc -fpack-struct"
and not when compiling with gcc without "-fpack-struct"].

Which is odd. As near as I can figure,
FILE* points to a struct which
really should not be packed on your machine.

Yes, but this could be precisely the problem.

The C standard tells us only that "FILE" is an object type defined
in <stdio.h>, and "stdin", "stdout", and "stderr" are macros that
expand to expressions that have type "pointer to FILE".

Macros? They don't expand to anything with my compiler.
Can't they just be pre-assigned variables?

The standard refers to them as macros.

N869
7.19.1 Introduction
[#1] The header <stdio.h> declares three types, several
macros, and many functions for performing input and output.
[#2] The types declared are ...

[#3] The macros are ...

.... and then, just before you get to [#4], there they are.
 
C

Chris Torek

[on stdin, stdout, and stderr]

The standard refers to them as macros.

N869
7.19.1 Introduction
[#1] The header <stdio.h> declares three types, several
macros, and many functions for performing input and output.
[#2] The types declared are ...

[#3] The macros are ...

... and then, just before you get to [#4], there they are.

All of which means that a C test suite can do things like:

#include <stdio.h>

#ifndef stdout
# error stdout not defined by stdio.h
#endif

It is a bit silly, but if <stdio.h> has "extern FILE *stdout;" as
an ordinary variable declaration, it must then have something like:

#define stdout stdout

just to comply with this useless verbiage in the Standard. :)
 
M

Mark McIntyre

Macros? They don't expand to anything with my compiler. Can't they just be
pre-assigned variables?

No and yes. Whether they can or not is system-specific and you should not
rely on ihs behaviour.
Or is the Standard saying they could be macros, and so you shouldn't try
to assign to them?

Not only that, but some environments will complain if you try to assign
them to something else.
 

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
474,147
Messages
2,570,833
Members
47,380
Latest member
AlinaBlevi

Latest Threads

Top