NXC - addressOf and pointers

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC - addressOf and pointers

Post by HaWe »

what is the advantage of doing it this way (which appears to be very complicated and slow) instead of using the pseudo-pass-by-reference method?
tabbycatrobots
Posts: 100
Joined: 27 Dec 2010, 19:10

Re: NXC - addressOf and pointers

Post by tabbycatrobots »

The advantage - no code advantage - but I think in 'C'. If all you have is a hammer - everything looks
like a nail. But I did get out my C++ books tonight to try to understand pass by reference. I will try it.
But, a question - in NXC, will this pass by reference work for variables local to a function, or only for
global variables? Thanks.
Regarding the comment about a second routine accessing a variable while the first routine might be
modifying it, I'm careful about how any globals are shared between tasks, and in my robots so far, I
haven't had 2 or more tasks doing similar jobs where they might use the same functions.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC - addressOf and pointers

Post by HaWe »

it works for local and global variables from one task or function to any other function (the variables you pass by reference to the destination function of course are local in here).

I am always working with lots of tasks (10- 20) - be assured that there are ways to protect variables from being prematurely overwritten, so don't mind about that for the moment.

And don't mind that "pass-by-reference" actually works completely different in NXC, don't ask why and simply keep your eyes shut, just consider it to be a black box which appears to work like the "by-reference" way although it actually doesn't.
Umm - better forget my last sentence as well. :D
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: NXC - addressOf and pointers

Post by muntoo »

doc-helmut wrote:Although muntoo tried a little around faking pointers by arrays, but IIRC it was only tinkering^^ (no offense, muntoo ;) )
I did kind of get bored with it. :)
@OP Basically, I was doing:

Code: Select all

byte stack[20000];
ID p = stack_malloc(100 * sizeof byte);
But a bit more complicated than that.
Image

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


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests