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


Groups > comp.lang.c > #41491

Re: Padding involved

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Padding involved
Date 2014-03-08 14:03 -0800
Organization None to speak of
Message-ID <lna9d0uz52.fsf@nuthaus.mib.org> (permalink)
References <43b13d99-38eb-4711-824d-5fc40d55edf3@googlegroups.com> <vjrSu.26037$r53.19153@fx02.am4> <lnvbvpvbvw.fsf@nuthaus.mib.org> <m%BSu.56526$NZ3.32303@fx33.am4>

Show all headers | View raw


"BartC" <bc@freeuk.com> writes:
> "Keith Thompson" <kst-u@mib.org> wrote in message
> news:lnvbvpvbvw.fsf@nuthaus.mib.org...
>> "BartC" <bc@freeuk.com> writes:
>
>> [...]
>>> #pragma pack(1)
>> [...]
>>
>> The OP may not be aware that #pragma pack is non-standard.  It's an
>> extension implemented by gcc (and probably other C compilers).
>
> I mentioned that in my post.

Sorry I missed that.  But you wrote that it "will vary between
compilers".  Not all compilers necessarily have a way to specify
packing of structure members.

> "Keith Thompson" <kst-u@mib.org> wrote in message
> news:lnr46dv8ol.fsf@nuthaus.mib.org...
>
>> One more nitpick: sizeof yields a result of type size_t; the "%d"
>> format requires an argument of type int.  Use the "%zu" format, or
>> convert the sizeof result to int (or to unsigned long and use "%lu").
>
> But you didn't pick on the dozen or so non-uses of "%zu" in the rest of my
> post.

I was replying to someone else.  I don't point out every error in every
post.

> However I dislike having to remember and use all these weird and wonderful
> format specifiers (a 'zoo' of them almost!). If the right one is that
> important, then the compiler should tell me about it (but only lccwin32
> seems to do so at default warning levels).

gcc warns about about mismatches between format strings and arguments,
at least in many cases.  But warning about such mismatches in all cases
is not possible.  Format strings are interpreted at run time.  A format
string is commonly a string literal, but needn't be.  You just have to
develop the habit of using the right format yourself if you want to
avoid undefined behavior.

> Ideally it should figure it out for itself (a few years ago, I proposed a %?
> specifier for that purpose, for use in the 99.9% of cases where the format
> string was a constant). Because managing these format strings can be a lot
> of work (you change one type from int to long long, then you have to change
> hundreds of %d to %lld or %x to %llx).

You can always convert the argument to a known type.  For example, if u
is of some unsigned type, but you're not sure which one, you can do:

    printf("%llu\n", (unsigned long long)u);

Or if you happen to know that the value of u is fairly small (say,
because it's the size of a structure that you know is smaller than 32
kbytes), you can just convert to int:

    printf("%d\n", (int)sizeof whatever);

> (FWIW, not using %zu doesn't seem to matter on my machine; when I'm 
> compiling for 64-bits and a size_t value occupies 8 bytes, while int is 4 
> bytes, then presumably the parameter stack is also 64-bit aligned so use of 
> %d seems to have no ill-effects. Tested with 3 x64 compilers.)

I see the same behavior.  I wouldn't be surprised to see it fail on a
big-endian system.  (Actually I just tried it and it "worked"; I'm not
sure why.)

But by using the correct format, perhaps with a cast, I don't have to
worry about it; I know it will work.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

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


Thread

