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


Groups > comp.lang.c > #91959

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

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: Structure packing & padding: what sizeof reports? Any caveats?
Date 2016-08-08 11:17 -0400
Organization A noiseless patient Spider
Message-ID <noa7pf$tnp$1@dont-email.me> (permalink)
References <inbgqb9dm89a0el2m4sh8ntqb8541r7miu@4ax.com>

Show all headers | View raw


On 08/08/2016 03:15 AM, 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

Richard Heathfield has already commented on the missing semicolon on
that line.

> memcpy(&mystr[4],&whatever,sizeof(struct MixedData));

I prefer mystr+4 over &mystr[4], just because it's simpler.
I prefer sizeof whatever over sizeof(struct MixedData), because it will
continue to calculate the correct quantity, even if you change the type
of whatever.

You're assuming mystr is at least 4 bytes bigger than whatever. That
assumption is probably correct, but I'd recommend either testing that
assumption:

    if(4+sizeof whatever > sizeof mystr)
    {
        // Error handling
    }

 or re-writing the code to guarantee that mystr is big enough:

    struct MixedData whatever;
    char mystr[4 + sizeof whatever];

By the way - what is the purpose of the "4"?

> ...
> blah blah
> ..
> memcpy(&whatever,&mystr[4],sizeof(struct MixedData));
> // i should be now 60

You last time a value was written to i, that value was 55. Why do you
think it would now be 60? For that matter, what is the purpose of i?


> Is above OK or should I worry about if my structed is padded or packed, ie. will
> sizeof() report correct value.

sizeof should always report the actual size, including padding bits or
bytes.

Back to comp.lang.c | Previous | Next — Previous 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