| Author |
Message |
|
sunnyslope
Rookie
Joined: Sat Aug 09, 2008 7:18 pm Posts: 17
|
 is it possible to forward declare a struct
I would like to be able to do something like:
struct { B* b; }A; struct B { A* a; }B;
Is there any way to get that code to compile in RobotC? I would expect something along these lines to work:
typedef struct A; typedef struct B;
struct { B* b; }A; struct B { A* a; }B;
Forgive me, I've been programming in C++ for so long I've forgotten many of the diffferences between C and C++. In any case, I've tried many various ways of declaring these structs in order to resolve the circular dependency and nothing seems to compile in RobotC. Is there any way around it?
|
| Sat Aug 09, 2008 7:33 pm |
|
 |
|
Dick Swan
Creator
Joined: Fri Feb 09, 2007 9:21 am Posts: 613
|
 Re: is it possible to forward declare a struct
No. There is no current way to create the "circular" dependency.
I think this is a characteristic of standard C and not simply a ROBOTC declaration.
You may be able to work around this by declaring the two pointers within the structure as pointers to "int" and then make heavey use of "cast" to redefine these. Don't go too far down the road with this approach without trying it out on a simple program because your next post might be about bugs in casts involving pointers; this is my indifect way of saying this casts involving pointers is not something that's had a lot of testing.
|
| Sun Aug 10, 2008 1:26 am |
|
 |
|
sunnyslope
Rookie
Joined: Sat Aug 09, 2008 7:18 pm Posts: 17
|
 Re: is it possible to forward declare a struct
Is there any way you could add this functionality in the next version? I find it extremely limiting not to be able to do this, especially when working with include files. I've started including a number of files at the top of my main.c file. Since robotc has no linker I was intending to include all of my .h files at the top of my main.c file and then after the .h files to include all of my .c files. This should work fine if it were possible to forward declare structs.
|
| Sun Aug 10, 2008 9:28 pm |
|
 |
|
Ford Prefect
Senior Roboticist
Joined: Sat Mar 01, 2008 12:52 pm Posts: 936 Location: a small planet in the vicinity of Beteigeuze
|
 Re: is it possible to forward declare a struct
