NXC: array init faulty if global, ok if it's local!

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

NXC: array init faulty if global, ok if it's local!

Post by HaWe »

hi,
this code works:

Code: Select all

task main(){
  int  werte[8] = {1, 2, 3, 4, 5, 6, 7, 8};

  for (int i=0; i<8; i++) NumOut(0, i*8, werte[i]);

  while (true);
}

this doesn't (shows only 0's all over):

Code: Select all

int  werte[8] = {1, 2, 3, 4, 5, 6, 7, 8};

task main(){

  for (int i=0; i<8; i++) NumOut(0, i*8, werte[i]);

  while (true);
}
ronmcrae
Posts: 33
Joined: 28 Sep 2010, 14:56

Re: NXC: array init faulty if global, ok if it's local!

Post by ronmcrae »

The initialization should have either a size defined (i.e. [8]) OR it should have empty brackets [] and a list of elements to initialize the array. For the latter option the length of the list infers the size of the array.

Ron.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: array init faulty if global, ok if it's local!

Post by HaWe »

thx, you're right, this works:

Code: Select all

int  werte[] = {1, 2, 3, 4, 5, 6, 7, 8};

task main(){

  for (int i=0; i<8; i++) NumOut(0, i*8, werte[i]);

  while (true);
}
But this does work as well if it's defined local
int werte[8] = {1, 2, 3, 4, 5, 6, 7, 8};
?
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: NXC: array init faulty if global, ok if it's local!

Post by muntoo »

doc-helmut wrote:But this does work as well if it's defined local
int werte[8] = {1, 2, 3, 4, 5, 6, 7, 8};
?
'Cause, IIRC, John doesn't want to implement:

Code: Select all

int foo[8];

task main()
{
    //...
}
Image

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


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: array init faulty if global, ok if it's local!

Post by afanofosc »

I don't understand muntoo's comment. You can do what he showed in his example. It is correctly initialized to 8 zeros.

What Doc has experienced is a bug/known issue. If you specify a size for an array it generates code to initialize the array to N zeros or empty elements of whatever type your array is. If you also provide an initializer in braces then it acts differently depending on whether it is a global or a local. If it is a global then the array is initialized via static values in the dataspace portion of the RXE. Locals need to be initialized at run time.

Bottom line is I need to fix this and will try to do so in the next release. For now, if you provide an initializer then leave off the size. That's standard C syntax and it works right in NXC. Using both an initializer and a size in the brackets is also standard C syntax but it does not work right in NXC for global variables so avoid this syntax in this situation.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: NXC: array init faulty if global, ok if it's local!

Post by muntoo »

Errr... nevermind what I said.
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: Semrush [Bot] and 0 guests