CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Data layout in EEPROM

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








Data layout in EEPROM
PostPosted: Mon Nov 14, 2005 10:02 am     Reply with quote

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 Very Happy
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Mon Nov 14, 2005 10:04 am     Reply with quote

Yes you are right.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
Ttelmah
Guest







PostPosted: Mon Nov 14, 2005 10:10 am     Reply with quote

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








PostPosted: Mon Nov 14, 2005 10:28 am     Reply with quote

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








PostPosted: Mon Nov 14, 2005 10:33 am     Reply with quote

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







PostPosted: Mon Nov 14, 2005 10:44 am     Reply with quote

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








PostPosted: Mon Nov 14, 2005 11:10 am     Reply with quote

Got it!!!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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