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


Groups > comp.lang.c > #23377

Re: Dereferencing type-punned pointer

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Dereferencing type-punned pointer
Date 2012-07-18 14:14 -0700
Organization None to speak of
Message-ID <lnsjcobwm2.fsf@nuthaus.mib.org> (permalink)
References <ju7722$ttq$2@speranza.aioe.org>

Show all headers | View raw


John May <nospam@nospam.com> writes:
>   I keep getting this warning in GCC with the code segment below.
>
> file.c:256 "warning: dereferencing type-punned pointer will break
> strict-aliasing rules"
>
> /*****************************************/
> char buffer[SIZE];
> int length, type;
>
> /* Code to fill buffer */
>
> length = ntohs( *((unsigned short *)buffer) );
> type = ntohs( *((unsigned short *)buffer + 1) );
>
> /*****************************************/
>
>
> I'm trying to take the first two bytes (in network byte order) off the
> buffer and put them in the length, followed by the next two bytes into
> the type.  GCC complains about the length conversion, but not the type.
>
> What does this warning mean, and should I bother with it?

Assuming buffer is aligned at an even address, buffer + 1 is at an odd
address; attempting to read a short object from an odd address can
easily crash your program. (x86 systems, I think, don't impose strict
alignment requirements.)

If you want to copy bytes from an array of char into a larger integer,
either do so explicitly or use memcpy().

You also appear to be making some assumptions about the sizes of short
and int. You should at least document those assumptions.

> I don't want to keep checking this forum, so answers by email required. 
> Quick responses appreciated.

Answers posted here are not just for your benefit; they're for everyone
who reads this newsgroup. Feel free to explain why you should get
special treatment, but don't expect to convince anyone. Post here, get
your answers here.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"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 | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Dereferencing type-punned pointer John May <nospam@nospam.com> - 2012-07-18 20:41 +0000
  Re: Dereferencing type-punned pointer Keith Thompson <kst-u@mib.org> - 2012-07-18 14:14 -0700
    Re: Dereferencing type-punned pointer Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-19 01:33 +0100
  Re: Dereferencing type-punned pointer Keith Thompson <kst-u@mib.org> - 2012-07-18 14:17 -0700
    Re: Dereferencing type-punned pointer Ike Naar <ike@sverige.freeshell.org> - 2012-07-19 06:16 +0000
      Re: Dereferencing type-punned pointer Keith Thompson <kst-u@mib.org> - 2012-07-19 01:35 -0700
        Re: Dereferencing type-punned pointer Ike Naar <ike@sdf.lonestar.org> - 2012-07-19 14:11 +0000
  Re: Dereferencing type-punned pointer Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-19 01:56 +0100
    Re: Dereferencing type-punned pointer Nobody <nobody@nowhere.com> - 2012-07-20 07:40 +0100

csiph-web