This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

C++: Aligned byte fields not possible in classes?

Hi,
if I want to define a 4byte aligned char array in C, then I typically would declare:

__align(4) char acBuf[20];

In C++, inside a class, this unfortunately fails (Error #328: invalid storage class for a class member).

Is there any way to enforce alignment for C++ class members?

Parents Reply Children
  • I am just programming some USB class, and I had the ambition to define the Endpoint buffers inside the class.

    Such endpoint buffers need to be aligned on 4-byte adresses, otherwise you get bad crashs (HardFault).

    I now recognized, that concerning alignement, the c++ compiler seems to handle the class data like in a struct, if I use the "packed" pragma. So the flexibility of data alignment in a struct seems to be superior than to the data alignement in classes? This really is quite a disapointment (comparing c++ to "normal" c). ... I just recognized, the c++ compiler really located my USB buffers on 1-byte adresses (without getting red ...).

    ... so thank you for this dark art trick, with this union trick it works ... :)

  • ... sorry about one error in my last post: structs of course would also handle such byte fields packed (without "pack" pragma) ... . So no reason to get red for the C++ compiler here :).