View unanswered posts | View active topics It is currently Tue Oct 21, 2014 2:20 pm






Reply to topic  [ 13 posts ] 
Pointers to functions 
Author Message
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3225
Location: Rotterdam, The Netherlands
Post Pointers to functions
Hey there,

I am looking for a way to have pointers to functions so I can stick 'em in an array. I know this is possible in regular C, however, before I go down that road, does RobotC have support for this?
I'd like to make a generic framework for subsumption based robot "brains", unfortunately the way I had envisioned that would involve said pointers.

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 Oct 04, 2008 2:38 pm
Profile WWW
Expert
User avatar

Joined: Sat Sep 20, 2008 12:16 pm
Posts: 106
Location: Curently fighting aliens on P3X-828
Post Re: Pointers to functions
Yes, and you can also use functions that are in header files like both C and C++ (First programing language that I learned)

_________________
:downloading:
Sorry, your program could not be downloaded. Please reboot your computer.
FTC Team #2959 Team Smash Bros CLICK HERE.(or click www)
Quote:
What the world needs is more geniuses with humility, there are so few of us left.

Agreed


Sat Oct 04, 2008 3:32 pm
Profile WWW
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3225
Location: Rotterdam, The Netherlands
Post Re: Pointers to functions
If pointers to functions are supposed to work, then why does the following piece of code barf when compiled:
Code:
void func(int _arg);

task main(){
   // pointer to a function that takes an int as an argument
   // and has no return value
   void (*fp)(int);

   // assign the address of the 'func' routine to fp
   fp = func;

   // call the 'func' routine using the standard methods
   (*fp)(1);
   fp(2);
}

void func(int _arg){
   nxtDisplayString(2, "arg: %d", _arg);
}

If RobotC supported pointers to functions, this would work. More info here: http://publications.gbdirect.co.uk/c_bo ... nters.html.

Regards,
Xander

This is the compile error:
**Error**:Unexpected scanner token-> '('
**Error**:Unexpected scanner token-> '*'
**Error**:Undefined variable 'fp'. 'short' assumed.
**Error**:Missing ';' before ')'
**Error**:Unexpected scanner token-> ')'
**Error**:Missing 'Identifier' before ')'
**Error**:Variable 'fp' is subsequently used as a procedure
*Warning*:Meaningless statement -- no code generated
*Warning*:Invalid '=' operation for types 'short' and 'void'
**Error**:Unexpected scanner token-> '('
**Error**:Unexpected scanner token-> '*'
**Error**:Missing ';' before ')'
**Error**:Unexpected scanner token-> ')'
**Error**:Unexpected scanner token-> '1'
*Warning*:Meaningless statement -- no code generated
**Error**:Variable 'fp' is not a Procedure.

_________________
| 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 Oct 04, 2008 3:41 pm
Profile WWW
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post Re: Pointers to functions
Pointers to functions do not work!

There is an alternative implementation that is a little less elegant that might work for you. Instead of storing a function pointer store a numerical index. Then where you would normally "execute" the function, you would call a function that does on a switch on this index and it calls the appropriate function.

Another limitation within ROBOTC that you might run into is that it does not support recursive function calls. I'm not sure if this is a problem with a subsumption architecture.


Sun Oct 05, 2008 7:06 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3225
Location: Rotterdam, The Netherlands
Post Re: Pointers to functions
Dick,

Are there any plans to include pointers or recursion as part of the functionality offered by RobotC?

I guess RobotC is slowly growing to be a more advanced platform, not just reserved for people wanting to learn C, but also for people who want to do more advanced things. Perhaps you could make this kind of functionality part of the advanced settings, thereby hiding it from the novices.

I used to run from pointers, but now I kind of miss them :) My guess is you will make Ford Prefect a very happy man if you give him recursion, then he can finally calculate his Fibonacci numbers!

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]


Sun Oct 05, 2008 7:34 am
Profile WWW
Expert
User avatar

Joined: Fri Nov 09, 2007 4:51 am
Posts: 121
Location: Hungary, Europe
Post Re: Pointers to functions
mightor,

I can see your point but RobotC is designed to control robots. Designers of RobotC have limited resources. They might work on pointers to functions, or recursion -- or they might work on support of new sensors, more stable bluetooth, better documentation for beginners and intermediate users etc etc.

I can imagine that RobotC might come with somethink like call to a native machine code subroutine in which we can do really CPU-intensive tasks with recursion etc. Written in machine code, like in the good old days of Commodore VC 20 :)

L


