Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从boost创建的缓冲区访问数据_C++_Vector_Boost_Buffer - Fatal编程技术网

C++ 从boost创建的缓冲区访问数据

C++ 从boost创建的缓冲区访问数据,c++,vector,boost,buffer,C++,Vector,Boost,Buffer,我试图访问使用boostbuffer函数序列化的数据,并希望将其填充到两个向量中。我在填写第二个向量时遇到地址问题。下面的课程展示了这两个向量及其填充方式 class LidarMeasurement { private: std::vector<uint32_t> _header; std::vector<float> _azimuth; public: //The header consists of an array of uin

我试图访问使用boostbuffer函数序列化的数据,并希望将其填充到两个向量中。我在填写第二个向量时遇到地址问题。下面的课程展示了这两个向量及其填充方式

class LidarMeasurement {
  private:
    std::vector<uint32_t> _header;
    std::vector<float> _azimuth;

  public:

    //The header consists of an array of uint32_t's in the following layout
    enum Index : size_t {
         HorizontalAngle,
         ChannelCount,
         SIZE
         };

    explicit LidarMeasurement(uint32_t NumOfChannels = 0u): _header(Index::SIZE + NumOfChannels, 0u) {
        _header[Index::ChannelCount] = NumOfChannels;
    }

    // called before filling vectors
    void Reset(uint32_t total_point_count) {
        std::memset(_header.data() + Index::SIZE, 0, sizeof(uint32_t) * GetChannelCount());
        _azimuth.clear();
        _azimuth.reserve(total_point_count);
    }

    // after reset,Write point function starts filling vectors.. following function is called 104 times (not constant) before next reset
    void WritePoint(uint32_t channel, float angle_hor) {
        _header[Index::SIZE + channel] += 1u;
        _azimuth.emplace_back(angle_hor);
    }

    uint32_t GetChannelCount() const {
       return _header[Index::ChannelCount];
    }
}
但是,我得到的结果有一个内存偏移量。我得到了104个值,但最后18个值都是垃圾。从错误的起始地址读取向量。代码有什么问题


问题是由于错误的起始地址计算引起的

begin_azi=const_cast(reinterpret_cast(_begin))+(sizeof(uint32_t)*(GetChannelCount()+Index::SIZE))

1) 指针算法只需要指针和要前进的元素数。编译器应根据指针类型自行扣除的字节数。因此,在
sizeof(uint32\u t)
处的乘法是多余的。指针前进的正确方式显示在
float*end_azi=begin_azi+GetTotalPointCount()

2) 应该为指向uint32_t类型的指针计算地址偏移量,然后才转换为指向浮点类型的指针

所以正确的开始方式应该是这样的:

begin_azi=const_cast(reinterpret_cast(_begin+GetChannelCount()+Index::SIZE))

为什么它早些时候起了部分作用?从

指针算法

如果指针p指向索引为I的数组的元素,则

  • P+N和N+P是指向索引为I+N的同一数组的元素的指针
  • P-N是指向索引为{tt | I-N}的同一数组元素的指针
仅当原始指针和结果指针都指向同一数组的元素,或者指向该数组末尾的元素时,才定义该行为


没有人知道错误计算后指向的
从何处开始。因此,无法保证程序将以正确或错误的方式执行。

我将其更改为“NumOfChannels”,以避免混淆。。!我不太熟悉指针算术魔术,但我对
reinterpret\u cast(\u begin))+(sizeof(uint32\u t)*(GetChannelCount()+Index::SIZE))
感到困惑,首先将指向uint32\u t的指针转换为指向float的指针,然后尝试推进整数字节。在这一点上效果好吗?据我记忆所及,指针加法需要相同的类型,因此这里您应该首先将int数组末尾的地址前移,然后将指针强制转换为float。
uint32\u t OFFSET=sizeof(uint32\u t)*(GetChannelCount()+LidarMeasurement::Index::SIZE);begin_azi=常量(重新解释常量(_begin+偏移))即使这样也不起作用。什么是GetTotalPointCount()?当您试图调用GetAzimuth时,它的返回值是否大于或等于104?返回指针算术魔术。您不应该向编译器显示一个元素占用了多少字节,因此不需要在sizeof(uint32_t)处对元素进行乘法。编译器应该根据元素的类型和元素的数量来扣除它<代码>begin_azi=const_cast(重新解释cast(_begin+GetChannelCount()+LidarMeasurement::Index::SIZE))template <typename Sensor>
  inline Buffer LidarSerializer::Serialize(
      const Sensor &,
      const LidarMeasurement &measurement,
      Buffer &&output) {
    std::array<boost::asio::const_buffer, 2u> seq = {
        boost::asio::buffer(measurement._header),
        boost::asio::buffer(measurement._azimuth)};
    output.copy_from(seq);
    return std::move(output);
  }
std::vector<float> GetAzimuth(const uint32_t* _begin) const{
      std::vector<float> localAzimuthMemCopy;
      begin_azi = const_cast<float*>(reinterpret_cast<const float*>(_begin )) + (sizeof(uint32_t) * (GetChannelCount() + Index::SIZE));
      end_azi = begin_azi + GetTotalPointCount();//Total point count is the addition of individual channel point counts (not shown here)
      for(float* i = begin_azi; i < end_azi; i++){
        localAzimuthMemCopy.emplace_back(*i);
      }
      return localAzimuthMemCopy;
    }