Re: C programming on ARM




"Grant Edwards" <grante@xxxxxxxx> wrote in message news:13q263sdle28k54@xxxxxxxxxxxxxxxxxxxxx
On 2008-01-31, Wilco Dijkstra <Wilco_dot_Dijkstra@xxxxxxxxxxxx> wrote:

"Grant Edwards" <grante@xxxxxxxx> wrote in message news:13q21msqkij3dbb@xxxxxxxxxxxxxxxxxxxxx
On 2008-01-30, Wilco Dijkstra <Wilco_dot_Dijkstra@xxxxxxxxxxxx> wrote:

Actually most architectures are byte addressable, including ARM,

True, but many of them (ARM, SPARC, MSP430, ...) have alignment
requirements for objects larger than 1 byte in size.

Byte addressability refers to the smallest unit of
addressability, not to the coursest.

I know.

so casting pointers just works.

I think the disconnect is in what you meant by "just works". We
assumed you meant it resulted in a pointer that could be
dereferences. Apparently you meant that it could be cast to
someother type, then cast back to the original type and result
in the same pointer?

OK, so I was a bit short on the explanation. I meant that pointers
contain no magic bits or other info besides the bit pattern of the
address. Therefore you can cast an integer to a pointer and
back, or a pointer to any other pointer type and back and you do
not lose a single bit of information. All this is guaranteed if you
have a byte addressable architecture, ie. pointer casting is safe.

Dereferencing pointers is something entirely different altogether.
Not all pointers can be dereferenced, think about the null pointer,
or pointers that point just beyond a variable (something C permits).
Also it is never a good idea to cast a pointer to a small object to
a larger one as you will corrupt memory if you access beyond the
original object. Natural alignment is just another thing to take into
account (however many compilers support unaligned types).

Wilco


.



Relevant Pages

  • Re: Why strNlen is not in Std ?
    ... main reason for which C allows pointer to vary in size. ... C could have added bitwise addressability with no change ... there is at least 2: KCC and gcc. ... are non conforming -- 7 is too short and neither allow char to cover all ...
    (comp.std.c)
  • Re: Typecasting pointers
    ... less strict alignment, it may be cast back and is promised to compare ... A pointer to an object or incomplete type may be converted to ...
    (comp.lang.c)
  • Re: Typecasting pointers
    ... less strict alignment, it may be cast back and is promised to compare ... A pointer to an object or incomplete type may be converted to ...
    (comp.lang.c)
  • Re: cast-as-lvalue (Thank You)
    ... pointer after assigning a value to the object to which dest points, ... the cast may not be valid, and even if it is valid it may not point to ... memory with appropriate alignment. ...
    (comp.lang.c)
  • Re: please explain why this is Undefined Behavior
    ... >> alignment issues. ... > You can cast a pointer to any type. ... > Casting char * to other pointer types is commonly performed in the ...
    (comp.lang.cpp)