Add elements to an array dynamically?

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
rizla420
 
Posts: 332
Joined: Thu Feb 05, 2009 9:03 pm

Add elements to an array dynamically?

Post by rizla420 »

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?

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

Re: Add elements to an array dynamically?

Post by adafruit »

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

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

Re: Add elements to an array dynamically?

Post by rizla420 »

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?

nemesisrobot
 
Posts: 25
Joined: Tue Jan 01, 2008 3:27 am

Re: Add elements to an array dynamically?

Post by nemesisrobot »

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

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

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

Re: Add elements to an array dynamically?

Post by rizla420 »

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

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.

nemesisrobot
 
Posts: 25
Joined: Tue Jan 01, 2008 3:27 am

Re: Add elements to an array dynamically?

Post by nemesisrobot »

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.

sigflup
 
Posts: 44
Joined: Tue Mar 31, 2009 8:50 pm

Re: Add elements to an array dynamically?

Post by sigflup »

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

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

int *array1;
instead. Most compilers don't distinguish the two but I wouldn't be surprised if some do.

User avatar
zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Re: Add elements to an array dynamically?

Post by zener »

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

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”