Use alignof in gcc

alignof is defined in stdalign.h for gcc:

$ cat /usr/lib/gcc/x86_64-unknown-linux-gnu/9.1.0/include/stdalign.h
......
#ifndef _STDALIGN_H
#define _STDALIGN_H

#ifndef __cplusplus

#define alignas _Alignas
#define alignof _Alignof

#define __alignas_is_defined 1
#define __alignof_is_defined 1

#endif

#endif  /* stdalign.h */

To use alignofstdalign.h must be included, otherwise following errors will be reported:

......
warning: implicit declaration of function 'alignof' [-Wimplicit-function-declaration]
    3 |  return alignof(int);
      |         ^~~~~~~
 error: expected expression before

It is no need to include any header files to use _Alignof and __alignof__.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.