following excerpted from here
pw = (widget *)malloc(sizeof(widget));
allocates raw storage. Indeed, the malloc call allocates storage that's big enough and suitably aligned to hold an object of type widget
also see fast pImpl from herb sutter, he said:
Alignment. Any memory Alignment. Any memory that's allocated dynamically via new or malloc is guaranteed to be properly aligned for objects of any type, but buffers that are not allocated dynamically have no such guarantee
I am curious about this, how does malloc know alignment of the custom type?
See Question&Answers more detail:os