Help with creating an array within a struct

I am implementing the Karplus-Strong algorithm in code. Instead of just making my code specific for my needs, I would really like to make it a library. In order to do so, I need to declare an array of a specific length when I call a new instance of my struct. In processing, I know I could so something like int[] yada = new int[foo]; but I don't think I am able to do such a thing in Arduino because of the way that memory is assigned. It isn't really an option to make an array that is large enough for all instances and then cut it down by assigning the shortened length to a variable MAX. The Karplus-Strong takes too much memory for that.

For those that wish to help me, my coding experience is as such: I have a lot of experience with Arduino and a little bit with Processing. I am wanting transition into actual C code without the Arduino IDE, but I am not quite that good yet. I can modify and understand raw C but I am not good enough to write it from scratch like I can in Arduino.

Help would be very appreciated!

What kind of numbers are we talking about (i.e., how many elements in the array)? You've only got 2Kbytes of RAM on an ATmega328 (and a chunk of that should be set aside for the stack) so this may all be a moot point.

In such a constrained-memory system you are right, you do not want to do dynamic memory allocation. But nor do you want to start implementing generic algorithms when a specific one is more reasonable.

-- Check out our new shield: http://www.ruggedcircuits.com/html/gadget_shield.html

int yada = new int[foo];

Unfortunately, the <new.h> library is not included in the arduino environment.

You can use the malloc() command (old-school), but it comes with a warning: This can be overwritten with stack information (or so I have been told).

The best thing to do is to reserve, say, 100 bytes, and govern your own memory allocation in that block.

There is no way arround learning C and a little bit of C++ when you want to use the Arduino efficiently...

Static arrays can of course be defined in structs, e.g.

struct {
  int anInt;
  byte anArray[9];
  } withArrays;
void loop() {
withArrays.anInt = withArrays.anArray[2];  
}

However take care of the little RAM in the megas. There is no compiler warning when you overdo, and the behavour is random and difficult to debug...

This still requires the array size to be known at compile time, no? I think the poster wanted to make a library that could dynamically allocate the array size at runtime.

-- Check out our new shield: http://www.ruggedcircuits.com/html/gadget_shield.html

You're right can't be done that way.

I had apparently tested against an C++0x compiler I'd been playing with.

I think the poster wanted to make a library that could dynamically allocate the array size at runtime.

That is correct; however, it seems like that will not be possible. I am creating arrays of 50 to 100 two-byte integers. My sketch will need 6 such arrays. Memory may be tight, but I think it is definitely do-able especially since I am running on an atmega328p. As to why I am creating these arrays of different sizes, the length of the array is dependent on both a sampling frequency and a fundamental note frequency for each guitar string that I am synthesizing. Those two values are to be specified by the user in the code. I was hoping for an easier way for the value to be specified than by modifying the library.

What you want can be done but it means using that scary malloc

// A quick n dirty example of a "dynamic array" the way it
// used to be done when men were men and c++ was a glimmer
// in Stroustrups demon eye. Ah gone are the days.

#include "stdlib.h"

struct {
  int anInt;
  byte *anArray;
} withArrays;

void setup() 
{
  Serial.begin(9600); 

  // we'll use this for the return status of malloc for no particular reason
  withArrays.anInt=0;

  if((withArrays.anArray = (byte*)malloc(10 * sizeof(byte))) == 0)
  {
    Serial.println("malloc failed");
    withArrays.anInt=1;  // set to failure
  }

  Serial.print("withArrays.anInt=");
  Serial.println(withArrays.anInt);     
  
  //populate our malloc'ed memory treating it as an array
  for (int i=0; i<10; i++)
  {
    withArrays.anArray[i]=i;
  }
  
  //print it out again
  for (int i=0; i<10; i++)
  {
      Serial.print(withArrays.anArray[i], DEC);
      Serial.print(":");
  }
  
  free(withArrays.anArray); //yeah you wish
  
  Serial.println("\nDone");     
} 

void loop() 
{ 
}

There is no way arround learning C and a little bit of C++ when you want to use the Arduino efficiently...

Static arrays can of course be defined in structs Code: struct { int anInt; byte anArray[9]; } withArrays;

That's not a static array, and most C compilers will complain about static arrays defined in a struct.

That's not a static array

I think to be fair deSilver was using the term static in the sense of being not dynamic. This is a bit of an old chestnut - allways a good one for the tea room - int *foo and int foo[] is another dynamic classic of course :)

In looking at malloc and free I at first thought they were poorly implemented on the arduino. I should know better but I based my thoughts on things like this: http://www.arduino.cc/playground/Code/AvailableMemory assuming that if it was in the playground it was likely correct - I won't make that mistake again. In fact this thread http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1231801806 lead to me take a bit more of a look myself at malloc and free and it appears that they are actually perfectly usable.