struct and arrray size

Hey @all,

i would love to know how to set up a struct with some arrays.
the challenging is, that the array size is not know.
The Idea is as following:
there is this really cool MultiMap function (Arduino Playground - MultiMap)
and my idea was to build a struct that holds all needed things for a nice mapping:

struct sMultiMapStruct
 {
 word mm_In [];
 word mm_Out [];
 byte size;
 };
 
 typedef struct sMultiMapStruct tmm;

for testing:

/****************************************************************************************************
  *   structTest sketch
  *   
  *   
  *   Created:  22.06.2012
  *   copyright by Stefan Krueger
  *   written by Stefan Krueger code@s-light.eu
  *   
  *   Last modified: 22.06.2012 21:00
  *   By Stefan Krueger
  *   
  *   changelog / history :
  *
  *
****************************************************************************************************/


struct sMultiMapStruct
{
 word mm_In [];
 word mm_Out [];
 byte size;
};

typedef struct sMultiMapStruct multiMap_type;


multiMap_type mmMyMap = {
 {  0, 20,  30},
 {  0, 80, 100},
 3
};





/**
  * MultiMap
  * http://arduino.cc/playground/Main/MultiMap
  *  modified for 16bit word datatype
  **/
word multiMap(word val, word* _in, word* _out, uint8_t size)
{
  // take care the value is within range
  // val = constrain(val, _in[0], _in[size-1]);
  if (val <= _in[0]) return _out[0];
  if (val >= _in[size-1]) return _out[size-1];

  // search right interval
  uint8_t pos = 1;  // _in[0] allready tested
  while(val > _in[pos]) pos++;

  // this will handle all exact "points" in the _in array
  if (val == _in[pos]) return _out[pos];

  // interpolate in the right segment for the rest
  return map(val, _in[pos-1], _in[pos], _out[pos-1], _out[pos]);
}


/**  struct sMultiMapStruct used so you need no headerfile for struct definition **/
word mapIt(word wInputValue, struct sMultiMapStruct* mmTheMap)
{
 // '(*mmTheMap).mm_In' could be also written as 'mmTheMap->mm_In'
 return multiMap(wInputValue, (*mmTheMap).mm_In, (*mmTheMap).mm_Out, (*mmTheMap).size);
}

 
/***************************************************************************************************/
void setup()  
{
 /**************************************************/
 /* Welcom @ This Sketch!!                         */
 /**************************************************/
 Serial.begin(115200);
 Serial.println();
 Serial.println("       ");
 Serial.println("   oo  ");
 Serial.println("  (  ) ");
 Serial.println("   ^^  ");
 Serial.println();
 Serial.println("Welcome to structTest Sketch!");
 Serial.println("system is starting!");
 Serial.println(":-)");
 Serial.println("");
}

void loop()
{
 for ( word x = 0; x < 30; x++)
 {
 Serial.print(x);
 Serial.print("-->");
 Serial.println(mapIt(x, &mmMyMap ));
 //delay(10);
 if ( x == 20 )
 {
 Serial.println("*** 20 ***");
 delay(100);
 }
 }
 
 Serial.println();
 delay(500);
 Serial.println("**************************************");
 delay(500);
 Serial.println();
 delay(500);
 Serial.println("**************************************");
 delay(500);
 Serial.println();
 delay(500);
 Serial.println("**************************************");
 delay(500);
 Serial.println();
}

(

now the problem is - i try to setup the size of the arrays at declaration of the variable.
error:

structTest:32: error: too many initializers for 'word [0]'
structTest:32: error: too many initializers for 'word [0]'

is there some easy possibilities to do this an other way?

sunny greetings

stefan

Normally (on a proper OS) you would declare the structure members as pointers and allocate the memory as and when you need it:

struct sMultiMapStruct
	{
		word *mm_In;
		word *mm_Out;
		byte size;
	};
	
	typedef struct sMultiMapStruct tmm;

Then, to allocate 16 entries:

myTmmVar.mm_In = malloc(16*sizeof(word));

When you are done, you would want to free the data, or you'll have a memory leak:

free(myTmmVar.mm_In);

WARNING

While this is perfectly valid and possible on the Arduino it isn't advised. You will end up with bad memory fragmentation. On a computer with lots of memory you don't see a problem, but when memory is limited it becomes a real issue. It is far safer to decide on a maximum size for the arrays and allocate that many entries at compile time in the traditional manner.

ok thanks majenko!
i will define a maximal for my use case!

I would use fixed maximal array element too due to the same reason. Arduino doesn't have a proper OS or decent memory management with its 2KB memory. If you find yourself needing more memory, you should move the data processing onto a PC and only let arduino handle acquiring the data and transferring them to the PC. Of course, if you just need say 100KB of data and don't mind getting an arduino MEGA dev board, you can get it and a memory module to get up to 512KB memory (rugged circuit's quadRAM shield). The malloc is turned into just assigning an address to the array and using it (kind of wild :slight_smile: )