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


Groups > comp.lang.c > #91925

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

From Richard Heathfield <rjh@cpax.org.uk>
Newsgroups comp.lang.c
Subject Re: Structure packing & padding: what sizeof reports? Any caveats?
Date 2016-08-08 08:51 +0100
Organization Origin Sonata
Message-ID <no9dlh$afe$1@dont-email.me> (permalink)
References <inbgqb9dm89a0el2m4sh8ntqb8541r7miu@4ax.com>

Show all headers | View raw


On 08/08/16 08:15, 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;

What's this for?

> whatever.num1=60

You forgot the semicolon, so you're going to get a diagnostic message 
from your compiler. More importantly, whatever.Data1 and whatever.num2 
remain indeterminate.

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

memcpy treats every byte as if it were an unsigned char, so you'll get 
away this this copy operation, but from this point on, the content of 
mystr must be handled very carefully, as much of that content has an 
indeterminate value.

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

It's horrible, and it isn't guaranteed to work. Your hard-coded 4 is one 
of your problems, since it assumes that there are exactly three padding 
bytes after the Data1 member.

> or should I worry about if my structed is padded or packed,

Padding is legal (but not required) after any structure member, and 
there is no upper limit on the number of bytes of padding that can be used.

> ie. will
> sizeof() report correct value.

sizeof will always report the number of bytes required to store its 
operand, and that number will always be correct, given a correct 
program. (In other words, if you stick to the rules, so will the 
compiler.) But that size may well include padding bytes.

> I am using GCC

If that matters, you need to be asking in a gcc group. And if it doesn't 
matter, it doesn't matter.

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

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