muntoo wrote:Code: Select all
const int fsize = 1024;
int * ptr = &fsize;
*ptr = -1;
std::cout << "This should say -1: " << fsize << std::endl;
NOTE: This is C++, not NXC. NXC currently has no support for pointers, and according to John Hansen, probably never will.
This raises an error in my C++ compiler, ptr needs to be const int *ptr since you are assigning it to address containing a const int. But doing this will invalidate the next line...
I have seen this example before, so I guess this was fixed in a newer version of the spec?
And since NXC compiles into NXT standard firmware bytecode which doesn't support pointers or anything similar, support will not come without a firmware change.
muntoo wrote:nxt-ai wrote:So isn't #define faster if its hard coded versus an unchangeable variable?
Not necessarily. Might be, might not, I'm not sure about the details. If the compiler is good, it'll automatically determine this for you.
There should be no speed difference if the compiler is optimizing properly as it needs to convert the #define into a variable in the end anyway (when using the .rxe format). However the NXC compiler just doesn't optimize the code very well, in fact it struggles with just creating clean code...
I mentioned it a long time ago (perhaps on nxtasy.org, don't remember), but using constant variables instead can cause speed, executable size and memory usage to worsen a bit. (depends a lot on the actual case though.) However I wouldn't be surprised if there are also cases where #define is less efficient...
But if you really care about efficiency, program in NBC, NXC compilation just is too inefficient... So using const is probably a better idea.
@nxt-ai,
I don't really know, but I think the error might be with connecting and transferring the program to the NXT and not the NXC code.