Pages: [1]   Go Down
Author Topic: quick question about declaring an array  (Read 793 times)
0 Members and 1 Guest are viewing this topic.
California
Offline Offline
Sr. Member
****
Karma: 3
Posts: 433
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If I declare an array inside of my void loop(), can I change the number of items each time the loop starts over? I've only ever programmed in visual basic, and the array's there were much easier, i didn't have to worry about declaring its size.

And speaking of that, can I change the size of an array if its already declared?

Thanks
Logged

California
Offline Offline
Sr. Member
****
Karma: 3
Posts: 433
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ugh, alright so upon trying it out, I seem to have found that the size of an array must be constant. Is there any way around this other than to remember how many values I want to use and ignoring the rest?
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
, I seem to have found that the size of an array must be constant
A static or global one, maybe, but not an automatic one.
Code:
void test (int a)
{
  int array [a];
}
Logged

Per Arduino ad Astra

California
Offline Offline
Sr. Member
****
Karma: 3
Posts: 433
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh okay, I just stupidly declared the array twice, trying to move it. So this is okay? :

Code:
void loop() {
Fade = map(AnalogInputValue,0,1023,0,Size);
int Array[Fade];
}

Is it okay if this value is different every time it runs the loop?
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As long as Size is not too big for the stack (because that's where the array will be put), yes.

Of course, you should put it on the heap (use malloc) but don't forget to free it when you've finished with it.
« Last Edit: February 26, 2011, 06:04:43 am by Groove » Logged

Per Arduino ad Astra

California
Offline Offline
Sr. Member
****
Karma: 3
Posts: 433
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So I can't just declare it at the beginning of the loop? I have to use Malloc? Is the array freed at the end of the function automatically?

What I mean is, at the end of the function, Im done with the array. I entirely refill the array when the function starts again. So what do I need to do to free it at the end of the function?
« Last Edit: February 26, 2011, 06:09:11 am by stoopkid » Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, you don't have to use malloc, but it is better if you do.
Quote
So what do I need to do to free it at the end of the function?
Call "free".
Logged

Per Arduino ad Astra

California
Offline Offline
Sr. Member
****
Karma: 3
Posts: 433
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

okay. thanks
Logged

Pages: [1]   Go Up
Jump to: