cascaded struct members.

Discussion in 'C++' started by sunny, Oct 16, 2006.

  1. sunny

    sunny Guest

    Hi All

    why does forward declaration does not work in following code.

    struct A;
    struct B
    {
    struct A a;

    // struct A *a // WORKS SINCE ITS POINTER
    };

    struct A
    {
    struct B b;
    };

    gives compiler error for incomplete type A. why does forward
    declaration of Struct A solve this problem.
    sunny, Oct 16, 2006
    #1
    1. Advertising

  2. sunny

    red floyd Guest

    sunny wrote:
    > Hi All
    >
    > why does forward declaration does not work in following code.
    >
    > struct A;
    > struct B
    > {
    > struct A a;
    >
    > // struct A *a // WORKS SINCE ITS POINTER
    > };
    >
    > struct A
    > {
    > struct B b;
    > };
    >
    > gives compiler error for incomplete type A. why does forward
    > declaration of Struct A solve this problem.
    >


    1. How big is A? The compiler doesn't know at the point you declare A a
    in B.

    2. How big is an A at the end of your code? If you were allowed to
    have an A contain a full B, and vice versa, then it's infinitely sized:

    A contains a B which contains an A which contains a B which ....

    3. Works when a is an A* because the size of a pointer is well-defined.
    red floyd, Oct 16, 2006
    #2
    1. Advertising

  3. sunny wrote:
    > why does forward declaration does not work in following code.
    >
    > struct A;
    > struct B
    > {
    > struct A a;


    Because the compiler NEEDS TO KNOW the internals of 'A' here.

    >
    > // struct A *a // WORKS SINCE ITS POINTER


    All pointers to objects are of the same size. The compiler does
    NOT need to know the internals of 'A' here.

    > };
    >
    > struct A
    > {
    > struct B b;
    > };
    >
    > gives compiler error for incomplete type A. why does forward
    > declaration of Struct A solve this problem.


    The last statement is false. A forward declaration of 'A' does
    NOT solve the problem of mutual containment. Nothing can. If
    each object 'B' contains a member of type 'A', and, in turn, each
    object 'A' contains a member of type 'B', what's the size of the
    objects?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Oct 16, 2006
    #3
  4. sunny

    Ron Natalie Guest

    sunny wrote:
    > Hi All
    >
    > why does forward declaration does not work in following code.
    >
    > struct A;
    > struct B
    > {
    > struct A a;


    You must have a complete definition here since you actually
    allocate one.
    >
    > // struct A *a // WORKS SINCE ITS POINTER
    > };
    >
    > struct A
    > {
    > struct B b;
    > };
    >
    > gives compiler error for incomplete type A. why does forward
    > declaration of Struct A solve this problem.


    You have A containing a B which contains an A which contains a B ...

    How big is this object?
    Ron Natalie, Oct 16, 2006
    #4
  5. sunny

    BobR Guest

    Ron Natalie wrote in message
    <4533c2bf$0$5922$>...
    >sunny wrote:
    >> Hi All
    >>
    >> why does forward declaration does not work in following code.
    >>
    >> struct A;
    >> struct B{
    >> struct A a;
    >> };
    >>
    >> struct A{
    >> struct B b;
    >> };
    >>
    >> gives compiler error for incomplete type A. why does forward
    >> declaration of Struct A solve this problem.

    >
    >You have A containing a B which contains an A which contains a B ...
    >
    >How big is this object?


    Same size as the universe + 1.

    I'm running the calculation on my computer now...... uhh....might be a while.
    I'll get back to you on this when it's done...
    <millions of years pass>
    Ahhh, 1% done.... won't be long now...

    --
    Bob <G> R
    POVrookie
    BobR, Oct 16, 2006
    #5
  6. BobR wrote:

    >>You have A containing a B which contains an A which contains a B ...
    >>
    >>How big is this object?

    >
    > Same size as the universe + 1.
    >
    > I'm running the calculation on my computer now...... uhh....might be a
    > while. I'll get back to you on this when it's done...
    > <millions of years pass>
    > Ahhh, 1% done.... won't be long now...


    Your computer is broken. Any decent computer after that long calculation
    must tell that the answer is 42.

    --
    Salu2
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Oct 16, 2006
    #6
  7. sunny

    BobR Guest

    Julián Albo wrote in message <>...
    BobR wrote:

    >>>You have A containing a B which contains an A which contains a B ...
    >>>How big is this object?

    >>
    >> Same size as the universe + 1.
    >>
    >> I'm running the calculation on my computer now...... uhh....might be a
    >> while. I'll get back to you on this when it's done...
    >> <millions of years pass>
    >> Ahhh, 1% done.... won't be long now...


    >Your computer is broken. Any decent computer after that long calculation
    >must tell that the answer is 42.


    I just checked with Duck Dogers (remember Daffy Duck?) in the year 2525, and
    he said the output was:
    42e+std::numeric_limits<long long>::max()
    ....as the calculation passed through his time period. So, obviously YOU
    misplaced a decimal point!!
    [ gads, you'd think with a 1024bit 7.2TeraHz CPU (low end machine) the 'long
    long' would be deprecated by then!! ]

    Yup, that duck really quacks me up!
    --
    Bob <G> R
    POVrookie
    BobR, Oct 17, 2006
    #7
    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. Michael Blackney

    C Preprocessor - Cascaded Macros

    Michael Blackney, Sep 10, 2004, in forum: C Programming
    Replies:
    1
    Views:
    697
    Chris Barts
    Sep 10, 2004
  2. Frank

    Cascaded Macros -- Back-Up

    Frank, Nov 22, 2007, in forum: C Programming
    Replies:
    1
    Views:
    318
    Eric Sosman
    Nov 22, 2007
  3. GENERATE - cascaded

    , Apr 28, 2008, in forum: VHDL
    Replies:
    2
    Views:
    417
  4. dbuchanan
    Replies:
    6
    Views:
    2,824
    Steven Cheng [MSFT]
    Jun 11, 2008
  5. John Reye
    Replies:
    28
    Views:
    1,332
    Tim Rentsch
    May 8, 2012
Loading...

Share This Page