Page 1 of 1
NXC: array init faulty if global, ok if it's local!
Posted: 23 Mar 2011, 21:12
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);
}
Re: NXC: array init faulty if global, ok if it's local!
Posted: 23 Mar 2011, 21:17
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.
Re: NXC: array init faulty if global, ok if it's local!
Posted: 23 Mar 2011, 21:21
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};
?
Re: NXC: array init faulty if global, ok if it's local!
Posted: 24 Mar 2011, 05:08
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:
Re: NXC: array init faulty if global, ok if it's local!
Posted: 24 Mar 2011, 05:43
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
Re: NXC: array init faulty if global, ok if it's local!
Posted: 24 Mar 2011, 05:48
by muntoo
Errr... nevermind what I said.