Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #91922

Re: Structure packing & padding: what sizeof reports? Any caveats?

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c
Subject Re: Structure packing & padding: what sizeof reports? Any caveats?
Date 2016-08-08 19:36 +1200
Message-ID <e0qr3cFfpjhU1@mid.individual.net> (permalink)
References <inbgqb9dm89a0el2m4sh8ntqb8541r7miu@4ax.com>

Show all headers | View raw


On 08/ 8/16 07:15 PM, Test wrote:
> I am saving structures and variables to a string. For example:
>
> ...
> struct MixedData
> {
>      char Data1;
>      int num1;
>      short num2;
> };
>
> ....
> blah blah
> ...
>
> char mystr[256];
> int i;
> struct MixedData whatever;
>
> ....
> blah blah
> ...
>
> i=55;
> whatever.num1=60
> memcpy(&mystr[4],&whatever,sizeof(struct MixedData));
>
> ....
> blah blah
> ...
> memcpy(&whatever,&mystr[4],sizeof(struct MixedData));
> // i should be now 60
>
> Is above OK or should I worry about if my structed is padded or packed, ie. will
> sizeof() report correct value.

The value returned by sizeof includes any packing bytes.

-- 
Ian

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Structure packing & padding: what sizeof reports? Any caveats? Test <test@.nil.invalid.com> - 2016-08-08 10:15 +0300
  Re: Structure packing & padding: what sizeof reports? Any caveats? Ian Collins <ian-news@hotmail.com> - 2016-08-08 19:36 +1200
    Re: Structure packing & padding: what sizeof reports? Any caveats? Richard Heathfield <rjh@cpax.org.uk> - 2016-08-08 08:52 +0100
  Re: Structure packing & padding: what sizeof reports? Any caveats? Richard Heathfield <rjh@cpax.org.uk> - 2016-08-08 08:51 +0100
    Re: Structure packing & padding: what sizeof reports? Any caveats? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-08-08 11:29 +0100
    Re: Structure packing & padding: what sizeof reports? Any caveats? Keith Thompson <kst-u@mib.org> - 2016-08-08 08:56 -0700
  Re: Structure packing & padding: what sizeof reports? Any caveats? Barry Schwarz <schwarzb@dqel.com> - 2016-08-08 01:37 -0700
  Re: Structure packing & padding: what sizeof reports? Any caveats? James Kuyper <jameskuyper@verizon.net> - 2016-08-08 11:17 -0400

csiph-web