We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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?
I don't want to sound like I'm from the 'you should learn to ask a smart question brigade' here, but I'm interested to know why you need/want to do that?
FWIW I know of no method outside the region of dark art.
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 :).