Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #106790
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: struct padding question |
| Date | 2017-03-23 11:28 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <ob07qd$if0$1@dont-email.me> (permalink) |
| References | <ob062g$coi$1@dont-email.me> |
On 23/03/17 10:58, MehdiAmini wrote:
>
> // struct padding question
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <inttypes.h>
>
> #define N 6
>
> static struct data
> { uint64_t ctr[N];
> long double mean;
> long double variance;
> long double * probability_ptr;
>
> } my_data;
>
> void init_data( struct data* const);
>
> int main(void)
> {
> init_data(&my_data);
>
> return EXIT_SUCCESS;
> }
>
> void init_data(struct data * const data_ptr)
> {
> data_ptr->probability_ptr = NULL;
> for(long i = 0; i<N; i++)
> data_ptr->ctr[i] = 0;
> data_ptr->mean = (long double)0.0;
> data_ptr->variance = (long double)0.0;
>
> return;
> }
>
> // Compiling the above code I get the warning, how to fix it?
>
> clang -pedantic -Weverything -Wall -m64 -std=c11
>
> warning: padding size of 'struct data' with 8 bytes to alignment
> boundary [-Wpadded]
> static struct data
> ^
When you define a struct in C, the compiler may add extra "padding"
bytes in order to make sure all the alignments are correct for the
struct members. It may also add padding at the end of the struct, so
that alignments would be correct in an array of the structs.
This is normal C behaviour. But sometimes, people don't want to waste
this space, or they may have expected their struct to fit accurately so
that padding indicates a mistake in their code. Thus some compilers
(gcc and clang, certainly, but maybe many others) can warn if they add
padding. This is controlled by the "-Wpadded" or "-Wno-padded" flags.
Normally, this warning is disabled - gcc does not include it in "-Wall
-Wextra". Apparently, clang has it in "-Weverything".
You may decide that this warning is not a problem - for most code,
padding bytes are not an issue. Then you simply add "-Wno-padded" to
your compiler line. This does not change your code, but it hides the
warning.
Alternatively, you need to re-arrange your struct so that there are no
paddings needed.
The struct you have now is:
struct data {
uint64_t ctr[6]; // 8-byte alignment, 8x6 data
long double mean; // 16-byte alignment, 16-byte data
long double variance; // 16-byte alignment, 16-byte data
long double * p_ptr; // 8-byte alignment, 8-byte data
// inserted by compiler...
uint64_t padding; // 8-byte alignment, 8-byte data
};
This final hidden padding is added to make the whole struct a multiple
of 16 bytes. If you add it explicitly, you will get the same structure
in your code and no warning.
Note that such alignments and paddings are implementation-dependent - on
an implementation with 8-byte "long double", or a platform with an 8
byte (or less) maximum alignment, there would be no padding here.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
struct padding question MehdiAmini <atorrses@gmail.com> - 2017-03-23 14:28 +0430
Re: struct padding question David Brown <david.brown@hesbynett.no> - 2017-03-23 11:28 +0100
Re: struct padding question MehdiAmini <atorrses@gmail.com> - 2017-03-23 16:44 +0430
Re: struct padding question jm.bourguet@gmail.com - 2017-03-23 05:30 -0700
Re: struct padding question Keith Thompson <kst-u@mib.org> - 2017-03-23 08:45 -0700
Re: struct padding question Noob <root@127.0.0.1> - 2017-04-01 19:15 +0200
Re: struct padding question tim prince <n8tm@aol.com> - 2017-04-01 14:41 -0400
Re: struct padding question supercat@casperkitty.com - 2017-04-02 09:15 -0700
Re: struct padding question scott@slp53.sl.home (Scott Lurndal) - 2017-04-03 13:51 +0000
Re: struct padding question supercat@casperkitty.com - 2017-04-03 07:48 -0700
Re: struct padding question scott@slp53.sl.home (Scott Lurndal) - 2017-04-03 15:47 +0000
Re: struct padding question supercat@casperkitty.com - 2017-04-03 09:24 -0700
Re: struct padding question Keith Thompson <kst-u@mib.org> - 2017-04-01 11:43 -0700
Re: struct padding question Robert Wessel <robertwessel2@yahoo.com> - 2017-04-01 13:46 -0500
Re: struct padding question Keith Thompson <kst-u@mib.org> - 2017-04-01 12:00 -0700
Re: struct padding question Robert Wessel <robertwessel2@yahoo.com> - 2017-04-01 16:40 -0500
Re: struct padding question Keith Thompson <kst-u@mib.org> - 2017-04-01 15:24 -0700
Re: struct padding question Robert Wessel <robertwessel2@yahoo.com> - 2017-04-03 17:46 -0500
Re: struct padding question BartC <bc@freeuk.com> - 2017-03-23 11:50 +0000
Re: struct padding question David Brown <david.brown@hesbynett.no> - 2017-03-23 14:08 +0100
Re: struct padding question BartC <bc@freeuk.com> - 2017-03-23 13:31 +0000
Re: struct padding question David Brown <david.brown@hesbynett.no> - 2017-03-23 14:46 +0100
Re: struct padding question BartC <bc@freeuk.com> - 2017-03-23 14:07 +0000
Re: struct padding question David Brown <david.brown@hesbynett.no> - 2017-03-23 15:52 +0100
Re: struct padding question scott@slp53.sl.home (Scott Lurndal) - 2017-03-23 17:19 +0000
Re: struct padding question David Brown <david.brown@hesbynett.no> - 2017-03-23 19:57 +0100
Re: struct padding question Ian Pilcher <arequipeno@gmail.com> - 2017-03-23 08:52 -0500
Re: struct padding question David Brown <david.brown@hesbynett.no> - 2017-03-23 15:54 +0100
Re: struct padding question supercat@casperkitty.com - 2017-03-23 08:29 -0700
Re: struct padding question GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-03-23 14:04 +0100
Re: struct padding question MehdiAmini <atorrses@gmail.com> - 2017-03-23 20:32 +0430
Re: struct padding question GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-03-23 18:35 +0100
Re: struct padding question supercat@casperkitty.com - 2017-03-23 12:02 -0700
Re: struct padding question Keith Thompson <kst-u@mib.org> - 2017-03-23 12:21 -0700
Re: struct padding question supercat@casperkitty.com - 2017-03-23 12:55 -0700
Re: struct padding question Les Cargill <lcargill99@comcast.com> - 2017-03-25 15:12 -0500
csiph-web