[cfe-users] -Wcast-align

degski via cfe-users cfe-users at lists.llvm.org
Thu Apr 12 06:27:18 PDT 2018


I have the following code (C11):

#define vector_grow(vec,
count)                                                                     \
do
{
\
    if(!(vec))
{
\
        size_t *__p = (size_t*) malloc ((size_t)(count) * sizeof(*(vec)) +
(sizeof(size_t) * 2));   \

assert(__p);
\
        (vec) = (void
*)(&__p[2]);
\
        vector_set_capacity((vec),
(count));                                                        \
        vector_set_size((vec),
0);                                                                  \
    } else
{
\
        size_t *__p1 = &((size_t
*)(vec))[-2];                                                      \
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        size_t *__p2 = realloc(__p1, ((size_t)(count) * sizeof(*(vec))+
(sizeof(size_t) * 2)));     \

assert(__p2);
\
        (vec) = (void
*)(&__p2[2]);
\
        vector_set_capacity((vec),
(count));                                                        \

}
\
} while(0)

With -Wall it generates the following warning: cast from 'float *' to
'size_t *' (aka 'unsigned long long *') increases required alignment from 4
to 8 [-Wcast-align], triggered by the indicated line, in case the type of
pointer is smaller than size_t.

I get the gist of it, but reading the std (taking cppreference.com's word
for it), this seems un-necessary.

malloc: "If allocation succeeds, returns a pointer to the lowest (first)
byte in the allocated memory block *that is suitably aligned for any object
type*.".
and aligned_alloc: "Regular malloc aligns memory suitable for any object
type (which, in practice, *means that it is aligned to
alignof(max_align_t))*.", which means long double.
It appears that even on Windows (without a real long double), alignment
*is* at least 16.

No warning on this issue from VS 15.6.6.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20180412/55c619e3/attachment.html>


More information about the cfe-users mailing list