Page 1 of 2

Pointers in NXC

Posted: 03 Dec 2010, 00:27
by vogtnich
Is it true that NXC doesn't have pointers at all? What about dynamic arrays? I wanted to use both of these in a program for an NXT robot. It seems like NQC has them, but it doesn't have the NXT methods l was already using (MotorRotationCount() for example). Is there any way to use pointers etc. with an NXT brick?

thanks

Re: Pointers in NXC

Posted: 03 Dec 2010, 01:11
by muntoo
vogtnich wrote:Is it true that NXC doesn't have pointers at all?
Not really. The closest you can get are addressOf[Ex](), SysIOMapReadByID(), and SysIOMapWriteByID().
vogtnich wrote:What about dynamic arrays?
What NXC doesn't have are static arrays. ;)

Code: Select all

long myArr[];
unsigned int elements = 10;
ArrayInit(myArr, 42, elements);
if(myArr[9] != 42)
    TextOut(0, 0, "42 is not the answer!", 0);
vogtnich wrote:Is there any way to use pointers etc. with an NXT brick?
You could try other programming languages. One I believe has pointers is RobotC ($30), but others (like nxtOSEK or Lejos) may have them, too.

Re: Pointers in NXC

Posted: 03 Dec 2010, 03:40
by vogtnich
Thanks for the reply.

So I can use unsigned long as a pointer variable for any object using addressOf? That sounds like it will work.

The other thing I need is something like C++'s new operator to allocate space for new instances of structs. I guess C has malloc and free but I've never used them before. Does NXC have something similar?

Re: Pointers in NXC

Posted: 03 Dec 2010, 10:09
by HaWe
One I believe has pointers is RobotC
I don't think so. IIRC RobotC has got neither pointers, nor dynymic memoy allocation, nor recursions, so ist's ANQCLNXC (also not quite C like NXC) and far, far away from ACDC - (Almost Completely Disputably C)
;)
but in earnest, AFAIK only with nxtOSEK or Trampoline you may have full C/C++ functionality. Correct me if I'm wrong, but the reason might be that neither NXC nor RobotC got a stack or a heap, but just a clump, different from both OSEK fw implementations.

Re: Pointers in NXC

Posted: 03 Dec 2010, 10:36
by mightor
Nope, no pointers in ROBOTC.

If you want full pointer and recursion support, use a platform that uses GCC as its compiler, like nxtOSEK, NXOS and trampoline.

- Xander

Re: Pointers in NXC

Posted: 03 Dec 2010, 10:54
by HaWe
just recalling... there should be also sort of a native embedded ARM7 C compiler as well... :geek:

Re: Pointers in NXC

Posted: 03 Dec 2010, 11:33
by mightor
Yeah, there's IAR's Workbench for ARM. There's a free NXT version that is limited to 128K code (this does not include variable space, just actual executable code). This is what John Hansen uses for his enhanced firmware.

NXTGCC is another project that can be used to compile native ARM stuff. I believe it is possible to compile the standard firmware with this as well.

IAR Workbench for ARM: http://www.iar.com/website1/1.0.1.0/1483/1/
NXTGCC: http://nxtgcc.sourceforge.net/

- Xander

Re: Pointers in NXC

Posted: 03 Dec 2010, 11:35
by markcrosbie
So let's take a step back and ask: what exactly is the problem you are trying to solve? Do you need to manage dynamic data? Why are you sure that you need pointers and malloc/free? Think about it: you can actually implement your own equivalent malloc/free/pointer concept in NXC by allocating a large static array and then defining a data structure to fit inside it. Define functions to insert/search/delete/update data and then hide the implementation of the dynamic aspect from your caller. You don't get language support for pointers and dynamic memory, but you achieve the same end result. After all, the RAM in the next is a static linear array of bytes, and pointers and dynamic memory are merely a language abstraction provided by C to shoot yourself in the foot.

Take a read of http://en.wikipedia.org/wiki/Dynamic_memory_allocation and possibly consider implementing a simple fixed-size memory block allocator that carves a large array into N chunks each of size X bytes. You might want to look into http://en.wikipedia.org/wiki/Memory_pool Memory Pools as a suitable simple solution for what you are trying to achieve.

What type of data are you trying to store dynamically? Have you investigated other data structures that could provide the equivalent expressive power? Do you understand what hash tables, trees, tries, linked lists, priority queues and graphs are? Would one of these data structures solve your problem? If the answer is yes then you can easily search online for example code to implement a data structure given a static fixed array of bytes. See Donald Knuth's classic book "The Art of Computer Programming" to see how to solve these problems old-school: http://en.wikipedia.org/wiki/The_Art_of ... rogramming. It might also be worth reading "Programming Pearls" by Jon Bentley to learn how to think about solving problems efficiently in both space and time.

So take some time, read and learn from the above and let us know how you get on!

I'll get off my soapbox now...

Regards,
Mark

Re: Pointers in NXC

Posted: 03 Dec 2010, 11:40
by HaWe
you can actually implement your own equivalent malloc/free/pointer concept in NXC by allocating a large static array

CMIIW but AFAIK NXC has no static arrays but only dynamic arrays and IIRC there are some problems using arrays within structures or structures within arrays
(having poiners and malloc was a wish also of me, a long, long time ago in a galaxy far, far away...)
and pointers and dynamic memory are merely a language abstraction provided by C to shoot yourself in the foot
better to shoot myself once in 1 foot than tying my brain round all my hands and feet struggling with objects and methods and throwing exceptions round implementations and classes and instances...(yes, you're right assuming that I really LOVE Java...)
:)
edit: sry, forgot about extensions :D

Re: Pointers in NXC

Posted: 03 Dec 2010, 12:07
by markcrosbie
Doc,

A standard homework in any operating-systems class is to implement a dynamic memory manager for an OS given a large fixed size array of bytes (also known as RAM). There are also quite a few examples of dynamic memory management libraries for embedded or real-time systems on the internet. I don't have time to search for them now, but porting one to the NXT would be a fun challenge.

When I say map your data structure into an array I am not talking about the literal use of structs and arrays in NXC, I mean in your logical design for your data structure you lay out how your data is formatted given a linear fixed-size array of bytes. Once you understand your data model you then specify an interface to access it, followed by the implementation underneath. Standard programming practice.

What I'm driving at is: understand the problem you are trying to solve and select the appropriate data structure before demanding that a language supports a specific syntactical and semantic model. There are many languages that have no malloc/free/pointers and are computationally complete.

Regards,
Mark