Commit | Line | Data |
---|---|---|
5e8754f9 | 1 | # serial 4 |
49e4877c PA |
2 | # Check for flexible array member support. |
3 | ||
5e8754f9 | 4 | # Copyright (C) 2006, 2009-2016 Free Software Foundation, Inc. |
49e4877c PA |
5 | # This file is free software; the Free Software Foundation |
6 | # gives unlimited permission to copy and/or distribute it, | |
7 | # with or without modifications, as long as this notice is preserved. | |
8 | ||
9 | # Written by Paul Eggert. | |
10 | ||
11 | AC_DEFUN([AC_C_FLEXIBLE_ARRAY_MEMBER], | |
12 | [ | |
13 | AC_CACHE_CHECK([for flexible array member], | |
14 | ac_cv_c_flexmember, | |
15 | [AC_COMPILE_IFELSE( | |
16 | [AC_LANG_PROGRAM( | |
17 | [[#include <stdlib.h> | |
18 | #include <stdio.h> | |
19 | #include <stddef.h> | |
5e8754f9 | 20 | struct s { int n; double d[]; };]], |
49e4877c PA |
21 | [[int m = getchar (); |
22 | size_t nbytes = offsetof (struct s, d) + m * sizeof (double); | |
23 | nbytes += sizeof (struct s) - 1; | |
24 | nbytes -= nbytes % sizeof (struct s); | |
25 | struct s *p = malloc (nbytes); | |
26 | p->d[0] = 0.0; | |
27 | return p->d != (double *) NULL;]])], | |
28 | [ac_cv_c_flexmember=yes], | |
29 | [ac_cv_c_flexmember=no])]) | |
30 | if test $ac_cv_c_flexmember = yes; then | |
31 | AC_DEFINE([FLEXIBLE_ARRAY_MEMBER], [], | |
32 | [Define to nothing if C supports flexible array members, and to | |
33 | 1 if it does not. That way, with a declaration like 'struct s | |
34 | { int n; double d@<:@FLEXIBLE_ARRAY_MEMBER@:>@; };', the struct hack | |
35 | can be used with pre-C99 compilers. | |
36 | When computing the size of such an object, don't use 'sizeof (struct s)' | |
37 | as it overestimates the size. Use 'offsetof (struct s, d)' instead. | |
38 | Don't use 'offsetof (struct s, d@<:@0@:>@)', as this doesn't work with | |
39 | MSVC and with C++ compilers.]) | |
40 | else | |
41 | AC_DEFINE([FLEXIBLE_ARRAY_MEMBER], [1]) | |
42 | fi | |
43 | ]) |