dynamic array allocation?

Hi!

So I posted earlier about defining a class within a sketch. I’ve totally been able to do it except for dynamic instancing the class into an array- so I resorted to using a fixed array length: ‘LedSprite LedArray[4];’ - that works (as a newbie workaround).

Now I am trying to dynamically define an array based on a parameter. This is for an LED sprite type class and in the code snippet below I try to define the length of an array for each instance. If I print out the values of the array from within the function, they are correct but when I try to use the array from another part of my program, the array values are garbage.

So I tried defining a fixed length of the array in my class definition that is larger than I will need - and that works fine.

My question is though: what is the “right” way to define an array based on a variable?

–Roy

void LedSprite:: setupLed(int a, int n){
  num = a;
  numLeds = n;
  ledGroup[n];
  int i;
  for (i = 0; i < n; i++){
    ledGroup[i] = a + i;
  active = false;
  moving = false;
};

Are you by any chance exceeding the 1Kb RAM limit ?

I don't think so (it's 7k on this ATmega8 board -afaik)

if I predefine the array length (when the var is first declared in the class def) > than I need, then all is well. I only get garbage values when i try to define it with a passed param.

I was thinking of defining it dynamically to save space :)

But the hard coded def is working so I will learn the "right" way further on down the road... I was just wondering

--Roy

To allocate a dynamically sized array, you need to use malloc() or calloc() (the latter initializes the values of the array elements to 0):

ledGroup = malloc(n * sizeof(int)); // replace int with whatever the type of the ledGroup's elements are.

ledGroup = calloc(n, sizeof(int)); // ditto

You can free the memory in a destructor for your class, although if it's used from the top level of a sketch, the variable will never go out of scope, so the memory won't be freed. (Which should be fine, since you want the LedSprite to be usable for the entire duration of the sketch's execution.)

The 7K is flashram for storing code. There’s only 1k om RAM for storing variables.

You can use flashram for variables with the PROGMEM statement but that will reduce memory available for code.

I don’t know where the code allocated with MALLOC comes from ?

The 7K is flashram for storing code.

Flash is "a specific type of EEPROM" rather than RAM.

You can use flashram for variables with the PROGMEM statement but that will reduce memory available for code.

You can only store constants with PROGMEM, nothing that changes at runtime. So this means you can store text or look-up arrays with PROGMEM, for example, but not anything that changes when the program runs like counters or buffers.

I don't know where the code allocated with MALLOC comes from ?

Because it's allocated dynamically it comes from the RAM.

Hope this helps clarify things... (Even if it doesn't solve the OP problem. :-) )

--Phil.

Ahaa. Thanks for making that clear.