Re: More proof of errors going uncorrected among the "regs"

Discussion in 'C Programming' started by Ben Bacarisse, Feb 20, 2009.

  1. pete <> writes:

    > Han from China wrote:
    >
    >> ---------------------------------------------------------------------------
    >> $ cat proof.c
    >> struct reg farts; // Look, declaration of an object of incomplete type!
    >>
    >> int main(void) { return 0; }
    >>
    >> struct reg {int haha;};
    >>
    >> $
    >> ---------------------------------------------------------------------------

    >
    >> Comments?

    >
    > Compiling...
    > new.c
    > C:\Program Files\DevStudio\SharedIDE\bin\new.c(5) : error C2079:
    > farts' uses undefined struct 'reg'
    > Error executing cl.exe.
    >
    > new.obj - 1 error(s), 0 warning(s)


    As so often happens, I am not sure what point you are making! This
    looks like an error in the compiler or in the way you invoke it.

    Han's code looks correct to me, though I disagree with his apparent
    conclusion. Object types and incomplete types form disjoint sets so
    the tentative definition is a declaration of an identifier, not of an
    object. The standard is a bit fuzzy about this, but 6.9.2p2 (about
    tentative definitions) does talk about "A declaration of an identifier
    *for* an object..." [emphasis mine] as if it would really like to
    make this important distinction. In other words, it is only the
    second declaration that declares (and defines) the object.

    --
    Ben.
    Ben Bacarisse, Feb 20, 2009
    #1
    1. Advertising

  2. On Fri, 20 Feb 2009 04:01:41 +0000, Ben Bacarisse
    <> wrote:

    > pete <> writes:
    >
    > > Han from China wrote:


    > >> struct reg farts; // Look, declaration of an object of incomplete type!


    > >> struct reg {int haha;};


    > Han's code looks correct to me, though I disagree with his apparent
    > conclusion. Object types and incomplete types form disjoint sets so
    > the tentative definition is a declaration of an identifier, not of an
    > object. The standard is a bit fuzzy about this, but 6.9.2p2 (about
    > tentative definitions) does talk about "A declaration of an identifier
    > *for* an object..." [emphasis mine] as if it would really like to
    > make this important distinction. In other words, it is only the
    > second declaration that declares (and defines) the object.


    The terminology can be a little confusing, especially as the same
    words are frequently used for both syntactic (source-only) constructs
    and related but often nonidentical semantic constructs (which mostly
    come through to, or at least affect, runtime).

    A syntactic declaration can be a semantic declaration only, or a
    semantic declaration plus a definition or tentative definition. (For
    functions, a function-definition is syntactically not a declaration,
    but semantically a definition. But here we deal with an object.)

    All semantic declarations associate an identifier with an object or
    function, which has a specified type, within their scope. (Or for a
    typedef an identifier is associated with a type, or for an enumerator
    with an int value (and type). And struct/union/enums can declare a tag
    rather than, or in addition to, an object or typedef.) That's their
    purpose. A definition (also) creates = allocates and optionally
    initializes the object, or specifies the contents of the function.

    These concepts are clearly related -- an identifier referencing an
    entity is only meaningful if that entity exists (or only _while_ it
    exists for an automatic object, whose lifetime is less than all of the
    program's execution), and an entity is only useful if it can be
    referenced, which has to start with at least one identifier.
    (Static entities and auto objects are referenced directly by an
    identifier; dynamic = malloced objects can be referenced only by a
    pointer, but that pointer has to come from some kind of expression
    whose component(s) come from identifier(s), all the way down).
    But they are distinct, and the differences matter.

    There may be multiple semantic declarations for the same identifier
    (and entity) in the same scope, as long as they specify compatible
    types. But at most one semantic definition for the entity. For objects
    with no linkage -- block-scope auto and static -- every syntactic
    declaration is a semantic definition, and hence there can be only one.
    If the declarations specify different (but compatible!) types, the
    type information is merged into a single 'composite' type. Similarly
    for some types there may be an declaration of the incomplete type
    (sometimes called forward) and a later declaration that completes it.
    (IMO it would make sense to use the same terminology and call this a
    definition of the type, but the standard doesn't do so.)

    In the example, the first declaration declares the identifier 'farts'
    as an object having the incomplete type 'struct reg'. The second
    declaration completes the type 'struct reg'; it doesn't declare
    'farts' at all. If it did, that would be legal, and would redundantly
    declare 'farts' to be of the now-completed type 'struct reg', still
    without defining the object. At the end of the translation unit, since
    there was a tentative definition (or two) but no actual definition,
    the compiler supplies a 'declaration ... with the composite type ...
    and initializer ... 0'. Which as Han noted is effectively { 0 } .
    Such a declaration with an initializer is a definition, and thus is
    the definition of the object. (Formally it also is another semantic
    declaration, but for typical one-pass parsers it must be at the end of
    the t.u., so its scope is empty, and as a declaration it is useless.)
    David Thompson, Mar 9, 2009
    #2
    1. Advertising

  3. Ben Bacarisse

    Stefan Ram Guest

    David Thompson <> writes:
    >These concepts are clearly related -- an identifier referencing an
    >entity is only meaningful if that entity exists


    An entity exists (otherwise, it would not be an entity).
    Stefan Ram, Mar 9, 2009
    #3
  4. Ben Bacarisse

    Richard Bos Guest

    -berlin.de (Stefan Ram) wrote:

    > David Thompson <> writes:
    > >These concepts are clearly related -- an identifier referencing an
    > >entity is only meaningful if that entity exists

    >
    > An entity exists (otherwise, it would not be an entity).


    That depends on the philosopher. To some, an entity is something that
    exists; to others, it is something which, _if_ it exists, has an
    independent existence, as opposed to, say, properties and concepts.
    Under the latter definition, an unsigned int is (within the world of a C
    program) an entity, while "unsignedness", though clearly existing even
    when no unsigned ints happen to be present, is not.

    Richard
    Richard Bos, Mar 11, 2009
    #4
  5. In article <-berlin.de>,
    Stefan Ram <-berlin.de> wrote:
    >David Thompson <> writes:
    >>These concepts are clearly related -- an identifier referencing an
    >>entity is only meaningful if that entity exists

    >
    > An entity exists (otherwise, it would not be an entity).
    >


    Isn't it pretty clear that both Beaver Cleaver and God are entities.

    Yet neither exists.

    And also Santa Clause and the Easter Bunny.
    Kenny McCormack, Mar 11, 2009
    #5
  6. Ben Bacarisse

    Guest

    On 11 Mar, 16:14, (Kenny McCormack) wrote:
    > In article <-berlin.de>,
    > Stefan Ram <-berlin.de> wrote:
    > >David Thompson <> writes:


    > >>These concepts are clearly related -- an identifier referencing an
    > >>entity is only meaningful if that entity exists

    >
    > >  An entity exists (otherwise, it would not be an entity).

    >
    > Isn't it pretty clear that both Beaver Cleaver and God are entities.
    >
    > Yet neither exists.
    >
    > And also Santa Clause and the Easter Bunny.


    I was going to make a very similar reply (using Sherlock Holmes as my
    example) but you beat me to it.
    , Mar 12, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Karl Core

    Any regs up for a CSS challenge

    Karl Core, Feb 20, 2005, in forum: HTML
    Replies:
    2
    Views:
    397
    Augustus
    Feb 21, 2005
  2. what are REGS,SREGS ?

    , Oct 5, 2005, in forum: C Programming
    Replies:
    5
    Views:
    677
    Alexei A. Frounze
    Oct 6, 2005
  3. Rod Pemberton

    RE: union REGS don't get DOS Version

    Rod Pemberton, Feb 8, 2006, in forum: C Programming
    Replies:
    1
    Views:
    490
    Bruno Barros
    Feb 9, 2006
  4. Marcpp
    Replies:
    1
    Views:
    264
    Jorgen Bodde
    Jun 12, 2007
  5. Eric Sosman
    Replies:
    5
    Views:
    274
    Tim Rentsch
    Feb 24, 2009
Loading...

Share This Page