View unanswered posts | View active topics It is currently Wed Aug 20, 2014 8:23 pm






Reply to topic  [ 19 posts ]  Go to page 1, 2  Next
is it possible to forward declare a struct 
Author Message
Rookie

Joined: Sat Aug 09, 2008 7:18 pm
Posts: 17
Post 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
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 614
Post 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
Profile
Rookie

Joined: Sat Aug 09, 2008 7:18 pm
Posts: 17
Post 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
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 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.

Code:
/* fakultaet.c */
#include <stdio.h>

long fakultaet(long);

void main(void)
{
   long n=5;
   printf("\nDie Fakultät von %ld ist %ld.\n",n,fakultaet(n));

} /* end main */

long fakultaet(long n)
{
   if (n>1)
      return n*fakultaet(n-1);

   return 1;

} /* end fakultaet */

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Mon Aug 11, 2008 3:47 am
Profile
Rookie

Joined: Sat Aug 09, 2008 7:18 pm
Posts: 17
Post 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
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 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:

Code:
/* fakultaet.c */

#define println nxtDisplayTextLine

long fakultaet(long n);

task  main()
{
   long n=5;
   println(1,"Fakultaet von %d", n);
   println(2,"%d", fakultaet(n));

   while (true)  {}

} /* end main */

long fakultaet(long n)
{
   if (n>1)
      return n*fakultaet(n-1);

   return 1;

} /* end fakultaet */


and you start the program, you get the output:
Quote:
Fakultaet von 5
1

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
Code:
/* fakultaet.c */

#define println nxtDisplayTextLine

long fakultaet(long n);
long f;                            // <<<<<<<<<<<<<<<<<<<<<<<

task  main()
{
   long n=5;
   f=fakultaet(n);                   // <<<<<<<<<<<<<<<<<<<<<<<
   println(1,"Fakultaet von %d", n);
   println(2,"%d", f);               // <<<<<<<<<<<<<<<<<<<<<<<

   while (true)  {}

} /* end main */

long fakultaet(long n)
{
   if (n>1)
      return n*fakultaet(n-1);

   return 1;

} /* end fakultaet */


RobotC completely crashes!

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Mon Aug 11, 2008 5:31 am
Profile
Rookie

Joined: Sat Aug 09, 2008 7:18 pm
Posts: 17
Post 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
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 614
Post 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
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 614
Post 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
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 614
Post 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
Profile
Rookie

Joined: Sat Aug 09, 2008 7:18 pm
Posts: 17
Post 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
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 614
Post 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
Profile
Expert

Joined: Sun Aug 19, 2007 2:43 pm
Posts: 136
Location: New Jersey
Post 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:

Code:
typedef struct {
 int   x1;
 int        x2;
 int    x3;
}xstruct;
xstruct XX;

task main()
{
    xstruct *xp;
    XX.x1 = 91;
    XX.x2 = 82;
    XX.x3 = 73;
    xp = &XX;
    nxtDisplayString(4, "%d", xp->x1);
    nxtDisplayString(5, "%d", xp->x2);
    nxtDisplayString(6, "%d", xp->x3);
    wait1Msec(10000);
}


The following crash the firmware, and I need to remove the battery to revive:
Code:
typedef struct {
 ubyte x1;
 ubyte  x2;
 ubyte  x3;
}xstruct;
xstruct XX;

task main()
{
    xstruct *xp;
    XX.x1 = 0xF;
    XX.x2 = 0x1F;
    XX.x3 = 0x2F;
    xp = &XX;
    nxtDisplayString(4, "%x", xp->x1);
    nxtDisplayString(5, "%x", xp->x2);
    nxtDisplayString(6, "%x", xp->x3);
    wait1Msec(10000);
}


Sat Aug 16, 2008 9:32 am
Profile WWW
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 614
Post 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
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3188
Location: Rotterdam, The Netherlands
Post Re: is it possible to forward declare a struct
Dick Swan wrote:
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.

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

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sat Aug 16, 2008 2:19 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 19 posts ]  Go to page 1, 2  Next

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.