Pointers in NXC

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
m-goldberg
Posts: 73
Joined: 29 Sep 2010, 12:05

Re: Pointers in NXC

Post by m-goldberg »

markcrosbie wrote:There are many languages that have no malloc/free/pointers and are computationally complete.
Indeed, early Fortran, for example, was computationally complete although it's only data structure was the array and its only memory management tool the COMMON block (*). This may seem awkward by modern standards, but arrays and indexes can be used to build any data structure that can be built with pointers and offsets. In Standard C, this equivalence is explicit and easily demonstrated.

Code: Select all

// Declarations
a_type array[SIZE];
a_type * ptr = array;
a_type u, v, w;
// There are two ways to access an the item stored at index i.
u = array[i];
v = *(ptr + i);
// I have used C compilers that were perfectly happy to fetch an item at index i if you wrote
w = i[array];
(*) COMMON blocks were perhaps better bug production tools as they were memory management tools.
Regards, Morton
bullestock
Posts: 27
Joined: 29 Sep 2010, 19:34
Location: Denmark
Contact:

Re: Pointers in NXC

Post by bullestock »

m-goldberg wrote: // I have used C compilers that were perfectly happy to fetch an item at index i if you wrote
w = i[array];
Forgive me for being a language nerd, but I just have to point out that this is true for all C and C++ compilers. Array indexing is commutative in C/C++.
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: Pointers in NXC

Post by muntoo »

Maybe, if I can accumulate enough skills, I may be able to try implementing a memory manager in NXC...

Of course, it will be pretty sloooowww... for CPU-intensive tasks
Image

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


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
m-goldberg
Posts: 73
Joined: 29 Sep 2010, 12:05

Re: Pointers in NXC

Post by m-goldberg »

bullestock wrote: Array indexing is commutative in C/C++.
Yes, that was one of points I was making. A C compiler translates the expression *(ptr + i) to *(ptr + i * sizeof(a_type), and array into &array + i * sizeof(a_type). Addition being commutative, the gives &array + i * sizeof(a_type) = *(ptr + i * sizeof(a_type) = *(i * sizeof(a_type) + ptr) = i * sizeof(a_type) + &array. The last expression back translates into i[array] at the source code level. (The equal signs here are math equals, not C assignment operators.)
... that this is true for all C and C++ compilers

My comment on this behavior, which made it a matter personal observation, was worded the way it is because it being true for all C and C++ compilers is beyond my certain knowledge. I think what you say is true, but I'm unable to cite any standards document where it is confirmed.
Regards, Morton
vogtnich
Posts: 3
Joined: 02 Dec 2010, 23:54

Re: Pointers in NXC

Post by vogtnich »

That stuff about memory pools made me realize I could probably just use one big array after all. I wanted the pointers for a linked queue of search nodes, but making the queue a big array probably won't slow things down noticeably. My project is due in a few days, so I don't want to mess with the firmware or anything. I'd like to learn more about data abstraction etc. sometime though. Thanks for the helpful links.

Now I just have one simple problem. I can't figure out how to initialize arrays of structs.

Code: Select all

struct Location
{
  short value;
  unsigned short num;
  unsigned short paths[4][2];
  short x;
  short y;
};

Location map[23] =
{{0, 3, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, 12, 12},//Location 0
{0, 0, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, 0, 0}, //Location 1
{0, 0, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, 0, 0}, //Location 2
{0, 0, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, 0, 0}, //Location 3
{0, 0, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, 0, 0}, //Location 4
{0, 0, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, 0, 0}, //Location 5
{0, 0, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, 0, 0}, //Location 6
{0, 0, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, 0, 0}, //Location 7
{0, 0, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, 0, 0}, //Location 8
{0, 0, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, 0, 0}, //Location 9
{0, 0, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, 0, 0}, //Location 10
{0, 0, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, 0, 0}, //Location 11
{0, 0, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, 0, 0}, //Location 12
{0, 0, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, 0, 0}, //Location 13
{0, 0, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, 0, 0}, //Location 14
{0, 0, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, 0, 0}, //Location 15
{0, 0, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, 0, 0}, //Location 16
{0, 0, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, 0, 0}, //Location 17
{0, 0, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, 0, 0}, //Location 18
{0, 0, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, 0, 0}, //Location 19
{0, 0, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, 0, 0}, //Location 20
{0, 0, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, 0, 0}, //Location 21
{0, 0, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, 0, 0}}; //Location 22

task main()
{
  NumOut(0, 0, map[0].x);
  
  Wait(10000);
  
}
It compiles and looks good but it outputs 0 instead of 12.
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: Pointers in NXC

Post by mightor »

Code: Select all

It compiles and looks good but it outputs 0 instead of 12.
First thing that came to mind when I read that was "welcome to the world of pointers!"

If you are not using the enhanced firmware, you will find that anything but a flat, one dimensional array is going to fail. There is a bug in the standard firmware that prevents it from working with a 2D array.

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

Re: Pointers in NXC

Post by muntoo »

As the person who owns a hovercraft filled with eels ( :?: ) said, you need the Enhanced Firmware. The latest version should be located here: "C:\Program Files\BricxCC\lms_arm_nbcnxc_131.rfw"

Connect your NXT Brick (via USB), and Find Brick. Now, go to BricxCC.exe->Tools->Download Firmware, and do exactly what it says (*).

* especially if it suggests dying your hair pink
Image

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


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: Pointers in NXC

Post by mightor »

muntoo wrote:* especially if it suggests dying your hair pink
John must've added some randomisation in there because it told me to dye mine purple.

I have the latest stable release of BrixCC and that firmware file is not on my system. Perhaps an address where the latest version of BrixCC/NXC/Firmware zip file can be downloaded would be helpful :) For details on how to obtain and install the latest test release of NXC, please take a look at this thread: [LINK].


- 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)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Pointers in NXC

Post by afanofosc »

The problem with the code posted above appears to be that the compiler is not generating valid initialization code for the array of structures. Sadly, the firmware doe not support static initialization of arrays with more than one dimension (iirc, but I could be wrong since this is a very poorly documented and understood part of the NXT firmware). For sure, the compiler does not currently automatically generate code to dynamically initialize an array using code like what is written above. An array of struct that did not contain another array should, I believe, work just fine. The compiler also currently just ignores array sizes specified in structure declarations. The compiler may in the future automagically initialize instances of structures that contain arrays but for now you have to do that yourself explicitly for each instance of a structure containing arrays.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests