Memory Alignment

D

Dharma

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.
 
K

Keith Thompson

Dharma said:
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.
 
O

Old Wolf

Keith said:
Dharma said:
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.
 
K

Keith Thompson

Old Wolf said:
Keith said:
Dharma said:
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.
 
E

EventHelix.com

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/RealtimeMantra/ByteAlignmentAndOrdering.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top