Using an array as Storage for linked objects

Hey guys,

coming from c# with lists i have to find way to "store" some kind of obejcts.In my case i have 6
RS_DHTSensor Objects and want to add them to another object. Normaly i would have created a list and then i could add/remove them.

So i wanted to use an array instead. Now i learned, that i can only point to the objects and cant hold them in an array.

Now lets say i want to store n amount (max 64) sensor objects, i would create an array of 64.

But when i want to add one, how does i know which array element is free?

And how can i for example loop through the array and only take the pointed objects out?

Hope you i explained it correctly :smiley:

Following example:

Sensor.h

class RS_DHTController {
private:
    RS_DHTSensor *sensors[64];

public:
    RS_DHTController();

    bool addSensor(RS_DHTSensor sensor);

};

sensor.cpp

RS_DHTController::RS_DHTController()
{
}

bool RS_DHTController::addSensor(RS_DHTSensor sensor)
{
    // Something like this?!?
    for (int i=0; i<sizeof sensors; i++) {
         //How can i check now if i allready linked an object to this element??

    }

    return false;
}

Greetings
Raphael

Dangerous...

If you need to add and remove members, I’d use an array[] of structs()

Then you could include a forward and backward index in the struct for navigation across the (not necessarily in order) members of the array.

Instead of using "sizeof" to iterate the array, declare a constant that defines the size of the array instead:

#define ARRAY_SIZE 64
DataType *the_array[ARRAY_SIZE] = {0};
...
for (i = 0; i < ARRAY_SIZE; i++) do_it(the_array[i]);

Adding = {0} to the array declaration will ensure that all elements are zero'ed / null. So whenever you need to find an empty spot, you can look for a null element. Even better would be to add a counter that holds the number of elements in the array:

int num_elements = 0;

int add(some* thing)
{
  if (num_elements >= ARRAY_SIZE) return -1; //Fail, no more space
  the_array[num_elements] = thing;
  num_elements++;
  return num_elements - 1; //Return the position added to
}

for (int i = 0; i < num_elements; i++) do_it(the_array[i]); //Only iterate occupied elements

When you add something to the array, make sure it is a pointer and not a static variable.

That's incorrect. An array can hold just about any type of object.

What type of Arduino board are you using? If it's not an AVR-based one, then you have std::vector<> and other STL containers at your disposal.

For your list of 64 pointers you could use NULL to indicate an empty space.

class RS_DHTController
{
  private:
    RS_DHTSensor *sensors[64];

  public:
    RS_DHTController();
    bool addSensor(RS_DHTSensor sensor);
};

RS_DHTController::RS_DHTController()
{
  for (size_t i = 0; i < (sizeof sernsors / sizeof sensors[0]); i++)
    sensors[i] = NULL;
}

bool RS_DHTController::addSensor(RS_DHTSensor *sensor)
{
  for (size_t i = 0; i < (sizeof sernsors / sizeof sensors[0]); i++)
  {
    if (sensors[i] == NULL)
    {
      sensors[i] = sensor;
      return true;
    }
  }

  return false;
}

Thank you guys. That way it works perfectly :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.