数组只返回最后一个元素.[C / Arduino]
Array keeps returning only the last element. [C/Arduino]
我在Arduino上有一个数组(称为"GeneralInput"类型的"输入")的问题,基本上,无论我尝试访问哪个元素,代码总是返回我该数组的最后一个元素。下面是部分代码:
//...include statements
//other initializations
GeneralInput *Inputs[19];
void setup()
{
//...
//...
InitializeInputs();
}
void InitializeInputs()
{
//type 0 = pedal switch; 1 = volume pedal
//type 2 = potentiometer; 3= switch;
//pedal switches
Inputs[0] = &GeneralInput(0,0,true,false,NULL,10);
Inputs[1] = &GeneralInput(1,0,true,false,NULL,9);
Inputs[2] = &GeneralInput(2,0,true,false,NULL,6);
Inputs[3] = &GeneralInput(3,0,true,false,NULL,5);
//volume pedal
Inputs[4] = &GeneralInput(4,1,false,false,NULL,A2);
//potentiometer
Inputs[5] = &GeneralInput(5,2,false,true,mux2,5);
Inputs[6] = &GeneralInput(6,2,false,true,mux2,6);
Inputs[7] = &GeneralInput(7,2,false,true,mux2,7);
Inputs[8] = &GeneralInput(8,2,false,true,mux2,8);
Inputs[9] = &GeneralInput(9,2,false,true,mux2,9);
Inputs[10] = &GeneralInput(10,2,false,true,mux2,10);
Inputs[11] = &GeneralInput(11,2,false,true,mux2,11);
//switch
Inputs[12] = &GeneralInput(12,3,true,true,mux2,15);
Inputs[13] = &GeneralInput(13,3,true,true,mux2,14);
Inputs[14] = &GeneralInput(14,3,true,true,mux2,13);
Inputs[15] = &GeneralInput(15,3,true,true,mux2,12);
//joystick
Inputs[16] = &GeneralInput(16,3,true,true,mux1,2); //switch
Inputs[17] = &GeneralInput(17,2,false,true,mux1,1); //x axis
Inputs[18] = &GeneralInput(18,2,false,true,mux1,3); //y axis
}
void loop()
{
int length=0;
//cycle through different inputs
int startIndex=0,endIndex=0;
//temp arrays
byte toSendTmp[30];
for(int i=0;i<30;i++)
toSendTmp[i]=0;
//...
//..
int packetIndex=0;
for(int i=startIndex;i<endIndex;i++)
{
//if the input is updated,fill the array with the new data
/*
* When i try to have access to the i-element i always get
* the last one instead.
*/
if(Inputs[i]->Update())
{
toSendTmp[(packetIndex*3)] = Inputs[i]->GetID();
toSendTmp[(packetIndex*3)+1] = Inputs[i]->GetType();
toSendTmp[(packetIndex*3)+2] = Inputs[i]->GetValue();
packetIndex++;
}
}
//....
//...
}
如果需要,这里是GeneralInput.h和GeneralInput.cpp代码。注意:我无法判断数组是否总是返回最后一项,或者数组的每个槽都被指向相同对象(最后创建的对象)的指针填充。
你知道我做错了什么吗?
您的&GeneralInput
是不正确的,事实上您创建临时对象并将其地址存储在数组中,但是一旦您的GeneralInput
对象被销毁(与创建相同的行),新对象将在相同的地址发生:
// Create GeneralInput at address @
Inputs[0] = &GeneralInput(0,0,true,false,NULL,10);
// End of your temporary object, the `GeneralInput` object is destroyed but you still
// points to its address...
/* etc. */
你得到的是最后一个值,因为编译器总是在相同的地址创建GeneralInput
,所以所有Inputs[]
都指向相同的地址。
您需要动态创建您的GeneralInput
:
Inputs[0] = new GeneralInput(0,0,true,false,NULL,10);
数组中的每个槽都有一个指向同一内存位置的指针,该内存位置被您创建的最后一个元素占用。通过执行&GeneralInput(...)
,您正在堆栈上创建GeneralInput对象并检索其堆栈地址。但是,由于GeneralInput对象本身从未被赋值给变量,因此它占用的内存可以立即重用。这意味着每个GeneralInput对象都是在堆栈上的相同地址创建的。但是,解决方案不是将代码更改为
GeneralInput genInput = GeneralInput(...);
Inputs[...] = &genInput;
像这样的代码仍然会用指向堆栈地址的指针填充数组。当函数返回时,这些指针将立即失效。你应该用
之类的东西填充你的数组Inputs[...] = (GeneralInput*)malloc(sizeof(GeneralInput));
*Inputs[...] = GeneralInput(...);
使用此方法确保如果您的Inputs
数组达到不再使用它的点,则循环free
每个元素。
编辑:Arduino使用C,所以没有new
。使用malloc
和free
代替。
正如其他人所说,问题在于临时变量的地址。您可以通过使用默认参数来解决"新"问题。
class GeneralInput
{
public:
GeneralInput(int a = 0, int b = 0, bool c = true, bool d = true, int* e = NULL, int f = 0);
...
};
然后声明你的数组-它接受GeneralInput和默认参数
GeneralInput inputs[20];
然后在Initialize -中,你就不会有新的问题,或者在例程结束时临时消失的问题。
void InitializeInputs()
{
inputs[0] = GeneralInput(0,0,true,false,NULL,10);
...
}
我不知道NULL指向什么,但如果它不是复制值,你可能想为它添加一个复制操作符。不是很有效,因为它调用构造函数两次,但这只发生在初始化时。
- Mongodb c++驱动程序:如何查询元素的数组
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用strcpy将char数组的元素复制到另一个数组
- 使用不带参数的函数访问结构元素
- 给定n个元素的m个集合.在C++中找到出现在最大集合数中的元素
- C++如何通过用户输入删除列表元素
- lower_bound()返回最后一个元素
- 基于多个条件处理地图中的所有元素
- 调整大小后指向元素值的指针unordered_map有效?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 使用函数"remove"删除重复元素
- 具有最大子序列大小的序列,每个元素都相同
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 如何将元素添加到数组的线程安全函数?
- 如何在 Arduino 字符串的开头添加元素.类似于 JS unshift();
- C++递减单字节(易失性)数组的元素不是原子!为什么?(还有:如何在Atmel AVR mcus/Arduino中强制原
- 结合阵列元素Arduino
- 数组只返回最后一个元素.[C / Arduino]