recursions (direct as well as indirect) are a feature of ANSI C. forward declarations also are a feature of ANSI C as well. So RobotC should have recursions and forward declarations as well.  |  |  |  | Quote: Rekursion und Rekursive Strukturen: Listen und Bäume Wie in jeder anständigen Programmiersprache[27] ist Rekursion auch in C möglich, gleichermaßen direkte Rekursion wie indirekte. In den folgenden Abschnitten sollen mit zunehmendem Schwierigkeitsgrad drei Beispiele für Rekursion in C vorgestellt werden. Rekursion Das Grundprinzip der direkten Rekursion in C: eine Funktion f() ruft sich selbst auf, sofern nicht eine Abbruchbedingung (Rekursionsboden) erfüllt ist. Die indirekte Rekursion unterscheidet sich davon dadurch, daß zwei oder mehr Funktionen beteiligt sind, die sich wechselseitig aufrufen. Zur einfachen, direkten Rekursion sei das beliebte Beispiel der Berechnung der Fakultät angeführt: zu einer Zahl n soll n!, das Produkt aller natürlichen Zahlen von 1 bis n, berechnet werden. – Nachfolgend ein Programmlisting hierzu. |  |  |  |  |
_________________ Ford Prefect
Never purchase release 1.x ! (ancient programmer's wisdom) "Don't argue with idiots. They'll drag you down to their level and then beat you with experience."
|
| Mon Aug 11, 2008 3:47 am |
|
 |
|
sunnyslope
Rookie
Joined: Sat Aug 09, 2008 7:18 pm Posts: 17
|
 Re: is it possible to forward declare a struct
The example you provided is an example of a forward declared function, which RobotC does support. It should follow that structs can be forward declared as well.
|
| Mon Aug 11, 2008 4:08 am |
|
 |
|
Ford Prefect
Senior Roboticist
Joined: Sat Mar 01, 2008 12:52 pm Posts: 936 Location: a small planet in the vicinity of Beteigeuze
|
 Re: is it possible to forward declare a struct
hi! ...no, this example I provided does NOT run with RobotC. If you write the source a little more RobotC-like (and a little less C-like) in order to get rid of the error messages, such as: and you start the program, you get the output: which obviously is nonsense (as well known, it's 120). It's simply because RobotC doesn't call the forward declarated function correctly and because of this there won't be any a recursive call of "fakultaet" by itself! and if you change the source to RobotC completely crashes!
_________________ Ford Prefect
Never purchase release 1.x ! (ancient programmer's wisdom) "Don't argue with idiots. They'll drag you down to their level and then beat you with experience."
|
| Mon Aug 11, 2008 5:31 am |
|
 |
|
sunnyslope
Rookie
Joined: Sat Aug 09, 2008 7:18 pm Posts: 17
|
 Re: is it possible to forward declare a struct
That's interesting. I guess there is some bug with recursive function calls in RobotC. In any case, it still supports the forward declaration of the function so that something simple like this will work: //example void DisplayNumber(); task main { DisplayNumber(); } void DisplayNumber() { int n = 12345; nxtDisplayTextLine( 1, "%d", n); } At least the compiler recognizes and allows the forward declaration of functions, but obviously they should fix the bug in the recursion.. and allow forward declaration of structs as well 
|
| Mon Aug 11, 2008 11:06 pm |
|
 |
|
Dick Swan
Creator
Joined: Fri Feb 09, 2007 9:21 am Posts: 613
|
 Re: is it possible to forward declare a struct
Recursive functions are not supported in ROBOTC at present. The compiler has checks to detect recursion and generate error messages. Technically this has to do with use of internal statis variables related to procedures rather than storage on local stack. The lack of recursion is mentioned in the documentation. Althought it may be "well hidden". 
|
| Tue Aug 12, 2008 12:29 am |
|
 |
|
Dick Swan
Creator
Joined: Fri Feb 09, 2007 9:21 am Posts: 613
|
 Re: is it possible to forward declare a struct
Lack of forward declared "struct" is an oversight in the ROBOTC compiler implementation.
Forward declared struct, or to use the terminology in the C standard "incomplete" variable declarations is only required for pointer use. ROBOTC did not originally support pointers which partially explained why it was not implemented originally.
Forward declarations will be added to the list of new feature requests.
|
| Tue Aug 12, 2008 4:24 am |
|
 |
|
Dick Swan
Creator
Joined: Fri Feb 09, 2007 9:21 am Posts: 613
|
 Re: is it possible to forward declare a struct
Forward declarations of "struct" will be in release 1.41.
|
| Tue Aug 12, 2008 11:03 am |
|
 |
|
sunnyslope
Rookie
Joined: Sat Aug 09, 2008 7:18 pm Posts: 17
|
 Re: is it possible to forward declare a struct
Great! Any idea when 1.41 will be released?
|
| Wed Aug 13, 2008 1:22 am |
|
 |
|
Dick Swan
Creator
Joined: Fri Feb 09, 2007 9:21 am Posts: 613
|
 Re: is it possible to forward declare a struct
Should be available for testing in the next week or so.
|
| Wed Aug 13, 2008 1:46 am |
|
 |
|
elizabeth.mabrey
Expert
Joined: Sun Aug 19, 2007 2:43 pm Posts: 133 Location: New Jersey
|
 Re: is it possible to forward declare a struct
I am currently using 1.40. pointer to struct does not work. The following is a simple test: The following crash the firmware, and I need to remove the battery to revive:
|
| Sat Aug 16, 2008 9:32 am |
|
 |
|
Dick Swan
Creator
Joined: Fri Feb 09, 2007 9:21 am Posts: 613
|
 Re: is it possible to forward declare a struct
Structures can crash ROBOTC in 1.40. There is a compiler code optimization error in 1.40 where it incorrectly removes instructions that generate pointer variables. It is fixed in 1.41. I also think if you turn off code optimization -- by setting the "Code Generation Preferences" to "Debug" should (I think) avoid the issue.
You can set code generation to "Debug" mode either via the "Preferences" using the "Compiler" tab (you must be in "Expert" user level) or via the "Qucik Setup Preferences" in the "View" menu.
It's called "Debug" mode because this is best for single stepping program via the Debugger as the code optimizer will not do code re-ordering which can screw up the association with line numbers.
|
| Sat Aug 16, 2008 12:54 pm |
|
 |
|
mightor
Moderator
Joined: Wed Mar 05, 2008 8:14 am Posts: 2864 Location: Rotterdam, The Netherlands
|
 Re: is it possible to forward declare a struct
Is 1.41 released yet?  And if so, where is it? I don't see it in http://www.robotc.net/content/lego_down/downloads/Regards, Xander
_________________| Some people, when confronted with a problem, think, "I know, I'll use threads," | and then two they hav erpoblesms. (@nedbat)| My Blog: I'd Rather Be Building Robots| ROBOTC 3rd Party Driver Suite: [ Project Page]
|
| Sat Aug 16, 2008 2:19 pm |
|
|