Pages: [1]   Go Down
 Author Topic: struct and arrray size  (Read 420 times) 0 Members and 1 Guest are viewing this topic.
Germany
Offline
Jr. Member
Karma: 0
Posts: 70
just sun
 « on: June 22, 2012, 03:28:52 pm » Bigger Smaller 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
*   written by Stefan Krueger stefan@s-light.eu
*
*   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
Karma: 90
Posts: 3903
 « Reply #1 on: June 22, 2012, 03:36:16 pm » Bigger Smaller 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

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Germany
Offline
Jr. Member
Karma: 0
Posts: 70
just sun
 « Reply #2 on: June 22, 2012, 04:30:47 pm » Bigger Smaller Reset

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

Central MN, USA
Online
Tesla Member
Karma: 63
Posts: 6732
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
 « Reply #3 on: June 22, 2012, 04:55:00 pm » Bigger Smaller 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 )
 Logged

 Pages: [1]   Go Up