How to pass a bitfield (by reference) to a function?

我怕爱的太早我们不能终老 提交于 2019-12-08 08:19:28

Since the real usage of this code is to write to a hardware register, issues like padding and alignment/ordering of fields DOES matter.

I suspect the compiler is using 32-bit ints, and this structure is getting padded to 32-bits, but in the actual code being debugged GAIN_X isn't a local var, you're passing 0xNNNNNNN (or equivalent) - and the address isn't on the "right" boundary (quite possible as it's a 24-bit register). The compiler will assume you're passing a pointer to a real GAIN_REG_st, not a type-punned address, and so may have made assumptions about alignment.

To access hardware directly from C/C++, you need to know how the compiler handles stuff like this, and make sure you lie to the compiler carefully.

Could it be some alignment problem?

Maybe you can play with your compiler's alignment options? Or try stuffing your structure with a dummy uint8 at the end?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!