Velocity Reviews > Memory Alignment

# Memory Alignment

Dharma
Guest
Posts: n/a

 11-13-2005
hi,

struct a
{
byte d;
byte buf[16];
}text;

text.buf[0] = 'A'
text.buf[1] = '\0'
text.buf[2] = 'B'
text.buf[3] = '\0'
text.buf[4] = '\0'
text.buf[5] = '\0'

main()
{
unsigned short *c = NULL;
c = (unsigned short*)&text.buf;
if(0x0000 == *c)
{
return ;
}
}

i have a structure as above. Iam type casting the byte to unsigned
short pointer.
when i do (0x0000 == *c) the first byte becomes 0x00. I found that buf
starts with odd memory address, If i make the memory Alignment to even
there is no problem,
What the low level does if memory Alignment is odd aligned and it is
unsigned short* type.

thanks,
Dharma.

Keith Thompson
Guest
Posts: n/a

 11-13-2005
"Dharma" <(E-Mail Removed)> writes:
> struct a
> {
> byte d;
> byte buf[16];
> }text;

C has no standard type called "byte".

> text.buf[0] = 'A'
> text.buf[1] = '\0'
> text.buf[2] = 'B'
> text.buf[3] = '\0'
> text.buf[4] = '\0'
> text.buf[5] = '\0'
>
> main()

This should be "int main(void)".

> {
> unsigned short *c = NULL;
> c = (unsigned short*)&text.buf;
> if(0x0000 == *c)
> {
> return ;
> }
> }
>
> i have a structure as above. Iam type casting the byte to unsigned
> short pointer.

No, you're converting an address of (i.e., a pointer to) a byte to a
pointer to an unsigned short.

> when i do (0x0000 == *c) the first byte becomes 0x00. I found that buf
> starts with odd memory address, If i make the memory Alignment to even
> there is no problem,
> What the low level does if memory Alignment is odd aligned and it is
> unsigned short* type.

Alignment requirements vary from one system to another. If type short
requires even alignment, and you have a misaligned pointer, attempting
to dereference it (as in *c) invokes undefined behavior. As far as
the language is concerned, anything could happen.

The solution: don't do that.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Old Wolf
Guest
Posts: n/a

 11-13-2005
Keith Thompson wrote:

> "Dharma" <(E-Mail Removed)> writes:
>> struct a
>> {
>> byte d;
>> byte buf[16];
>> }text;
>> main()
>> {
>> unsigned short *c = NULL;
>> c = (unsigned short*)&text.buf;
>> if(0x0000 == *c)

>
> Alignment requirements vary from one system to another. If type short
> requires even alignment, and you have a misaligned pointer, attempting
> to dereference it (as in *c) invokes undefined behavior. As far as
> the language is concerned, anything could happen.

I think the cast causes undefined behaviour too, if there is an
alignment problem.

Keith Thompson
Guest
Posts: n/a

 11-14-2005
"Old Wolf" <(E-Mail Removed)> writes:
> Keith Thompson wrote:
>> "Dharma" <(E-Mail Removed)> writes:
>>> struct a
>>> {
>>> byte d;
>>> byte buf[16];
>>> }text;
>>> main()
>>> {
>>> unsigned short *c = NULL;
>>> c = (unsigned short*)&text.buf;
>>> if(0x0000 == *c)

>>
>> Alignment requirements vary from one system to another. If type short
>> requires even alignment, and you have a misaligned pointer, attempting
>> to dereference it (as in *c) invokes undefined behavior. As far as
>> the language is concerned, anything could happen.

>
> I think the cast causes undefined behaviour too, if there is an
> alignment problem.

You're right. C99 6.3.2.3p7:

A pointer to an object or incomplete type may be converted to a
pointer to a different object or incomplete type. If the resulting
pointer is not correctly aligned for the pointed-to type, the
behavior is undefined.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

EventHelix.com
Guest
Posts: n/a

 11-14-2005
> i have a structure as above. Iam type casting the byte to unsigned
> short pointer.
> when i do (0x0000 == *c) the first byte becomes 0x00. I found that buf
> starts with odd memory address, If i make the memory Alignment to even
> there is no problem,
> What the low level does if memory Alignment is odd aligned and it is
> unsigned short* type.

The following article should help:

http://www.eventhelix.com/RealtimeMa...ndOrdering.htm

--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
Sequence Diagram Based Real-time and Embedded System Design Tool