Dynamic array sizing ????

Hi,

Is it possible to dynamically size an array?

Here's the problem, I'm working with the BMP180 pressure sensor SparkFun Barometric Pressure Sensor Breakout - BMP180 - SEN-11824 - SparkFun Electronics. I've produced a library to manage the pressure readings, this includes loading the last X readings into an array, and then processing them as a group. The size of the array (i.e. X) is hardcoded into the library, if I want to change the size, I edit the library, re-compile, and load the sketch onto the arduino.

The problem is that my current project requires three identical BMP180 pressure sensors, so I need to create three different objects, each based on the same library. However, each different pressure module needs to work with a different number of samples i.e. a different size of array.

I've considered using three almost identical copies of the same library (each with a different array size), but that would waste memory and cause code-maintenance headaches. I've also considered moving the arrays outside of the library, but this would make the code very messy.

Currently the easiest way seems to be to size the array to be the maximum size required, and simple code the other two modules to ignore the extra samples that aren't required. This should give me the functionality I require, at the expense of having lots of extra memory wasted in over-sized arrays.

Do I have any other options?

thanks

You can declare an array with a variable as the dimension as in

  int arraySize = 10;
  int anArray[arraySize];

Could you apply this to your problem by passing the array size as a parameter to the library ?

You can malloc() the memory for each array based on a size passed as a parameter to the begin() function or similar.


Rob

passing the array size as a parameter to the library ?

My understanding is that array sizes have to be set at compile-time, rather than passed as parameters at run-time.

Fulliautomatix:

passing the array size as a parameter to the library ?

My understanding is that array sizes have to be set at compile-time, rather than passed as parameters at run-time.

What have you tried to test your understanding?

You can malloc() the memory for each array based on a size passed as a parameter to the begin() function or similar.

Hmmmm.... not come across this before. I feel a session of googling coming on....

Are you able to recommend an "idiots guide" to doing this?

What have you tried to test your understanding?

I tried passing the array size in as a parameter.

Fulliautomatix:

What have you tried to test your understanding?

I tried passing the array size in as a parameter.

And . . ?

Are you able to recommend an "idiots guide" to doing this?

Yeah, idiot. Try google. 8)

int *stuff = (int *)malloc(numberOfElements * sizeof(int));

Fulliautomatix:

passing the array size as a parameter to the library ?

My understanding is that array sizes have to be set at compile-time, rather than passed as parameters at run-time.

Prior to C99C yes it was. eg ANSI C89. if I recall right...