0

Add elements to an array dynamically?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Add elements to an array dynamically?

by rizla420 on Wed Feb 18, 2009 12:23 am

Is it possible to create an empty array variable that will grow dynamically?

I tried to create an array like so:

int array1[];
in array1_size;


then in the loop see what the length of the array is:

size = sizeof(array1) / sizeof(int);

I'm getting the following error when i compile: "storage size of 'array1' isnt' known"

THe reason i'm trying to use an array is because I dont know what the potential values are, so i want to create the array as a memory store.

Also, can you do string concatenation with arduino?

rizla420
 
Posts: 332
Joined: Thu Feb 05, 2009 9:03 pm

Re: Add elements to an array dynamically?

by adafruit on Wed Feb 18, 2009 12:29 am

not -really- theres very little memory available but you may be able to use malloc
id google arduino and malloc see if theres anything

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

Re: Add elements to an array dynamically?

by rizla420 on Wed Feb 18, 2009 12:35 am

ladyada wrote:not -really- theres very little memory available but you may be able to use malloc
id google arduino and malloc see if theres anything



I figured it had to be a hardware limitation issue.. its not like this little board is a full fledged computer.. oh well time to think of another way to skin this cat. Thanks for the reference to look into further.

One other question, by declaring an array as an INT. Does that mean all the variables in the array are individual Integers?

rizla420
 
Posts: 332
Joined: Thu Feb 05, 2009 9:03 pm

Re: Add elements to an array dynamically?

by nemesisrobot on Wed Feb 18, 2009 6:53 pm

rizla420 wrote:One other question, by declaring an array as an INT. Does that mean all the variables in the array are individual Integers?


Yep.

Also, it seems there's a existing library for dynamic arrays
Code: Select all | TOGGLE FULL SIZE
http://www.arduino.cc/playground/Code/DynamicArrayHelper


Doesn't look very friendly though imo. Anyways, it makes use of malloc/free, so at least now you know it's possible =D
nemesisrobot
 
Posts: 25
Joined: Tue Jan 01, 2008 3:27 am

Re: Add elements to an array dynamically?

by rizla420 on Wed Feb 18, 2009 7:03 pm

nemesisrobot wrote:
rizla420 wrote:One other question, by declaring an array as an INT. Does that mean all the variables in the array are individual Integers?


Yep.

Also, it seems there's a existing library for dynamic arrays
Code: Select all | TOGGLE FULL SIZE
http://www.arduino.cc/playground/Code/DynamicArrayHelper


Doesn't look very friendly though imo. Anyways, it makes use of malloc/free, so at least now you know it's possible =D



Thanks for the tip, but I think i'll stick with finding a workaround. Maybe just declaring an array size that I think will be the max amount of values i can think will be necessary.. no more than a 15 value array i assume.

rizla420
 
Posts: 332
Joined: Thu Feb 05, 2009 9:03 pm

Re: Add elements to an array dynamically?

by nemesisrobot on Thu Feb 19, 2009 2:28 am

Sounds good to me. Dynamic memory would only have been better if you really didn't know how much to allocate at runtime anyway, and if there was a large range of possible sizes.
nemesisrobot
 
Posts: 25
Joined: Tue Jan 01, 2008 3:27 am

Re: Add elements to an array dynamically?

by sigflup on Wed Apr 01, 2009 5:30 pm

Maybe just declaring an array size that I think will be the max amount of values i can think will be necessary.


Sounds good to me. Your compiler just wants to know the array size so it doesn't clobber it's contents with other things. If you declare:
Code: Select all | TOGGLE FULL SIZE
int array1[MAX_SIZE];


you don't need malloc or any fancy stuff like that. If you insist on using malloc or you know an area of memory that you believe will be free you might want to declare

Code: Select all | TOGGLE FULL SIZE
int *array1;


instead. Most compilers don't distinguish the two but I wouldn't be surprised if some do.
sigflup
 
Posts: 46
Joined: Tue Mar 31, 2009 8:50 pm

Re: Add elements to an array dynamically?

by zener on Wed Apr 01, 2009 10:15 pm

I don't program C (yet) so I probably shouldn't comment, but could you use a case statement?

zener
 
Posts: 4429
Joined: Sat Feb 21, 2009 2:38 am

Please be positive and constructive with your questions and comments.