Pointers to functions
Page 1 of 1

Author:  mightor [ Sat Oct 04, 2008 2:38 pm ]
Post subject:  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.


Author:  CC335 [ Sat Oct 04, 2008 3:32 pm ]
Post subject:  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)

Author:  mightor [ Sat Oct 04, 2008 3:41 pm ]
Post subject:  Re: Pointers to functions

If pointers to functions are supposed to work, then why does the following piece of code barf when compiled:
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

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

If RobotC supported pointers to functions, this would work. More info here: ... nters.html.


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.

Author:  Dick Swan [ Sun Oct 05, 2008 7:06 am ]
Post subject:  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.

Author:  mightor [ Sun Oct 05, 2008 7:34 am ]
Post subject:  Re: Pointers to functions


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!


Author:  elemes [ Mon Oct 06, 2008 1:36 pm ]
Post subject:  Re: Pointers to functions


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 :)


Author:  Ford Prefect [ Mon Oct 06, 2008 2:13 pm ]
Post subject:  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)

Author:  Dick Swan [ Mon Oct 06, 2008 3:13 pm ]
Post subject:  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.

[*]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.

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>

Author:  mightor [ Mon Oct 06, 2008 3:49 pm ]
Post subject:  Re: Pointers to functions


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.


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!

Author:  Jeff McBride [ Mon Oct 06, 2008 4:26 pm ]
Post subject:  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!)

Author:  Ford Prefect [ Mon Oct 06, 2008 4:48 pm ]
Post subject:  Re: Pointers to functions

 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;)

Author:  gravyuk [ Wed Nov 12, 2008 11:30 am ]
Post subject:  Re: Pointers to functions

Auto-complete on Tab would be my most desired fix in next release.

Sorry if this is already done.

Author:  Jeff McBride [ Wed Nov 12, 2008 1:30 pm ]
Post subject:  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.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group