Pages: [1]   Go Down
Author Topic: struct and arrray size  (Read 518 times)
0 Members and 1 Guest are viewing this topic.
Germany
Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
just sun
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 (http://arduino.cc/playground/Main/MultiMap)
and my idea was to build a struct that holds all needed things for a nice mapping:
Code:
struct sMultiMapStruct
{
word mm_In [];
word mm_Out [];
byte size;
};

typedef struct sMultiMapStruct tmm;

for testing:
Code:
/****************************************************************************************************
  *   structTest sketch
  *   
  *   
  *   Created:  22.06.2012
  *   copyright by Stefan Krueger
  *   written by Stefan Krueger stefan@s-light.eu
  *   
  *   Last modified: 22.06.2012 21:00
  *   By Stefan Krueger
  *   stefan@s-light.eu
  *   
  *   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:
Code:
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
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
struct sMultiMapStruct
{
word *mm_In;
word *mm_Out;
byte size;
};

typedef struct sMultiMapStruct tmm;

Then, to allocate 16 entries:

Code:
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:

Code:
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.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Germany
Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
just sun
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7197
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley )
Logged


Pages: [1]   Go Up
Jump to: