0

我有一个二维双数组指针。我可以将其转换为u_int8_t,将其提取到mutable_data()箭头池缓冲区,并构造一个箭头 DoubleArray。但是,当我从数组的 , 中获取值时Value()raw_values()我无法获得正确的数字。有人请查看我的代码并帮助我:

double* generateDoubleArray() {
    int totalColumns = 200;
    int totalRows = 200;
    std::random_device r;
    std::default_random_engine e1(r());
    std::normal_distribution<double> nor_dist(-1, 1);

    double *A = static_cast<double *>(malloc(totalColumns * totalRows * sizeof(double *)));
    for (int i = 0; i < totalRows; i++) {
        for (int j = 0; j < totalColumns; j++) {
            A[i*totalColumns + j] = sin(nor_dist(e1)) + exp(sin(nor_dist(e1)) + cos(nor_dist(e1)));
        }
    }

    return A;
}
int main()
{
    double* generatedDouble = generateDoubleArray();
    uint8_t *ui_generatedDouble = (uint8_t*)generatedDouble;
    arrow::MemoryPool* pool_;
    pool_ = arrow::default_memory_pool();
    auto data = std::make_shared<arrow::PoolBuffer>(pool_);
    size_t length = 200 * 200;
    const size_t data_nbytes = length * sizeof(double *);

    data->Resize(data_nbytes);
    *data->mutable_data() = *ui_generatedDouble;
    auto out = std::make_shared<arrow::DoubleArray>(data_nbytes, data);

    double test_value = out->Value(0); // The returned value is not correct

    return 0;
}

谢谢

4

1 回答 1

0

以下行不符合您的想法:

*data->mutable_data() = *ui_generatedDouble;

它只复制数据的第一个字节。你想 memcopy 整个缓冲区:

std::memcpy(data->mutable_data(), ui_generatedDouble, data_nbytes);
于 2017-12-14T06:59:49.977 回答