Nesting a Structure within itself? 
Author Message
 Nesting a Structure within itself?

I tried this:

typedef struct some_type {

        int     value;
        int     another;

        struct some_type    data;

Quote:
} some_type;

And I get some sort of error message.  But I can define a struct
some_type *data.  So I can use a pointer, but I can't use an automatic
variable ( is "automatic" is the correct terminology? ).

Does this have something to do with the compiler not knowing the size of
the struct yet, so it doesn't know how many bytes are contained in this
some_type structure I made up??  I just thought that somehow it would
"look ahead" and do it anyway, since it already can declare a pointer to
itself.



Wed, 18 Apr 2001 03:00:00 GMT  
 Nesting a Structure within itself?
On Sat, 31 Oct 1998 04:02:47 -0600, Brian Fleming

Quote:

>I tried this:

>typedef struct some_type {

>    int     value;
>    int     another;

>    struct some_type    data;

>} some_type;

>And I get some sort of error message.

Do you expect data to also contain a member called data?
If so, then do you expect data.data to also contain a member called data?
...

This declaration would yield a type with infinite size.

Quote:
>But I can define a struct some_type *data.  So I can use a pointer,
>but I can't use an automatic variable ( is "automatic" is the correct
>terminology? ).

I'm not sure what the right term is, but if you want a recursive
structure, then a pointer is what you want.

Quote:
>Does this have something to do with the compiler not knowing the size of
>the struct yet, so it doesn't know how many bytes are contained in this
>some_type structure I made up??  I just thought that somehow it would
>"look ahead" and do it anyway, since it already can declare a pointer to
>itself.

A pointer to a struct is a different type from the struct itself.  The
size of a pointer is always known to the compiler.  In that sense, it
is a fundamental type.

--

http://www.cs.wustl.edu/~jxh/        Washington University in Saint Louis

Quote:
>>>>>>>>>>>>> I use *SpamBeGone* <URL:http://www.internz.com/SpamBeGone/>



Wed, 18 Apr 2001 03:00:00 GMT  
 Nesting a Structure within itself?
Others have pointed out, quite correctly, that to put an instance
of "struct x" inside "struct x" necessarily requires that "struct x"
either be empty (the empty set is a member of the empty set) or
infinite.  Neither is allowed in C.


writes:

Quote:
>The size of a pointer to anything is fixed (and implementation dependent),
>often is 4 bytes (32 bit). A pointer can be converted to a pointer to void and
>converted back without changes,

All of this is correct, but:

Quote:
>that implies the size of a pointer does not depend on the objects it
>is pointing to. (size (void *) = size(che *) = size(long double *) =
>size(struct xyz *))

this is just plain wrong.

The fact that you can *convert* any data pointer (e.g., "int *")
to "void *" and back does not imply that the original pointer is
the same size as "void *".

For instance, there are machines where "void *" (and "char *") are
"byte pointers", while most or all other pointers are "word pointers".
These "byte pointers" are sometimes larger in size than word
pointers, typically because they need extra bits to specify which
byte is to be selected in the word to which the word-pointer points.
(In other words, on these machines, a byte pointer is really a
word pointer plus the byte offset within the word.)

Similarly, on many machines, you can convert any 32-bit "int" to
a 64-bit IEEE "double", then convert it back to an "int", and get
the same "int".  This does not mean that 32 bits and 64 bits are
the same number of bits.

Now, it stands to reason that "void *" pointers are unlikely to be
*smaller* than any other data pointer (since they need to be able
to hold enough information to convert a value back to the original
pointer).  The C Standard does not *require* this, though, so a
sufficiently perverse implementation could choose to make, e.g.,
sizeof(double *) > sizeof(void *).

This sort of thing is why you should only rely on behavior guaranteed
by the standard whenever feasible.  If you have two ways to do
something, one standard-conforming and one not, and either one is
about equally easy or difficult, choosing the standard-conforming
method will get you a program that works on more systems.
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc

