|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
Data layout in EEPROM |
Posted: Mon Nov 14, 2005 10:02 am |
|
|
Hi, everybody,
I want to raise a very basic question here.
Say, I've defined a structure like:
struct
{
int1 var_a;
int var_b;
int1 var_c;
} struct_test;
and store 'struct_test' in 24c64 EEPROM using page write giving a length of 3 bytes( I think the smallest read/write unit in 24c64 is bytes???): I2C_Page_Write( struct_test, 0x00, 0x00, 3);
So var_a and var_c will be expanded to occupy a whole byte in the EEPROM?
Can anybody tell me if I am right?
Many thx |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
|
Ttelmah Guest
|
|
Posted: Mon Nov 14, 2005 10:10 am |
|
|
Hint.
Try 'sizeof'.
As written, the compiler will assign a byte, then use one bit of it for 'var_a', then assign a byte for 'var_b', then assign another byte, and a bit of this for 'var_c'.
If you instead code as:
Code: |
struct
{
int1 var_a;
int1 var_c;
int var_b;
} struct_test;
|
The structure will only occupy two bytes, with the two bit fields both being packed into one byte.
Sizeof, will change from '3' with the original declaration, to '2' with the second declaration.
Best Wishes |
|
|
Guest
|
|
Posted: Mon Nov 14, 2005 10:28 am |
|
|
Thanks guys!
Then a 'further' question is, if I want to save it with the second declaration which occupies 2 bytes, then after reading back from the EEPROM again, the compiler will assign the first bit of the first byte to var_a, the second bit of the first byte to var_c and the second byte to var_b. Am I right? |
|
|
Guest
|
|
Posted: Mon Nov 14, 2005 10:33 am |
|
|
I should have quoted this question with the last post....
The 2nd question is, even though I use the second declaration:
Quote: | struct
{
int1 var_a;
int1 var_c;
int var_b;
} struct_test; |
If I still declare the length as 3 bytes in the i2c write functio, the complier will assign 3 bytes to the structure instead of 2?
Quote: | I2C_Page_Write( struct_test, 0x00, 0x00, 3); |
Regards |
|
|
Ttelmah Guest
|
|
Posted: Mon Nov 14, 2005 10:44 am |
|
|
The I2C_Write, has no effect at all on the declared size. If you transfer 3 bytes, when an object is only two bytes in size, you will just waste a location in the EEPROM. However when you read it _back_, then you risk overwriting other data that should not be touched.
This is why it is worth _not_ hardcoding the sizes transferred, but instead use the 'sizeof' function. If you tell the compiler to write data from the location holding the structure, and transfer the number of bytes given by 'sizeof', you will transfer the entire structure, no matter how it is laid out in memory, and similarly when retrieving it, will get back the entire structure, and not overwrite anything you shouldn't. :-)
Best Wishes |
|
|
Guest
|
|
Posted: Mon Nov 14, 2005 11:10 am |
|
|
Got it!!! |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|