Arduino动态二维数组

时间:2017-02-17 02:50:53

标签: arduino

我正在开发一个Arduino项目,我需要在运行时构建(并使用)二维数组。我一直在四处寻找解决方案,但我没有运气。我在这里找到了一个动态一维数组助手的例子:http://playground.arduino.cc/Code/DynamicArrayHelper,所以我一直在尝试采用该代码供我使用。我使用以下代码创建了一个库:

我的页眉文件:

#ifndef Dynamic2DArray_h
#define Dynamic2DArray_h

#include "Arduino.h"

class Dynamic2DArray
{
  public:
    Dynamic2DArray( bool sorted );
    //Add an integer pair to the array
    bool add( int v1, int v2);  
    //Clear out (empty) the array
    bool clear();
    //Get the array item in the specified row, column
    int getValue(int row, int col);    
    //Get the number of rows in the array
    int length();    
  private:
    int _rows;
    void * _slots;
    bool _sorted;
    void _sort();
};

#endif

图书馆的代码:

#include "Arduino.h"
#include "Dynamic2DArray.h"

#define ARRAY_COLUMNS 2

int _rows;
void * _slots;
bool _sorted;

Dynamic2DArray::Dynamic2DArray(bool sorted) {
  //Set our local value indicating where we're supposed to
  //sort or not
  _sorted = sorted;

  //Initialize the row count so it starts at zero
  _rows = 0;
}

bool Dynamic2DArray::add( int v1, int v2) {
  //Add the values to the array
  //implementation adapted from http://playground.arduino.cc/Code/DynamicArrayHelper
  //Allocate memory based on the size of the current array rows plus one (the new row)
  int elementSize = sizeof(int) * ARRAY_COLUMNS;
  //calculate how much memory the current array is using
  int currentBufferSize = elementSize * _rows;
  //calculate how much memory the new array will use
  int newBufferSize = elementSize * (_rows + 1);
  //allocate memory for the new array (which should be bigger than the old one)
  void * newArray = malloc ( newBufferSize );
  //Does newArray not point to something (a memory address)?
  if (newArray == 0)   {
    //Then malloc failed, so return false
    return false;
  }
  // copy the data from the old array, to the new array
  for (int idx = 0; idx < currentBufferSize ; idx++)
  {
    ((byte*)newArray)[idx] = ((byte *)_slots)[idx];
  }
  // free the original array
  if (_slots != NULL)
  {
    free(_slots);
  }
  // clear the newly allocated memory space (the new row)
  for (int idx = currentBufferSize; idx < newBufferSize; idx++)
  {
    ((byte *)newArray)[idx] = 0;
  }
  // Store the number of rows the memory is allocated for
  _rows = ++_rows;
  // set the array to the newly created array
  _slots = newArray;
  //Free up the memory used by the new array
  free(newArray);

  //If the array's supposed to be sorted,
  //then sort it
  if (_sorted) {
    _sort();
  }

  // success
  return true;

};

int Dynamic2DArray::length() {
  return _rows;
};

bool Dynamic2DArray::clear() {
  //Free up the memory allocated to the _slots array
  free(_slots);
  //And zero out the row count
  _rows = 0;
};

int Dynamic2DArray::getValue(int row, int col) {
  //do we have a valid row/col?
  if ((row < _rows) && (col < ARRAY_COLUMNS)) {
    //Return the array value at that row/col
    return _slots[row][col];
  } else {
    //No? Then there's nothing we can do here
    return -1;
  }
};

//Sorted probably doesn't matter, I can probably ignore this one
void _sort() {

}

_slots值的初始赋值给了我一些问题,我不知道如何定义它以便这个代码构建。 _slots变量应该指向动态数组,但我错了。

当我尝试将代码编译到项目代码中时,我得到以下内容:

Arduino: 1.8.0 (Windows 10), Board: "Pro Trinket 3V/12MHz (USB)"

sketch\Dynamic2DArray.cpp: In member function 'int Dynamic2DArray::getValue(int, int)':

sketch\Dynamic2DArray.cpp:83:22: warning: pointer of type 'void *' used in arithmetic [-Wpointer-arith]

     return _slots[row][col];

                      ^

Dynamic2DArray.cpp:83: error: 'void*' is not a pointer-to-object type

有人可以帮我解决这个问题吗?我已将文件发布到https://github.com/johnwargo/Arduino-Dynamic-2D-Array-Lib

1 个答案:

答案 0 :(得分:1)

您采用的代码是1D动态数组;对2D阵列的修改太棘手了。放弃这些恐怖。

我认为你没有理由使用动态数组。您可以假设max max是ROW_MAX * COL_MAX,因此您可以定义静态数组int array[ROW_MAX][COL_MAX]

  • 一方面,如果你定义了一个动态数组,当你不再使用它时可以释放空间,并利用它进行其他工作。我不知道这是不是你的情况。
  • 另一方面,如果你定义一个静态数组(在UNO上),你可以在程序空间上使用32kB,而不是在RAM上使用2kB。

由于32kB / 2kB的差异,你可以通过动态分配获得更大的数组。