473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Cyclical Dependency

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
Nov 14 '05 #1
8 1494
Bryan Bullard <re***@to.group.com> scribbled the following:
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 (pa*****@cc.helsinki.fi) ------------- 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
Nov 14 '05 #2
"Bryan Bullard" <re***@to.group.com> writes:
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.
--
"IMO, Perl is an excellent language to break your teeth on"
--Micah Cowan
Nov 14 '05 #3
Bryan Bullard wrote:
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.
Nov 14 '05 #4

"Bryan Bullard" <re***@to.group.com> wrote

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.
Nov 14 '05 #5

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:cf**********@oravannahka.helsinki.fi...
Bryan Bullard <re***@to.group.com> scribbled the following:
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.


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.
Nov 14 '05 #6

"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote in message
news:cf**********@newsg3.svr.pol.co.uk...

"Bryan Bullard" <re***@to.group.com> wrote

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.


This is what I think.
Nov 14 '05 #7
Bryan Bullard <re***@to.group.com> scribbled the following:
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:cf**********@oravannahka.helsinki.fi...
Bryan Bullard <re***@to.group.com> scribbled the following:
> 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.

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".


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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The trouble with the French is they don't have a word for entrepreneur."
- George Bush
Nov 14 '05 #8

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:cf**********@oravannahka.helsinki.fi...
Bryan Bullard <re***@to.group.com> scribbled the following:
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:cf**********@oravannahka.helsinki.fi...
Bryan Bullard <re***@to.group.com> scribbled the following:
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. :)
Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Kyle Root | last post by:
I'm working on a little practice program, but I've become stuck and am now utterly confused :? I've created a jumble of python modules, in each one is a tuple that goes something like, deps =...
2
by: Chris Capel | last post by:
Say ... does anyone know of a good (perhaps free) .NET assembly dependency walker? Because when you have a project A with a reference B which is also a reference of C, and C is also a reference of...
0
by: Michael R. Pierotti | last post by:
Has anyone seen this error before when trying to make the install on a program. ------ Starting pre-build validation for project 'HafaSMPPInstall' ------ WARNING: Unable to find dependency...
5
by: Jay A. Moritz | last post by:
Error: The dependency '<my dll>' in project '<my project>' cannot be copied to the run directory because it would conflict with dependency '<my dll>'. I am getting a dependency error building...
3
by: DJTN | last post by:
I'm getting the following error when I try to compile my setup project in VS 2002. I have re-installed the .net framework 1.1 and it didnt solve the problem. WARNING: Unable to find dependency...
20
by: Luke Matuszewski | last post by:
Welcome As suggested i looked into JSON project and was amazed but... What about cyclical data structures - anybody was faced it in some project ? Is there any satisactional recomendation... ...
9
by: Brett Romero | last post by:
I have two projects in one solution - a library and executable. Each have a namespace such as: ROOT.myapp.Functional ROOT.myapp.UI However, each cannot reference the other. myapp.UI is...
1
by: =?Utf-8?B?SmFzb24gUmljaG1laWVy?= | last post by:
I came across a code sample in a book that I am reading that uses a dependency property. I looked at some of the documentation for dependency properties but, for some reason, the concept is not...
1
by: vincentt | last post by:
Hi, We code DLL's and so far it was done using VS6.0. We are planning to migrate the VS.NET 2005 and use the VS6 VC++ code which generated the DLL to VS 2005 VC++.NET. However we donot plan to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.