[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: gcc oddities : why would this compile?



On Sat, Jan 05, 2002 at 06:52:04PM -0800, Eric G. Miller wrote:
| On Fri, 4 Jan 2002 21:41:13 -0500, dman <dsh8290@rit.edu> wrote:
| 
| > I have both gcc 2.95.4 and gcc 3.0.2 installed.  Both of them compiled
| > this C program (this is the entire thing) :
| > 
| > -----------
| > int
| > main()
| > {
| >     puts( uname() ) ;
| >     return 0 ;
| > }
| > -----------
| > 
| > Why does this compile without any errors?  (no options were given to
| > gcc)  Why don't I need to include stdio.h or sys/utsname.h?  After I
| > looked at the docs (I just wanted to see what the function would
| > return) I saw that it returns an int to indicate success/failure and
| > takes a pointer to a struct utsname as an out-argument.  (the program
| > above naturally seg faults)  I get the same sort of effect with
| > gethostbyname() (I should include netdb.h, it takes a string argument
| > and returns a pointer to a struct hostent).
| 
| By the looks of it, I would contend you have found a bug.  I think my
| contention is supported by the fact the using -Wall causes the compilation
| to fail at the linker.  So why would -Wall cause it to fail at the
| linker when not using it doesn't?  Using -pedantic-errors, does catch
| and stop the compilation before the linker is called.  So, I'm not
| hundred percent sure it'd be a bug.  GCC does try to support K&R, so
| will relax checking if it thinks it is supposed to "int main()" ??

I thought that leaving out the CL args and environment is legal
(according to ANSI in 1989) if you don't want to deal with them.

This is interesting :

    int main( int argc , char** argv , char*** env )
compiles if no warnings are requested.  With -Wall it suggests char**
for the third argument.  If I correct it to char** (I didn't actually
remember which it was) it compiles fine.  If I try
    int main( int argc , char** argv , char** env , int bogus )
gcc says that main only takes zero or two arguments.  Interesting.  I
know I've seen that environment pointer before (though I don't know
how one would know how big the array is).

| I find GCC is often too lax and wish it'd generate more compilation
| errors out of the box.  I suppose, I could change the defaults somewhere.

Given the reminder that K&R C assumes no arguments (I had forgotten
that part) and return type of int, I tried compiling with 'g++' and it
fails (without specifying -Wall).  I guess I've been used g++ more
than gcc enough that I forgot it would be "legal".

Still, though, if uname() returns 'int' (which is actually does), how
can I pass that to 'puts'?  Hmm, I guess gcc just assumes it will work
since there is no prototype.  Yeah, if I do include stdio.h it tells
me about that.
 
| > The other thing that strikes me as odd, is the following portion of
| > the struct utsname definition (in the manpage) :
| > 
| >     #ifdef _GNU_SOURCE
| >         char domainname[SYS_NMLN] ;
| >     #endif
| > 
| > The odd part is that the 'domainname' member isn't present because
| > _GNU_SOURCE isn't defined on my system.
| 
| CONFORMING TO
|        SVr4, SVID, POSIX, X/OPEN.  There is no uname call in BSD 4.3.
| 
|        The domainname member (the NIS or YP domain name) is a  GNU  exten­
|        sion.
| 
| If you want all the GNU extensions, use -D_GNU_SOURCE or #define it.

I thought (without finding a manual) it would have been defined if I
was using GNU sources.  Defining it does allow it to compile, and
gives output without crashing (that means it really is a 'string').
The output is "(none)", which makes sense since the FM you qouted says
it is an NIS name.  (gethostbyname() returns what I was actually
looking for)

Thanks,
-D

-- 

If anyone would come after me, he must deny himself and take up his
cross and follow me.  For whoever wants to save his life will lose it,
but whoever loses his life for me and for the gospel will save it.  What
good is it for a man to gain the whole world, yet forfeit his soul?  Or
what can a man give in exchange for his soul?
        Mark 8:34-37



Reply to: