我有一個類來解析一些傳入的串行數據。解析一個方法後,應該返回一個帶有一些解析數據的字節數組。傳入的數據長度未知,所以我的返回數組將始終不同。如何從方法返回未知大小的字節數組
到目前爲止,我的方法分配一個比我需要返回的數組大的數組,並用我的數據字節填充它,並且保留一個索引,以便我知道我在字節數組中放置了多少數據。我的問題是,我不知道如何從實例方法返回此。
void HEXParser::getParsedData()
{
byte data[HEX_PARSER_MAX_DATA_SIZE];
int dataIndex = 0;
// fetch data, do stuff
// etc, etc...
data[dataIndex] = incomingByte;
_dataIndex++;
// At the very end of the method I know that all the bytes I need to return
// are stored in data, and the data size is dataIndex - 1
}
在其他語言上這是微不足道的事情,但我不是非常精通C++,我完全卡住了。
謝謝!
使用['std :: vector'](http://en.cppreference.com/w/cpp/container/vector)。 –
'數據'應該動態分配,如果你想返回它 –
ahhhhh,而不是在微控制器上。離開堆。 – jdr5ca