Page 2 of 2

Re: Pointers in NXC

Posted: 03 Dec 2010, 21:31
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.

Re: Pointers in NXC

Posted: 03 Dec 2010, 22:19
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++.

Re: Pointers in NXC

Posted: 04 Dec 2010, 05:24
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

Re: Pointers in NXC

Posted: 04 Dec 2010, 05:30
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.

Re: Pointers in NXC

Posted: 04 Dec 2010, 06:05
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.

Re: Pointers in NXC

Posted: 04 Dec 2010, 07:21
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

Re: Pointers in NXC

Posted: 05 Dec 2010, 06:09
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

Re: Pointers in NXC

Posted: 05 Dec 2010, 06:34
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

Re: Pointers in NXC

Posted: 05 Dec 2010, 21:07
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