Mon Oct 06, 2008 1:36 pm
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: Pointers to functions
elemes, in the last time you haven’t really made any a positive contribution -
just try to think more positive!

Or are you paid by RobotC to argue-down any wishful improvements?


(I hope my anglicisms are understandable)

_________________
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 Oct 06, 2008 2:13 pm
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post Re: Pointers to functions
Some history on ROBOTC development explains some of the current limitations.

The NXT is the most powerful platform available to end users for ROBOTC. [It runs on other non-public platforms as well). The NXT has a 32-bit CPU and relatively lots of memory.

ROBOTC also runs on small 8-bit CPY platforms with 2K of RAM and 32K of flash.

Some of the CPUs (those from Microchip and Atmel) are Harvard architectures which means separate data busses for RAM and RON/Flash with separate instructions for RAM access and ROM/Flash access. [This is an oversimplification but satisfactory for here]. This introduces a lot of complexity to the ROBOTC VM for pointers when compared with architectures where RAM and ROM are in a single linear address space; you have two different pointer types requiring separate instructions to access.

Originally:
[list=]
[*]All platforms had nearly identical features
[*]16-bit was the standard for "int"
[*]Pointers all fit within 16-bit.
[*]char variables were not supported. Or were treated as 16-bit short.
[/list]


The vast majority of end users did not experience any limitations with these restrictions.

'char' size variables have been added.

Data pointers have been added. I expect there are some latent bugs someone, but none that are reported and unresolved. The data pointers are implemented as 16-bit variables. This required a bit of a kludge in that 'const' data variables are located in RAM. Their initial values are stored in flash but on program startup these are all copied to RAM.

ROBOTC programs are stored in flash and the instruction set is designed so that they are relocatable without any address fixup.

Both of the above two points are not conducive to "function" pointers.
  • Theye require 32-bit pointers
  • Function addresses are not compile time constants. They must be determined dynamically at run-time.
Function pointers are currently a low-priority for implementation.

For a similar set of "reasons", temporary variables and function parameters are statically allocated and not stored in a stack. This is why recursion is not available. This limitation impacts very few end users. It is a restriction that would be nice to limit and it is a low priority background development item.

Even with the above, you'll still find ROBOTC is the most feature rich of the available development environments for the NXT>


Mon Oct 06, 2008 3:13 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3225
Location: Rotterdam, The Netherlands
Post Re: Pointers to functions
Dick,

Thanks for taking the time to explain the reasons behind some of the limitations. It makes a lot more sense now.

Sooooo....when did you say they'd be implemented? *grin*

Just kidding :) Keep up the great work you guys are doing on RobotC.

Xander

PS: Elemes, I know all about software and systems design and how resources are (almost) always limited to some degree. I've been a Linux software and systems project engineer for over 14 years now. However, I also know that as long as you have customers, you will have feature requests. It's all part of the game called software development. In the end it is all about priorities and you can only ever have one number one priority, despite what your customers might think!

_________________
| 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]


Mon Oct 06, 2008 3:49 pm
Profile WWW
Professor

Joined: Fri Sep 19, 2008 1:22 am
Posts: 200
Post Re: Pointers to functions
I can tell you all that, through many years of experiance, Dick is the most responsive and reasonable retail* compiler writer that I've ever reported bugs to.

Dick even promised to make Tab autocomplete intrinsics for me in the next release. I just couldn't train my fingers to use Enter after all these years...

* I had to add "retail" so that I didn't make a good friend mad at me. (Hi Jim!)

_________________
Jeff McBride
Benson Robotics Club


Mon Oct 06, 2008 4:26 pm
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: Pointers to functions
Code:
 Dick is the most responsive and reasonable retail* compiler writer that I've ever reported bugs to.

I can copy this.

To be honest, he's the only one;)

_________________
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 Oct 06, 2008 4:48 pm
Profile
Rookie

Joined: Wed Nov 12, 2008 11:27 am
Posts: 3
Post Re: Pointers to functions
Auto-complete on Tab would be my most desired fix in next release.

Sorry if this is already done.


Wed Nov 12, 2008 11:30 am
Profile
Professor

Joined: Fri Sep 19, 2008 1:22 am
Posts: 200
Post Re: Pointers to functions
gravyuk wrote:
Auto-complete on Tab would be my most desired fix in next release.

Sorry if this is already done.


I asked for this some time back and Dick added it right away. I believe it went in for 1.44. The current version is 1.46 and I believe that 1.47 will be out soon.

_________________
Jeff McBride
Benson Robotics Club


Wed Nov 12, 2008 1:30 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 13 posts ] 

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.