🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

7 total messages Started by Tim Hollebeek Mon, 01 Apr 1996 21:53
Initializing a variable in terms of itself
#4976
Author: Tim Hollebeek
Date: Mon, 01 Apr 1996 21:53
17 lines
1062 bytes
Just out of curiosity, is this legal?

int main() {
    int x = x;
    return 0;
}

gcc compiles it without even a warning, suprisingly.  I had a slightly
more complex version of the same thing which was part of a bug in
another program, and would like to have known about it (the second x
was a typo).

---------------------------------------------------------------------------
Tim Hollebeek         | Disclaimer :=> Everything above is a true statement,
Electron Psychologist |                for sufficiently false values of true.
Princeton University  | email: tim@wfn-shop.princeton.edu
----------------------| http://wfn-shop.princeton.edu/~tim (NEW! IMPROVED!)
Re: Initializing a variable in terms of itself
#4981
Author: Dan Pop
Date: Tue, 02 Apr 1996 14:29
39 lines
1668 bytes
In <4jpj9l$ik9@cnn.Princeton.EDU> tim@franck (Tim Hollebeek) writes:

>Just out of curiosity, is this legal?
>
>int main() {
>    int x = x;
>    return 0;
>}

ANSI classic 1.6:

 * Undefined behavior --- behavior, upon use of a nonportable or
   erroneous program construct, of erroneous data, or of
   indeterminately-valued objects, for which the Standard imposes no
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   requirements.  Permissible undefined behavior ranges from ignoring the
   situation completely with unpredictable results, to behaving during
   translation or program execution in a documented manner characteristic
   of the environment (with or without the issuance of a diagnostic
   message), to terminating a translation or execution (with the issuance
   of a diagnostic message).

ANSI classic 3.5.7:

    If an object that has automatic storage duration is not initialized
    explicitly, its value is indeterminate.

So it's illegal, but a diagnostic isn't required.

>gcc compiles it without even a warning, suprisingly. 

Why "suprisingly"?  It doesn't complain about i=i++ either.

Dan
--
Dan Pop
CERN, CN Division
Email: danpop@mail.cern.ch 
Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
Re: Initializing a variable in terms of itself
#4977
Author: James Kanze US/ESC 60/3/141 #407...
Date: Tue, 02 Apr 1996 15:13
21 lines
1225 bytes
In article <4jpj9l$ik9@cnn.Princeton.EDU> tim@franck (Tim Hollebeek)
writes:

|> Just out of curiosity, is this legal?

|> int main() {
|>     int x = x;
|>     return 0;
|> }

No, it is undefined behavior, because you are accessing a variable
before having initialized it.

The reference to `x' in the initialization expression is not, per se,
illegal.  The variable is declared, and available for use, as soon as
the compiler sees the `=' sign.
-- 
James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils, études et réalisations en logiciel orienté objet --
                -- A la recherche d'une activité dans une region francophone
Re: Initializing a variable in terms of itself
#4992
Author: Timothy A. McDaniel
Date: Sat, 06 Apr 1996 16:18
23 lines
1240 bytes
In article <KANZE.96Apr2171323@slsvgqt.lts.sel.alcatel.de>,
James Kanze US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de> wrote:
>In article <4jpj9l$ik9@cnn.Princeton.EDU> tim@franck (Tim Hollebeek)
>writes:
...
>|>     int x = x;
...
>The reference to `x' in the initialization expression is not, per se,
>illegal.  The variable is declared, and available for use, as soon as
>the compiler sees the `=' sign.

So
    void *x = &x;
would be legal, then, and x would point to itself?  (Or would it have
to be
    void *x = (void *) &x;
?)

-- 
                             Tim McDaniel
                        Reply-To: tmcd@crl.com
                   Work account is mcdaniel@cpm.com
              Never use mcdaniel@mcdaniel.dallas.tx.us.
Re: Initializing a variable in terms of itself
#4998
Author: Norman Diamond
Date: Tue, 09 Apr 1996 02:44
22 lines
1543 bytes
In article <4k65h6$dfe@nntp.crl.com>, tmcd@crl.com (Timothy A. McDaniel) writes:
>In article <KANZE.96Apr2171323@slsvgqt.lts.sel.alcatel.de>,
>James Kanze US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de> wrote:
>>The variable is declared, and available for use, as soon as
>>the compiler sees the `=' sign.

>So     void *x = &x;
>would be legal, then, and x would point to itself?

Yes.

>(Or would it have to be     void *x = (void *) &x;    ?)

There is no difference between your two versions.  Conversions either way
between a pointer to object type (including a pointer to incomplete type)
and a pointer to void are allowed implicitly and are identical to conversions
obtained from explicit casts.
--
 <<  If this were the company's opinion, I would not be allowed to post it.  >>
"I paid money for this car, I pay taxes for vehicle registration and a driver's
license, so I can drive in any lane I want, and no innocent victim gets to call
the cops just 'cause the lane's not goin' the same direction as me" - J Spammer
Re: Initializing a variable in terms of itself
#5001
Author: J. Kanze
Date: Tue, 09 Apr 1996 10:59
44 lines
1796 bytes
In article <4k65h6$dfe@nntp.crl.com> tmcd@crl.com (Timothy A. McDaniel)
writes:

|> In article <KANZE.96Apr2171323@slsvgqt.lts.sel.alcatel.de>,
|> James Kanze US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de> wrote:
|> >In article <4jpj9l$ik9@cnn.Princeton.EDU> tim@franck (Tim Hollebeek)
|> >writes:
|> ...
|> >|>     int x = x;
|> ...
|> >The reference to `x' in the initialization expression is not, per se,
|> >illegal.  The variable is declared, and available for use, as soon as
|> >the compiler sees the `=' sign.

|> So
|>     void *x = &x;
|> would be legal,

Exactly.

|> then, and x would point to itself?  (Or would it have
|> to be
|>     void *x = (void *) &x;
|> ?)

The cast is not necessary.

I've even used this sort of thing in the past:

    struct NODE
    {
        NODE*           next ;
        NODE*           prec ;
    } ;

    struct NODE     root = { &root , &root } ;

Note that despite the added braces, etc., this is really just a variant
of the same theme.
-- 
James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils en informatique industrielle --
                            -- Beratung in industrieller Datenverarbeitung
Re: Initializing a variable in terms of itself
#5003
Author: Jon Becker
Date: Tue, 09 Apr 1996 14:45
19 lines
1268 bytes
In article <4kciur$l9l@usenet.pa.dec.com>,
Norman Diamond <diamond@tbj.dec.com> wrote:
>
>There is no difference between your two versions.  Conversions either way
>between a pointer to object type (including a pointer to incomplete type)
>and a pointer to void are allowed implicitly and are identical to conversions
>obtained from explicit casts.
>

I see nothing in the standard which suggests that a pointer to
incomplete type is included in the category of pointers to object
type.  My impression is that the standard describes these two pointer
types as entirely distinct.

(Note that this is a different topic than that to which your answer
refers.  I completely agree with your point.  I merely take issue with
the implication of your wording.)

-Jon
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads