|
|
|
|
|
by jstimpfle
765 days ago
|
|
gcc will compile this code and advance the pointer address by 1 byte. Yes, it's not standard conformant. My point though is that this is unlikely to happen by accident. The code above is clearly constructed. It's more work to type it than the correct version. The code makes no sense at all. 1 byte is wasted and it's in general a bad idea to allocate unaligned pointers. And when you for example provide your own allocator for nodes, you'll probably do it like this: Treenode treenode_pool[MAX_NODES];
or this: Treenode *treenode_pool = allocate(Treenode, node_count);
And the memory will be aligned properly. Even when you use a plain malloc (no type or alignment information given to the allocator) you will get memory that is at least pointer-aligned. |
|
https://godbolt.org/z/cjoYvjxGq