我有一个如下所示的代码:
typedef struct{
uint8_t distance[2];
uint8_t reflectivity;
}data_point;
typedef struct{
uint8_t flag[2];
uint8_t Azimuth[2];
std::vector<data_point> points; // always size is 32 but not initialized whereas filled in run-time using emplace_back
}data_block;
typedef struct{
uint8_t UDP[42];
std::vector<data_block> data; // always size is 12 but not initialized whereas filled in run-time using emplace_back
uint8_t time_stamp[4];
uint8_t factory[2];
}data_packet;
static std::vector<data_packet> packets_from_current_frame;
假设packets_from_current_frame.size() = 26,我如何计算其中的字节数packets_from_current_frame?
我在纸上的解决方案:
1 data_packet(假设 32points和 12 data)将有42+ (12*(2+2+32(3))) + 4 + 2 = 1248. 因此,结束地址是_begin + sizeof(uint8_t) * 26 * 1248(_begin是内存缓冲区的起始地址)。
通过这个计算,我总是会丢失一些数据。丢失的字节数取决于packets_from_current_frame.size()。计算有什么问题?