By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,948 Members | 1,595 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,948 IT Pros & Developers. It's quick & easy.

Union reversing half words

P: n/a
Hello,
I'm trying to create a memory dump parser tool for which I plan to use
existing header files that contain structures of registers. My plan
is to use a union and include the structures in the header files plus
a 'dump' variable where I would store the memory dump. A simplified
version of this would look something like this:

union
{
struct
{
unsigned short first1; //first half of first word
unsigned short second1; //sencond half of first word
unsigned long whole; //second word
}words;

unsigned long dump[2]; //two words
}words_union;

main()
{
words_union.dump[0] = 0x1BADC0DE;
words_union.dump[1] = 0x1BADC0DE;
return 0;
}
Where the struct 'words' represents a register memory map, and dump[]
my dump place holder.

After running main() what I'm seeing in memory is that first1 =
0xC0DE, second1 = 0x1BAD, and whole = 0x1BADCODE.
My expectaion (and wish) is that first1 = 0x1BAD, and second1 =
0xCODE. But apparently these two half words are reversed in memory.

My feeling is that this has to do with endianess, but I'm not sure,
could anyone give a clue on how to store the half words in memory
correctly?

Thanks in advance.
Nov 14 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On 26 Jul 2004 10:24:13 -0700, ja*******@hotmail.com (Jay Azurin)
wrote:
Hello,
I'm trying to create a memory dump parser tool for which I plan to use
existing header files that contain structures of registers. My plan
is to use a union and include the structures in the header files plus
a 'dump' variable where I would store the memory dump. A simplified
version of this would look something like this:

union
{
struct
{
unsigned short first1; //first half of first word
unsigned short second1; //sencond half of first word
unsigned long whole; //second word
}words;

unsigned long dump[2]; //two words
}words_union;

main()
{
words_union.dump[0] = 0x1BADC0DE;
words_union.dump[1] = 0x1BADC0DE;
return 0;
}
Where the struct 'words' represents a register memory map, and dump[]
my dump place holder.

After running main() what I'm seeing in memory is that first1 =
0xC0DE, second1 = 0x1BAD, and whole = 0x1BADCODE.
My expectaion (and wish) is that first1 = 0x1BAD, and second1 =
0xCODE. But apparently these two half words are reversed in memory.

My feeling is that this has to do with endianess, but I'm not sure,
could anyone give a clue on how to store the half words in memory
correctly?


Yes, you are suffering for little endian-ness. The easiest way to see
what memory actually looks like (or to set it to a particular sequence
of bytes) is to use an array of unsigned char. There might be a way
to do it with shifts and bitwise ands but I think this will also
suffer from endian-ness and be non-portable.
<<Remove the del for email>>
Nov 14 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.