Cyclical Dependency

B

Bryan Bullard

I have a cyclical dependency problem very much like the one listed below.
Does anyone have a solution?

struct A;
struct B;

struct A
{
struct B b;
};

struct B
{
struct A a;
};

int main() { return 0; }


TIA,
Bryan
 
J

Joona I Palaste

Bryan Bullard said:
I have a cyclical dependency problem very much like the one listed below.
Does anyone have a solution?
struct A;
struct B;
struct A
{
struct B b;
};
struct B
{
struct A a;
};
int main() { return 0; }

This code is impossible to compile, as you very well know. Both of your
structs contain themselves as elements, which means that their size is
infinite.
The solution is, of course, pointers.

struct A;
struct B;

struct A
{
struct B *b;
};

struct B
{
struct A *a;
}

int main() { return 0; }

Is this your real code or an exercise from a C textbook?

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I said 'play as you've never played before', not 'play as IF you've never
played before'!"
- Andy Capp
 
B

Ben Pfaff

Bryan Bullard said:
I have a cyclical dependency problem very much like the one listed below.
Does anyone have a solution?

struct A;
struct B;

struct A
{
struct B b;
};

struct B
{
struct A a;
};

One or both of the structure-type structure members must become a
pointer to a structure.
 
M

Martin Ambuhl

Bryan said:
I have a cyclical dependency problem very much like the one listed below.
Does anyone have a solution?

Yes: don't do the impossible. You need an infinite memory to do what
you are attempting.
struct A;
struct B;

struct A
{
struct B b;
};

struct B
{
struct A a;
};

If you are actually trying to do something useful, rather than the
absurd infinite

struct A {
struct B {
struct A {
struct B {
....

use pointers to structs.
 
B

Bryan Bullard

Joona I Palaste said:
This code is impossible to compile, as you very well know. Both of your
structs contain themselves as elements, which means that their size is
infinite.
The solution is, of course, pointers.

Well, in this instance I need the actual struct resident not pointers.
Here is a closer example:

/* file A.h * * * * * * */
#ifndef A_H
#define A_H

#include "B.h"

struct A
{
struct B b;
};

#endif


/* file B.h * * * * * * */
#ifndef B_H
#define B_H

#include "A.h" /* some other resource in A.h is needed */

struct B
{...};

#endif

/* main.c * * * * * * */
#include "A.h" /* needs resources in A.h and B.h */
#include "B.h"

int main() { return 0; }

This can become a problem when I must always be assured that "B.h" is
preprocessed before "A.h".
Is this your real code or an exercise from a C textbook?

Real code I'm afraid.
 
M

Malcolm

Bryan Bullard said:
I have a cyclical dependency problem very much like the one listed below.
Does anyone have a solution?
90% of the time the solution is to rework the logic of your progra, so that
cyclic dependencies disappear.
As others have pointed out, structs cannot mutually contain each other (like
a box cannot contain another box containing itself). However if you really
need mutual dependency then you can use pointers.
 
J

Joona I Palaste

Well, in this instance I need the actual struct resident not pointers.
Here is a closer example:
/* file A.h * * * * * * */
#ifndef A_H
#define A_H
#include "B.h"
struct A
{
struct B b;
};


/* file B.h * * * * * * */
#ifndef B_H
#define B_H
#include "A.h" /* some other resource in A.h is needed */
struct B
{...};

/* main.c * * * * * * */
#include "A.h" /* needs resources in A.h and B.h */
#include "B.h"
int main() { return 0; }
This can become a problem when I must always be assured that "B.h" is
preprocessed before "A.h".

I don't understand this at all. The code in your closer example here is
completely different from your original code. Your original code was
impossible to compile, even theoretically, as you had structures that
included themselves.
This code, however, has a structure including a structure including
something unknown. As long as that unknown isn't either of these two
structures, your program can be solved with careful attention to
preprocessing.
What are the "resources" you speak of? Structures? Typedefs? Macros?
Actual code? Knowing this is *very* important to determine whether
your problem can be solved.
Do you mean that "A.h" must use types defined in "B.h" and vice versa?
I don't see any way of doing this other than making a separate header
file including all the types that either file needs from the other and
making both #include it.
It's just strange that between your two posts, the entire problem
changed from a struct type problem to a preprocessing problem.
 
B

Bryan Bullard

Do you mean that "A.h" must use types defined in "B.h" and vice versa?
I don't see any way of doing this other than making a separate header
file including all the types that either file needs from the other and
making both #include it.
It's just strange that between your two posts, the entire problem
changed from a struct type problem to a preprocessing problem.

Right. Sorry for the confusion my made by my illustrations but I don't
believe it will be very helpful to get into specifics. Basically, what I
have is a growing code base that is not scaling very well. I believe what I
need to do is to split up my header files into a more granular and hopefully
scalable manner.
"The trouble with the French is they don't have a word for entrepreneur."
- George Bush

Great quote. :)
 

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

Members online

Forum statistics

Threads
474,146
Messages
2,570,831
Members
47,374
Latest member
anuragag27

Latest Threads

Top