Pointers in NXC

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
vogtnich
Posts: 3
Joined: 02 Dec 2010, 23:54

Pointers in NXC

Post 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
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: Pointers in NXC

Post 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.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
vogtnich
Posts: 3
Joined: 02 Dec 2010, 23:54

Re: Pointers in NXC

Post 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?
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Pointers in NXC

Post 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.
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: Pointers in NXC

Post 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
| My Blog: I'd Rather Be Building Robots (http://botbench.com)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Pointers in NXC

Post by HaWe »

just recalling... there should be also sort of a native embedded ARM7 C compiler as well... :geek:
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: Pointers in NXC

Post 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
| My Blog: I'd Rather Be Building Robots (http://botbench.com)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
markcrosbie
Posts: 34
Joined: 30 Sep 2010, 09:56
Location: Ireland
Contact:

Re: Pointers in NXC

Post 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
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Pointers in NXC

Post 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
Last edited by HaWe on 03 Dec 2010, 12:36, edited 1 time in total.
markcrosbie
Posts: 34
Joined: 30 Sep 2010, 09:56
Location: Ireland
Contact:

Re: Pointers in NXC

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests