EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

C programming on ARM

Started by aamer January 30, 2008
On 2008-01-30, Wilco Dijkstra <Wilco_dot_Dijkstra@ntlworld.com> 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.
> so casting pointers just works.
Unless the resulting pointer is misaligned.
> Of course it's essential to know what you're doing - well > written programs rarely need pointer casts.
-- Grant Edwards grante Yow! It was a JOKE!! at Get it?? I was receiving visi.com messages from DAVID LETTERMAN!! !
Wilco Dijkstra wrote:

>>> With any byte addressable architecture, a pointer is just a pointer.
>> But there are quite a few non-byte addressable architectures out there. >> Including ARM, last I checked.
> Actually most architectures are byte addressable, including ARM,
Depends on what exactly you mean by the term. AFAIK ARM7 is byte-addressable only for bytes, but not for larger objects. Well, may be it's just me, but I wouldn't call what happens when an ARM7 accesses a 32-bit integer without 4-byte alignment "addressing".
"Hans-Bernhard Br&#4294967295;ker" <HBBroeker@t-online.de> wrote in message news:fnr0vs$mp6$00$1@news.t-online.com...
> Wilco Dijkstra wrote: > >>>> With any byte addressable architecture, a pointer is just a pointer. > >>> But there are quite a few non-byte addressable architectures out there. >>> Including ARM, last I checked. > >> Actually most architectures are byte addressable, including ARM, > > Depends on what exactly you mean by the term. > > AFAIK ARM7 is byte-addressable only for bytes, but not for larger objects. Well, may be it's just me, but I wouldn't > call what happens when an ARM7 accesses a 32-bit integer without 4-byte alignment "addressing".
Obviously not all possible pointer values are valid for all types. This doesn't affect byte addressability as casting between char* and int* doesn't lose information. Accessing a misaligned pointer will result in an exception (which is a good way to detect corrupted pointers). However if you use the correct type (eg. __packed int *p rather than int *p), the compiler will generate an unaligned access. Similarly a 32-bit CPU with less than 4GB of memory doesn't have (say) a 18.25-bit address space - it still has a 32-bit address space of which only a small proportion is valid. Wilco
"Grant Edwards" <grante@visi.com> wrote in message news:13q21msqkij3dbb@corp.supernews.com...
> On 2008-01-30, Wilco Dijkstra <Wilco_dot_Dijkstra@ntlworld.com> 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.
>> so casting pointers just works. > > Unless the resulting pointer is misaligned.
Or the pointer is aligned but points to the wrong place, or invalid memory, or a peripheral, or ... If you don't know what you're doing then pointer casting is likely the least of your problems. My point was that the bitpatterns of all pointer types are the same for all addresses in the address space. Wilco
Paul Keinanen wrote:
> CBFalconer <cbfalconer@yahoo.com> wrote: >> Jim Relsh wrote: >>
... snip ...
>>> >>> Or cast to the correct type (if you are sure and know what >>> you're doing). >> >> Caution. Most pointer casts are implementation defined. The >> exception is casting to and from void*, and from void* back to >> the original type. > > In which case would an arbitrary pointer casting be a problem ? > > The only situation I can think of is for instance some DSP with > 24 bit addressable unit or some old 36 bit mainframe, in which > for instance a char* would require the memory word and also > information which quarter word is referenced. > > With any byte addressable architecture, a pointer is just a > pointer.
No. Pointers also carry (or imply) information about the object type, including alignment requirements. Misaligned access to an item can cause all sorts of unwelcome reactions. One thing you can do is cast a pointer to an unsigned char*. But not the reverse. Note that you can't dereference a void*. -- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: <http://cbfalconer.home.att.net> Try the download section. -- Posted via a free Usenet account from http://www.teranews.com
Hans-Bernhard Br&#4294967295;ker wrote:
> Paul Keinanen wrote: > >> In which case would an arbitrary pointer casting be a problem ? > > Roughly all of them. The real question is how violently the > problem will show itself. Some of them can lie dormant for a > decade before a platform change exposes them as total show-stoppers. > >> With any byte addressable architecture, a pointer is just a pointer. > > But there are quite a few non-byte addressable architectures out > there. Including ARM, last I checked.
In C, a byte is just the smallest addressable memory unit. It must hold at least 8 bits, but it can be many more. -- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: <http://cbfalconer.home.att.net> Try the download section. -- Posted via a free Usenet account from http://www.teranews.com
Wilco Dijkstra wrote:
>
... snip ...
> > My point was that the bitpatterns of all pointer types are the > same for all addresses in the address space.
Not necessarily so. void* carries all the information, apart from the object type. void* bitpattern is identical with the char* bitpattern, but after that there are no limits. -- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: <http://cbfalconer.home.att.net> Try the download section. -- Posted via a free Usenet account from http://www.teranews.com
On 2008-01-30, Wilco Dijkstra <Wilco_dot_Dijkstra@ntlworld.com> wrote:
> > "Hans-Bernhard Br&#4294967295;ker" <HBBroeker@t-online.de> wrote in message news:fnr0vs$mp6$00$1@news.t-online.com... >> Wilco Dijkstra wrote: >> >>>>> With any byte addressable architecture, a pointer is just a pointer. >> >>>> But there are quite a few non-byte addressable architectures out there. >>>> Including ARM, last I checked. >> >>> Actually most architectures are byte addressable, including ARM, >> >> Depends on what exactly you mean by the term. >> >> AFAIK ARM7 is byte-addressable only for bytes, but not for larger objects. Well, may be it's just me, but I wouldn't >> call what happens when an ARM7 accesses a 32-bit integer without 4-byte alignment "addressing". > > Obviously not all possible pointer values are valid for all types. This doesn't > affect byte addressability as casting between char* and int* doesn't lose > information. Accessing a misaligned pointer will result in an exception
On some systems. On others you just get garbage values.
> (which is a good way to detect corrupted pointers). However if > you use the correct type (eg. __packed int *p rather than int > *p), the compiler will generate an unaligned access.
I tried to figure out how to do that using gcc, but never I seemed to get the declarations or typedefs right... -- Grant Edwards grante Yow! We have DIFFERENT at amounts of HAIR -- visi.com
On 2008-01-31, Wilco Dijkstra <Wilco_dot_Dijkstra@ntlworld.com> wrote:
> > "Grant Edwards" <grante@visi.com> wrote in message news:13q21msqkij3dbb@corp.supernews.com... >> On 2008-01-30, Wilco Dijkstra <Wilco_dot_Dijkstra@ntlworld.com> 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?
> My point was that the bitpatterns of all pointer types are the > same for all addresses in the address space.
I see. I didn't recognize that point when it was spelled "casting just works". -- Grant Edwards grante Yow! Oh, I get it!! at "The BEACH goes on", huh, visi.com SONNY??
"CBFalconer" <cbfalconer@yahoo.com> wrote in message news:47A11649.3D617492@yahoo.com...
> Wilco Dijkstra wrote: >> > ... snip ... >> >> My point was that the bitpatterns of all pointer types are the >> same for all addresses in the address space. > > Not necessarily so. void* carries all the information, apart from > the object type. void* bitpattern is identical with the char* > bitpattern, but after that there are no limits.
You're talking about what the C language standards guarantee. I'm talking about how compilers implement the language. On a byte addressable architecture you can rely on the one I mentioned above. The fact is most programs rely on this whether the standard permits it or not... Wilco

The 2024 Embedded Online Conference