Antispam notice: unsolicited commercial email will be handled at my
consulting rate; pyramid-scheme mail will be forwarded to the FTC.



Wed, 18 Apr 2001 03:00:00 GMT  
 Nesting a Structure within itself?


Quote:
> I tried this:

> typedef struct some_type {

>    int     value;
>    int     another;

>    struct some_type    data;

> } some_type;

> And I get some sort of error message.  But I can define a struct
> some_type *data.  So I can use a pointer, but I can't use an automatic
> variable ( is "automatic" is the correct terminology? ).

> Does this have something to do with the compiler not knowing the size of
> the struct yet, so it doesn't know how many bytes are contained in this
> some_type structure I made up??  I just thought that somehow it would
> "look ahead" and do it anyway, since it already can declare a pointer to
> itself.

<Jack>

ANSI/ISO 9899:1990 section 6.5.2.1 structure and union specifiers:

Under constraints:

"A structure or union shall not contain a member with incomplete or function
type.  Hence it shall not contain an instance of itself (but may contain a
pointer to an instance of itself)."

It is easy to include a pointer to itself, it knows how large a pointer to
structure is because the size of the pointer is the same no matter what type
of structure it points to.

</Jack>



Thu, 19 Apr 2001 03:00:00 GMT  
 Nesting a Structure within itself?

Quote:

> > "A structure or union shall not contain a member with incomplete or function
> > type.  Hence it shall not contain an instance of itself (but may contain a
> > pointer to an instance of itself)."

> > It is easy to include a pointer to itself, it knows how large a pointer to
> > structure is because the size of the pointer is the same no matter what type
> > of structure it points to.

> This is a point I have trouble understanding. In a previous posting in this
> thread I made the statement that the sizeof a pointer to something was
> independent from the something. That is: sizeof(char *) =.....=sizeof(long
> double *)=sizeof(void *) with ... going through all fundamental types. I
> deduced that from the rules about pointer conversion back anf forth from
> void*. I was replied that that is not true, because the standard make no
> assumption on the size of pointers. Now you are making the statement (that
> makes a lot of sense to me) that the sizeof struct is the same independent
> from the struct. I see that the two statements are not contr{*filter*}ory, but I
> find them a little bizzare. The question is therefore: may the size of
> pointers be dependent from the object is pointing to and, even more bizzare,
> may the size of a pointer be larger than the size of a pointer to void?
> Tjanks for the reply, Paolo

The size of a pointer is dependent on the object being pointed to - each
of the fundamental types can have a different size representation,
larger or smaller, than any other.  And although I can't think of a
reason that a pointer to some type would be larger than a pointer to
void *, it's legal nevertheless.

On the other hand, pointers to structures must all be of the same size,
to allow pointers to incomplete structures and the like.  This could
prevent a compiler from "optimizing" the size of pointers to certain
structures, but this is probably not an optimization any compiler writer
would try to implement if possible anyways.

Another pointer tidbit:  A pointer to char * has the same size and
representation as a pointer to void *.  This allows backwards
compatibility with legacy code, when void * did not exist.  I can't
think of any other reason for this requirement, but if there are others,
I have no doubt that there will be some people who will be glad to
enumerate them for me.

--

I believe we can change anything.
I believe in my dream.
    - Joe Satriani



Thu, 19 Apr 2001 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. structures within structures

2. Q about structures within structures

3. Initialising Structure Within Structure...How?!

4. Restarting a service within itself

5. How to destroy the object within by the class function of itself

6. How to restart MFC application from within itself?

7. Structure member pointing to itself and free()'ing

8. calling to read() twice, once within a nested loop

9. Nesting of a preprocessor directive within a macro defn

10. HELP SDI,Nested CRecordview within CDialog

11. HELP SDI,Nested CRecordview within CDialog

12. CListCtrl not visible within nested PropertySheet

 

 
Powered by phpBB® Forum Software