Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: Mutually referential types (http://www.velocityreviews.com/forums/t314781-re-mutually-referential-types.html)

Eric 08-15-2003 06:20 PM

Re: Mutually referential types
 
Colin JN Breame <c.j.n.breame@durham.ac.uk> wrote:

> typedef struct {
> B *pB;
> } A;
>
> typedef struct {
> A *pA;
> } B;


should be:


typedef struct A
{
struct B *pB;
} A;

typedef struct B
{
struct A *pA;
} B;


--
== Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like violence, are the last refuge of the incompetent... ===

signuts 08-15-2003 06:58 PM

Re: Mutually referential types
 
> I've been using constructs like this, but I still wonder why it
> works - IOW: how is the compiler able to accept 'struct B *pB'
> without already having a declaration of 'struct B' at hand????


Someone please correct me if i'm wrong but I believe it's because
you're creating a pointer, of type "struct B." There is no need to
know all the members of the structure in order to do this.

A pointer is always the same size.. on my system it's 4

Hope this helps.

--
Sig
Fri Aug 15 14:56:25 EDT 2003


Arthur J. O'Dwyer 08-15-2003 07:24 PM

Re: Mutually referential types
 

On Fri, 15 Aug 2003, signuts wrote:
> >
> > I've been using constructs like this, but I still wonder why it
> > works - IOW: how is the compiler able to accept 'struct B *pB'
> > without already having a declaration of 'struct B' at hand????

>
> Someone please correct me if i'm wrong but I believe it's because
> you're creating a pointer, of type "struct B."


You're creating a pointer of type "pointer to struct B."
^^^^^^^^^^

> There is no need to
> know all the members of the structure in order to do this.


Right. In fact, your compiler *will* complain if you try
to access the members of an object of type 'struct B' before
'struct B' is defined, for precisely this reason. But since
you don't need to know the layout of 'struct B' in order to
point to it, everything's fine.

> A pointer is always the same size.. on my system it's 4


Not necessarily. The C standard doesn't even mandate that
all pointers *to structures* be the same size - maybe
sizeof(struct foo*)==4 and sizeof(struct bar*)==8. But
in practice, most compilers will make all struct pointers
the same size because it makes their job easier.

-Arthur


Eric Sosman 08-15-2003 09:01 PM

Re: Mutually referential types
 
"Arthur J. O'Dwyer" wrote:
>
> On Fri, 15 Aug 2003, signuts wrote:
> > >
> > > I've been using constructs like this, but I still wonder why it
> > > works - IOW: how is the compiler able to accept 'struct B *pB'
> > > without already having a declaration of 'struct B' at hand????

> >
> > Someone please correct me if i'm wrong but I believe it's because
> > you're creating a pointer, of type "struct B."

>
> You're creating a pointer of type "pointer to struct B."
> ^^^^^^^^^^
>
> > There is no need to
> > know all the members of the structure in order to do this.

>
> Right. In fact, your compiler *will* complain if you try
> to access the members of an object of type 'struct B' before
> 'struct B' is defined, for precisely this reason. But since
> you don't need to know the layout of 'struct B' in order to
> point to it, everything's fine.
>
> > A pointer is always the same size.. on my system it's 4

>
> Not necessarily. The C standard doesn't even mandate that
> all pointers *to structures* be the same size - maybe
> sizeof(struct foo*)==4 and sizeof(struct bar*)==8. But
> in practice, most compilers will make all struct pointers
> the same size because it makes their job easier.


6.2.5 Types
/26/ [...] All pointers to structure types shall have
the same representation and alignment requirements as
each other. [...]

.... so "most compilers" is a fairly inclusive set.

--
Eric.Sosman@sun.com

Mark Haigh 08-17-2003 12:40 AM

Re: Mutually referential types
 
signuts wrote:
>>I've been using constructs like this, but I still wonder why it
>>works - IOW: how is the compiler able to accept 'struct B *pB'
>>without already having a declaration of 'struct B' at hand????

>
>
> Someone please correct me if i'm wrong but I believe it's because
> you're creating a pointer, of type "struct B." There is no need to
> know all the members of the structure in order to do this.
>
> A pointer is always the same size.. on my system it's 4


This is misleading; all pointers may not, in fact, be the same size.
However, pointers to incomplete types must be of the same size as
pointer to void (which is simply an incomplete type that cannot be
completed).

Otherwise you would have chaos as different incomplete types in
different translation units could choose representations of differing size.



Mark F. Haigh
mfhaigh@sbcglobal.ten


Mark Haigh 08-17-2003 12:45 AM

Re: Mutually referential types
 
> Otherwise you would have chaos as different incomplete types in
> different translation units could choose representations of differing size.


Oops, should be:

Otherwise you would have chaos as pointers to different incomplete types
in different translation units could have representations of differing size.

Time for another beer :-)

Mark F. Haigh
mfhaigh@sbcglobal.ten



All times are GMT. The time now is 04:13 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.