B
barcaroller
I have a block of memory that looks as follows:
uint8 value1 16
uint16 value2 0301
uint16 value3 008c
However, when I use a C struct to map this block of memory, like this:
typedef struct
{
uint8 value1;
uint16 value2;
uint16 value3;
} mytype;
mytype p* = (mytpye*) block_of_memory
I get the following results:
sizeof(mytype) is being reported as 6, and not 5
value1 shows as 0x16 (correctly)
value2 shows as 0x01 (not 0x0301)
value3 shows as 0x018c (not 0x008c;
the 01 is the out-of bounds sixth byte)
Obviously, the compiler has its own idea of how to map this struct but I've
seen this kind of mapping all over the Linux header files (e.g. TCP and IP
header files). I've used similar mappings in my own code in the past, and I
*think* it always worked. What am I missing? And how should I go about
fixing this?
Side note
=========
If I change the struct to break value2 into two halves, like this:
typedef struct
{
uint8 value1;
uint8 value2_1;
uint8 value2_2;
uint16 value3;
} mytype;
I get the following result:
sizeof(mytype) still being reported as 6, and not 5
value1 shows as 0x16 (correctly)
value2_1 shows as 0x03 (correctly)
value2_2 shows as 0x01 (correctly)
value3 shows as 0x018c (not 0x008c,
the 01 is the out-of bounds sixth byte)
uint8 value1 16
uint16 value2 0301
uint16 value3 008c
However, when I use a C struct to map this block of memory, like this:
typedef struct
{
uint8 value1;
uint16 value2;
uint16 value3;
} mytype;
mytype p* = (mytpye*) block_of_memory
I get the following results:
sizeof(mytype) is being reported as 6, and not 5
value1 shows as 0x16 (correctly)
value2 shows as 0x01 (not 0x0301)
value3 shows as 0x018c (not 0x008c;
the 01 is the out-of bounds sixth byte)
Obviously, the compiler has its own idea of how to map this struct but I've
seen this kind of mapping all over the Linux header files (e.g. TCP and IP
header files). I've used similar mappings in my own code in the past, and I
*think* it always worked. What am I missing? And how should I go about
fixing this?
Side note
=========
If I change the struct to break value2 into two halves, like this:
typedef struct
{
uint8 value1;
uint8 value2_1;
uint8 value2_2;
uint16 value3;
} mytype;
I get the following result:
sizeof(mytype) still being reported as 6, and not 5
value1 shows as 0x16 (correctly)
value2_1 shows as 0x03 (correctly)
value2_2 shows as 0x01 (correctly)
value3 shows as 0x018c (not 0x008c,
the 01 is the out-of bounds sixth byte)