Page 2 of 2
Re: NXC - addressOf and pointers
Posted: 12 Jan 2012, 22:33
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?
Re: NXC - addressOf and pointers
Posted: 15 Jan 2012, 03:03
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.
Re: NXC - addressOf and pointers
Posted: 15 Jan 2012, 09:24
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
Re: NXC - addressOf and pointers
Posted: 21 Jan 2012, 02:10
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.