forward declarations and incomplete types

F

fmassei

Hello,
I'm have a question about forward structure declarations and
incomplete types. The complete structure of my file is pretty complex,
but just as an example the code below will explain the problem:

struct b;
struct a {
struct b m;
};
struct b {
struct a m;
};

This code won't compile: my compiler (gcc) says that a.m "has
incomplete type". Changing the members' types to structure pointers
works, but it will be nicer if I could do this as if the two
definitions were not "cross-defined".
Is there a way for doing this?
 
G

gw7rib

Hello,
I'm have a question about forward structure declarations and
incomplete types. The complete structure of my file is pretty complex,
but just as an example the code below will explain the problem:

struct b;
struct a {
    struct b m;};

struct b {
    struct a m;

};

This code won't compile: my compiler (gcc) says that a.m "has
incomplete type". Changing the members' types to structure pointers
works, but it will be nicer if I could do this as if the two
definitions were not "cross-defined".
Is there a way for doing this?

I thought this was a FAQ, but I can't actually find it...

Your problem is that you want a to contain a b, and b to contain an a.
In fact, the way you have written it, the two structs look identical,
and there don't seem to be any actual members there that you can use.
But if there were, then you would be wanting each struct to be bigger
than the other. Obviously this is impossible.

You probably want at least one of the structs (probably both) to
contain a pointer to a struct of the other sort. If this does not seem
right, feel free to come back with more details.

Hope that helps.
Paul.
 
F

fmassei

I thought this was a FAQ, but I can't actually find it...

Your problem is that you want a to contain a b, and b to contain an a.
In fact, the way you have written it, the two structs look identical,
and there don't seem to be any actual members there that you can use.
But if there were, then you would be wanting each struct to be bigger
than the other. Obviously this is impossible.

You probably want at least one of the structs (probably both) to
contain a pointer to a struct of the other sort. If this does not seem
right, feel free to come back with more details.

Hope that helps.
Paul.

Ok, one problem is actually caused by my stupidity :) Of course I
can't have them the way I wrote. At this point the "incomplete type"
problem remains but it's easy to solve moving the code (I added some
other members just to make it more realistic).

struct b;
struct a {
int i;
struct b m;
};
struct b {
int i, j;
struct a *m;
};

This will continue not to work. But I don't think there's a way to get
around it without moving things around, isn't it?
 
R

rahul

struct b;
struct a {
    int i;
    struct b m;};
No work arounds for this are possible; you need to have a pointer to
the incomplete type. The definition reserves space and in this case,
it can't reserve memory as the compiler is not aware about the amount
of space.
struct b {
    int i, j;
    struct a *m;
Need not have a pointer(until and unless you really need it to be a
pointer; i.e not required by the compiler)
 
G

gw7rib

struct b;
struct a {
    int i;
    struct b m;};

struct b {
    int i, j;
    struct a *m;

};

This will continue not to work. But I don't think there's a way to get
around it without moving things around, isn't it?

No, that won't work, because if you're going to include a whole b as
part of a, it needs to know how big b is. So you need to tell it that
first.

But your code should work if you move things around, as follows:

struct a;

struct b {
int i, j;
struct a *m;
};

// no problem here, we just want a pointer to a

struct a {
int i;
struct b m;};

// no problem here, we now know how big a b is.

Paul.
 
F

fmassei

No work arounds for this are possible; you need to have a pointer to
the incomplete type. The definition reserves space and in this case,
it can't reserve memory as the compiler is not aware about the amount
of space.


Need not have a pointer(until and unless you really need it to be a
pointer; i.e not required by the compiler)

Or simply moving the definition of struct b before struct a. It's
really weird that the order of the definitions counts. Probably it's
only a compiler matter, I don't believe that the standard defines such
a strict rule.
 
L

lawrence.jones

Or simply moving the definition of struct b before struct a. It's
really weird that the order of the definitions counts. Probably it's
only a compiler matter, I don't believe that the standard defines such
a strict rule.

You believe incorrectly. The standard supports a one-pass compilation
model, so a type has to be complete before you can use an actual
instance of it (so the compiler knows how much space to reserve).
Pointers to incomplete types are OK, but not instances.
 
F

fmassei

You believe incorrectly. The standard supports a one-pass compilation
model, so a type has to be complete before you can use an actual
instance of it (so the compiler knows how much space to reserve).
Pointers to incomplete types are OK, but not instances.

Nice. Thank you all for the infos, there is always something to learn
here!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top