我必须遵循功能
public int[] PollAllDevices()
{
/* Polling commands */
const byte POLL_FRONT_RADAR = 0x00;
const byte POLL_REAR_RADAR = 0x01;
const byte POLL_DISP = 0x02;
const byte POLL_CTRL = 0x03;
const byte POLL_EXP1 = 0x04;
const byte POLL_EXP2 = 0x05;
byte[] buff = new byte[32];
int[] _ret = new int[32];// { 23, 24, 25, 26, 27, 28, 29, 30 };
int i = 0;
if(API.uart_write(devPtr, new byte[1] { POLL_FRONT_RADAR }, 0, 1))
{
if (API.uart_read(devPtr, buff, 0, 1, 1000) != 0)
{
_ret[i++] = (int)buff[1];
}
}
if (API.uart_write(devPtr, new byte[1] { POLL_REAR_RADAR }, 0, 1))
{
if (API.uart_read(devPtr, buff, 0, 1, 1000) != 0)
{
_ret[i++] = (int)buff[1];
}
}
return _ret;
}
我在观察窗口看到了_ret。我可以看到它存在,直到我调用API.uart_read,然后它在观察窗口中变为灰色,我看到
_ret无法获得本地或参数的价值' _ret'因为它在这个指令指针上不可用,可能是因为它已被优化掉了。 INT []
(只是将其作为额外信息投放在那里。这不是我的问题所以我不确定为什么这被标记为重复。该错误的所有解决方案似乎都围绕着优化而我没有打开优化。)
当我尝试写入它时,我得到一个超出范围的数组异常。那时i = 0,但只是为了笑,我把它硬编码到_ret [0],我仍然得到了异常。如果我将_ret更改为int,我可以再次写入。
我已关闭优化功能。谁能告诉我这里我做错了什么?
答案 0 :(得分:0)
#include <iostream>
#include <array>
using namespace std;
void addArrays(const array<int,20> &array1,
const array<int,20> &array2,
array<int,20> &array3);
int main()
{
array< int, 20 >array1 =
{7,45,90,1,33,254,3,8,9,4,2,6,77,5,9,2,22,4,12,6};
array< int, 20 >array2 =
{88,4,6,75,1,12,33,99,66,88,7,4,44,4,3,13,5,4,9,3};
array< int, 20 >array3;
addArrays(array1,array2,array3);
cout << "Array1 + Array2 = Array3" << endl;
int j;
for(j=0; j<20; ++j)
cout << array1[j] << "+" << array2[j] << "=" << array3[j] << endl;
return 0;
}
void addArrays(const array<int,20> &array1,
const array<int,20> &array2,
array<int,20> &array3)
{
int i;
for(i=0; i<20; ++i)
array3[i] = array1[i]+array2[i];
}
可能导致越界异常。
尝试将(int)buff[1]
拆分为
_ret[i++] = (int)buff[1];
看看会发生什么。
答案 1 :(得分:0)
由于某些原因,我们真的无法理解为什么你的阵列会超出范围,但是如果没有其他解决方案出现,我还有一个工作区:
制作方法public void PollAllDevices(out _ref){...}
,并像
int[] _ref;
PollAllDevies(out _ref);
并将int[] _ret = new int[32];
内部更改为_ref = new int[32]
。
当然,现在你也可以把一个bool作为返回值(或其他一些元数据),如果你有用的话。
答案 2 :(得分:0)
一些想法......
我无法访问您的API,因此我将您的代码修改为如下所示:
byte[] buff = new byte[32];
int[] _ret = new int[32];// { 23, 24, 25, 26, 27, 28, 29, 30 };
int i = 0;
if (true)
{
if (true)
{
_ret[i++] = (int)buff[1];
}
}
if (true)
{
if (true)
{
_ret[i++] = (int)buff[1];
}
}
这当然很好。如果是我,我会重新介绍API.uart_write
的电话,看看会发生什么。然后,我会再次删除这些呼叫并重新调用API.uart_read
。这将确保它与这两个呼叫的交互无关。
您发布的代码似乎并不完整,因为我无法在任何地方看到devPtr
的声明方式。这是对_ret
或_ret
值之一的第二次引用吗?
它甚至没有意义,因为你在方法调用结束时返回它,它会被优化掉。编译器不会优化方法的返回值。
答案 3 :(得分:0)
我的c ++函数API.uart_read()试图在buff中放入64个字节,它正在踩到_ret。
我想。我知道你将如何在C中描述它。当我使buff足够大以容纳所有64字节时,事情按预期工作。