Padding involved anish singh <anish198519851985@gmail.com> - 2014-03-07 13:13 -0800
  Re: Padding involved jacob navia <jacob@spamsink.net> - 2014-03-07 22:17 +0100
  Re: Padding involved Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-03-07 16:26 -0500
    Re: Padding involved anish singh <anish198519851985@gmail.com> - 2014-03-07 13:49 -0800
      Re: Padding involved Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-03-07 18:11 -0500
        Re: Padding involved anish kumar <yesanishhere@gmail.com> - 2014-03-07 15:19 -0800
          Re: Padding involved Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2014-03-07 16:26 -0700
            Re: Padding involved anish kumar <yesanishhere@gmail.com> - 2014-03-07 15:41 -0800
              Re: Padding involved James Kuyper <jameskuyper@verizon.net> - 2014-03-07 18:50 -0500
            Re: Padding involved glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-08 00:10 +0000
              Re: Padding involved Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2014-03-08 09:27 -0700
          Re: Padding involved Keith Thompson <kst-u@mib.org> - 2014-03-07 16:24 -0800
            Re: Padding involved anish kumar <yesanishhere@gmail.com> - 2014-03-07 16:45 -0800
              Re: Padding involved Kaz Kylheku <kaz@kylheku.com> - 2014-03-08 01:03 +0000
                Re: Padding involved David Thompson <dave.thompson2@verizon.net> - 2014-03-28 06:11 -0400
                Re: Padding involved glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-28 18:45 +0000
                Re: Padding involved James Kuyper <jameskuyper@verizon.net> - 2014-03-28 15:23 -0400
                Re: Padding involved Keith Thompson <kst-u@mib.org> - 2014-03-28 12:40 -0700
                Re: Padding involved glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-28 21:19 +0000
                Re: Padding involved Stephen Sprunk <stephen@sprunk.org> - 2014-03-28 17:07 -0500
                Re: Padding involved Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-03-28 18:25 -0400
                Re: Padding involved Stephen Sprunk <stephen@sprunk.org> - 2014-03-28 21:15 -0500
                Re: Padding involved Keith Thompson <kst-u@mib.org> - 2014-03-29 01:03 -0700
                Re: Padding involved Tim Rentsch <txr@alumni.caltech.edu> - 2014-03-29 14:23 -0700
                Re: Padding involved glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-30 01:43 +0000
                Re: Padding involved James Kuyper <jameskuyper@verizon.net> - 2014-03-28 21:32 -0400
                Re: Padding involved Richard Damon <Richard@Damon-Family.org> - 2014-03-28 21:59 -0400
                Re: Padding involved Stephen Sprunk <stephen@sprunk.org> - 2014-03-29 00:46 -0500
                Re: Padding involved James Kuyper <jameskuyper@verizon.net> - 2014-03-29 08:51 -0400
                Re: Padding involved Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-03-29 08:57 -0400
                Re: Padding involved Stephen Sprunk <stephen@sprunk.org> - 2014-03-30 18:53 -0500
                Re: Padding involved Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-03-30 20:25 -0400
                Re: Padding involved Stephen Sprunk <stephen@sprunk.org> - 2014-03-31 11:34 -0500
                Re: Padding involved Keith Thompson <kst-u@mib.org> - 2014-03-31 11:27 -0700
                Re: Padding involved Keith Thompson <kst-u@mib.org> - 2014-03-31 11:36 -0700
                Re: Padding involved Kaz Kylheku <kaz@kylheku.com> - 2014-03-31 19:14 +0000
                Re: Padding involved Stephen Sprunk <stephen@sprunk.org> - 2014-03-31 15:43 -0500
                Re: Padding involved Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2014-04-01 03:12 +0300
                Re: Padding involved Stephen Sprunk <stephen@sprunk.org> - 2014-04-03 00:45 -0500
                Re: Padding involved Stephen Sprunk <stephen@sprunk.org> - 2014-03-29 13:02 -0500
                Re: Padding involved Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-03-29 14:44 -0400
                Re: Padding involved glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-29 19:49 +0000
                Re: Padding involved James Kuyper <jameskuyper@verizon.net> - 2014-03-30 19:12 -0400
                Re: Padding involved Stephen Sprunk <stephen@sprunk.org> - 2014-03-31 16:20 -0500
                Re: Padding involved James Kuyper <jameskuyper@verizon.net> - 2014-03-31 17:53 -0400
                Re: Padding involved Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2014-04-01 03:22 +0300
                Re: Padding involved Stephen Sprunk <stephen@sprunk.org> - 2014-04-04 14:36 -0500
                Re: Padding involved James Kuyper <jameskuyper@verizon.net> - 2014-04-04 17:33 -0400
                Re: Padding involved Tim Rentsch <txr@alumni.caltech.edu> - 2014-04-14 13:46 -0700
                Re: Padding involved glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-04-14 22:00 +0000
                Re: Padding involved Keith Thompson <kst-u@mib.org> - 2014-04-14 15:37 -0700
                Re: Padding involved Tim Rentsch <txr@alumni.caltech.edu> - 2014-04-17 09:11 -0700
                Re: Padding involved Tim Rentsch <txr@alumni.caltech.edu> - 2014-04-14 13:25 -0700
                Re: Padding involved Keith Thompson <kst-u@mib.org> - 2014-03-29 00:57 -0700
                Re: Padding involved James Kuyper <jameskuyper@verizon.net> - 2014-03-29 08:57 -0400
                Re: Padding involved Keith Thompson <kst-u@mib.org> - 2014-03-29 14:18 -0700
                Re: Padding involved Tim Rentsch <txr@alumni.caltech.edu> - 2014-03-30 12:02 -0700
                Re: Padding involved Keith Thompson <kst-u@mib.org> - 2014-03-30 19:35 -0700
                Re: Padding involved Tim Rentsch <txr@alumni.caltech.edu> - 2014-04-14 12:54 -0700
                Re: Padding involved Tim Rentsch <txr@alumni.caltech.edu> - 2014-03-29 14:44 -0700
                Re: Padding involved glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-29 03:50 +0000
                Re: Padding involved James Kuyper <jameskuyper@verizon.net> - 2014-03-29 09:02 -0400
                Re: Padding involved Stephen Sprunk <stephen@sprunk.org> - 2014-03-29 13:19 -0500
                Re: Padding involved James Kuyper <jameskuyper@verizon.net> - 2014-03-28 21:21 -0400
                Re: Padding involved Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-03-28 15:27 -0400
                Re: Padding involved Kaz Kylheku <kaz@kylheku.com> - 2014-03-28 19:54 +0000
                Re: Padding involved Stephen Sprunk <stephen@sprunk.org> - 2014-03-28 15:02 -0500
          Re: Padding involved Kaz Kylheku <kaz@kylheku.com> - 2014-03-08 00:43 +0000
          Re: Padding involved Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-03-07 20:20 -0500
    Re: Padding involved anish singh <anish198519851985@gmail.com> - 2014-03-07 13:49 -0800
  Re: Padding involved James Kuyper <jameskuyper@verizon.net> - 2014-03-07 16:46 -0500
  Re: Padding involved "BartC" <bc@freeuk.com> - 2014-03-07 21:59 +0000
    Re: Padding involved anish kumar <yesanishhere@gmail.com> - 2014-03-07 14:34 -0800
      Re: Padding involved "BartC" <bc@freeuk.com> - 2014-03-07 23:59 +0000
    Re: Padding involved Keith Thompson <kst-u@mib.org> - 2014-03-07 15:15 -0800
      Re: Padding involved "BartC" <bc@freeuk.com> - 2014-03-08 10:08 +0000
        Re: Padding involved Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-08 12:39 +0000
        Re: Padding involved Keith Thompson <kst-u@mib.org> - 2014-03-08 14:03 -0800
  Re: Padding involved Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-03-08 09:01 -0800

csiph-web