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


Groups > comp.lang.c > #10836

Re: Inconsistent results between char/short/int

From Eric Sosman <esosman@ieee-dot-org.invalid>
Newsgroups comp.lang.c
Subject Re: Inconsistent results between char/short/int
Date 2011-09-23 20:21 -0400
Organization A noiseless patient Spider
Message-ID <j5j7qd$pr1$1@dont-email.me> (permalink)
References <j5i2ki$72q$1@ccpntc8.in2p3.fr>

Show all headers | View raw


On 9/23/2011 9:46 AM, Guillaume Dargaud wrote:
> Hello all,
> I know it's frowned upon to post system-specific code here, but I'd like to
> know if the error is in my C code (wrong pointer manipulation), in my
> handling of Linux virtual mem or in weird CPU mem alignments (it's a PPC
> processor).
>[...]
> # ./Peek -w 0x81600001 0x10
> 81600001~48006001: 00000000 00000000 00000000 00000000  ................
> 81600011~48006011: 00000000 00000000 00000000 00000000  ................
> 81600021~48006021: 00000000 00000000 00000000 00000000  ................
> 81600031~48006031: 00000000 00000000 00000000 00000000  ................

     In addition to the comments others have offered, it's quite
possible that your processor is unhappy trying to fetch four-byte
integers from odd addresses.  Different processors will behave
differently in the face of improper alignment; among the behaviors
I've actually seen are

     - The processor traps, and the O/S terminates the program
       (often with "SIGBUS" or something like it).

     - The processor traps, and low-level code simulates the
       invalid operation in software, loading bytes one by one
       and assembling the result for you.  The program works but
       takes a thousand or so times as long as if the access had
       been properly aligned.

     - The processor handles the load-and-assemble part without a
       trap and without software intervention.  The program works
       but takes two to eight times as long as normal.

     - The processor completely ignores the offending low-order
       address bits, so when you fetch an int from 0x81600001 you
       get the four bytes 0x81600000-0x81600003 instead of the
       intended 0x81600001-0x81600004.  Peculiar results ensue.

     This list is not exhaustive of all possible outcomes; it's
just the outcomes I personally have observed.  And it's meant as
a warning that your program is on uncertain ground ...

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

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


Thread

Inconsistent results between char/short/int Guillaume Dargaud <use_the_contact_form@www.gdargaud.net> - 2011-09-23 15:46 +0200
  Re: Inconsistent results between char/short/int Noob <root@127.0.0.1> - 2011-09-23 16:44 +0200
  Re: Inconsistent results between char/short/int Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-23 17:16 +0100
    Re: Inconsistent results between char/short/int Guillaume Dargaud <use_the_contact_form@www.gdargaud.net> - 2011-09-29 14:53 +0200
  Re: Inconsistent results between char/short/int Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-23 20:21 -0400
    Re: Inconsistent results between char/short/int Nobody <nobody@nowhere.com> - 2011-09-24 18:26 +0100
      Re: Inconsistent results between char/short/int Guillaume Dargaud <use_the_contact_form@www.gdargaud.net> - 2011-09-29 14:50 +0200
    Re: Inconsistent results between char/short/int Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-09-29 21:57 +0000

csiph-web