Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

c++ - C ++指针算术:对齐异常(0x801)(C++ pointer arithmetic: alignment exception (0x801))

I got a very strange alignment exception, which only occurs on certain hardware combinations.

(我遇到了一个非常奇怪的对齐异常,该异常仅在某些硬件组合上发生。)

I have implemented a bluetooth audio sink, which get its data fed from a unix file descriptor.

(我已经实现了一个蓝牙音频接收器,该音频接收器的数据从unix文件描述符中获取。)

When i combine a Macbook Pro (as bluetooth source) and a raspberry pi (as bluetooth sink), i get an alignment exception at the following point:

(当我将Macbook Pro(作为蓝牙源)和树莓派(作为蓝牙接收器)结合使用时,在以下几点出现对齐异常:)

void process(uint8_t* inData, uint32_t size, float* outData)
{
    int16_t* from = (int16_t*)inData;
    float* to = outData;

    for (size_t i = 0; i < size/2; ++i) {
        *to = *from/32767.0;
        ++to;
        ++from; // Crashes on MacbookPro/RasPi combination
    }
}

How comes?

(怎么会?)

My sink obviously does not know about my source.

(我的水槽显然不知道我的来源。)

And this works for other platforms (combinations)?

(这适用于其他平台(组合)吗?)

I also tried this snippet, however, also no success.

(我也尝试过此代码段,但是也没有成功。)

int8_t* from = (int8_t*)inData;
float* to = outData;

for (size_t i = 0; i < size/2; ++i) {
    int16_t tmp;
    std::memcpy(&tmp, from, 2);
    *to = tmp/32767.0;
    ++to;
    from += 2; // This crashes
}

I guess a running example would not help here, since the exact same code works, when using another data (bluetooth) source.

(我猜正在运行的示例在这里无济于事,因为当使用另一个数据(蓝牙)源时,完全相同的代码可以工作。)

  ask by mincequi translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are treating a pointer to an 8-bit value as a pointer to a 16-bit value.

(您正在将指向8位值的指针视为指向16位值的指针。)

This is undefined.

(这是未定义的。)

Further, typically, 8-bit values will have an alignment of 1 byte, where as a 16-bit value has am alignment of 2 bytes.

(此外,通常8位值的对齐方式为1个字节,而16位值的对齐方式为2个字节。)

As Eljay said in the comments, you can change the alignment of inData , but the result is still not defined.

(正如Eljay在评论中所说,您可以更改inData的对齐方式,但结果仍未定义。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...