Do you have a question? Post it now! No Registration Necessary
Subject
- Posted on
Re: writing value to pointer
- 07-17-2003
- Paul Pluzhnikov
July 17, 2003, 3:02 pm


If you lie to a computer, it will get you. You are telling gcc that
ptr1 is a valid pointer to uint16, which it isn't.

You are lucky it doesn't crash. Writing to a multi-byte item through
a mis-aligned pointer invokes undefined behaviour.

By writing correct code, perhaps?
uint16 val = 0x1234;
memcpy(&buffer[1], &val, sizeof(val));
Cheers,
--
In order to understand recursion you must first understand recursion.
In order to understand recursion you must first understand recursion.

Re: writing value to pointer

If the compiler designer wanted (I'm not claiming this is the
case in any extant C compiler), the packed structure members
could be marked as "unalgned" so that when the code generator is
called it's told to do byte-by-byte accesses.
AFAIK, gcc doesn't do this. It would be awfully nice for
filling in those communication headers...
--
Grant Edwards grante Yow! TAILFINS!!...click...
at
Grant Edwards grante Yow! TAILFINS!!...click...
at
We've slightly trimmed the long signature. Click to see the full one.

Re: writing value to pointer

You can't. One of the "limitations" of the arm processors (I believe it
affects all of them) is data must be aligned correctly. That is 16 bit
values aligned on 16 bit boundaries, 32 bit values on 32 bit boundaries. In
the case above, the arm processor effectively rounds the pointer down to a
"correct" boundary. If you absolutely have to do this, you will need to use
memcpy() or something similar to copy a byte at a time.
Blane.

Re: writing value to pointer

The compiler code is OK - see the generated assembly code with gcc -S.
Your problem is that the ARM hardware does weird things if you access a
halfword (16 bits) or a fullword (32 bits) on a non-natural boundary. The
halfword accesses assume that least significant bit of the address is zero,
the fullword accesses assume that two least significant bits of the address
are zeroes.
If you have a __attribute__((__packed__)) structure with non-aligned
members, the compiler splits and packs the data in software, so a single
item is accessed in small enough pieces to be aligned. This may lead to a
fullword accessed as four discrete bytes.
The ARM ARM
David Seal, ARM Achitecture Reference Manual, Addison-Wesley, ISBN
0-201-73719-1
explicitly states that the result of an unaligned data access is UNDEFINED.
For ARM7TDMI, such as Atmel AT91, the behaviour is described in the ARM7TDMI
Data Sheet, available at
<http://www.atmel.com/dyn/resources/prod_documents/Arm7tdmi.PDF>.
HTH
Tauno Voipio
tauno voipio @ iki fi
Site Timeline
- » Starting project with MPC8260 (considering Montavista)
- — Next thread in » Embedded Linux
-
- » Multithreaded program debugging on arm with gdbserver
- — Previous thread in » Embedded Linux
-
- » Crosscompiling for ARM: reloc type R_ARM_ABS32 is not supported for PIC - ...
- — Newest thread in » Embedded Linux
-
- » Communist Chinese Military Companies
- — The site's Newest Thread. Posted in » Field-Programmable Gate Arrays
-