How do I implement a list?

I have the following struct in my sketch:
struct SensorObject
{
int ID;
String Label;
byte SensorPin;
float Threshold;
byte OnPin;
byte OffPin;
bool Status;
};
Is there a way to make a List as I would in c#?

You can make an array. You can also use the Standard Template Library.

http://www.gammon.com.au/forum/?id=11119

Be warned that you don't have a lot of RAM.

Please note that in versions of the IDE up to and including 1.0.3, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.), as described here for example.

Alternatively, install the fix described here: Fixing String Crashes

Preferably upgrade your IDE to version 1.0.4 or above at: http://arduino.cc/en/Main/Software

List as I would in c#?

I don’t know what that does but a list (AKA linked list) in C is normally a struct with a pointer to the next (and often the previous) item in the list, those being structs of the same type.

I can never remember the exact syntax, I think you need to typedef the struct before you declare it but it’s something like this

struct SensorObject
{
   int ID;
   String Label;
   byte SensorPin;
   float Threshold;
   byte OnPin;
   byte OffPin;
   bool Status;
   SensorObject * next;
   SensorObject * prev;

};

If this is what you mean and nobody else comes up with the correct syntax I’ll look it up.


Rob

That compiles. Be warned that the use of complex classes like String mean you cannot malloc list members, you have to use new to create them.

The STL simplifies the creation of lists, but as I said, memory is tight.

I thought referencing the same struct you're in caused problems, but maybe not. All these years and I still can't remember this stuff :(


Rob

At the line where you declare:

   SensorObject * next;

It already knows what SensorObject is (a struct).

If you change it to put the name last then you need the forward reference:

struct SensorObject;

struct
{
   int ID;
   String Label;
   byte SensorPin;
   float Threshold;
   byte OnPin;
   byte OffPin;
   bool Status;
   SensorObject * next;
   SensorObject * prev;

} SensorObject;

Graynomad: I thought referencing the same struct you're in caused problems, but maybe not. All these years and I still can't remember this stuff :(


Rob

In 'C', if you defined a typedef for a struct tag then the typedef followed the struct declaration so was not visible within the struct. This meant that self references within the struct type needed to refer to itself by the struct tag not the typedef iyswim. This used to mean, for example, that struct types that referred to themselves couldn't be anonymous.

I understand that in C++ structs are now treated as classes so it is no longer necessary to refer to them via the the 'struct' keyword - this means the tag can be used directly within the struct for self references without using the 'struct' keyword. It also means that there's no need for the old practice of using typedefs to make a struct tag look like a fully fledged type.

In 'C',

That's probably why I thought as above, I've only used C lately as the base-level compiler for LPCs is not C++.


Rob

the use of complex classes like String mean you cannot malloc list members, you have to use new to create them.

Does that means that it fails entirely on Arduino, where new() is just a wrapper for malloc(), or does some kind of magic happen at the compile level?

The magic happens.

The use of "new" tells the compiler to do its stuff with constructors. If new is implemented with malloc, well that's just how it gets the memory.