Thread: sturcture to memory eeprom

  1. #1
    Registered User RocketMan's Avatar
    Join Date
    Dec 2007
    Posts
    28

    sturcture to memory eeprom

    Hi all,

    This question is slightly off topic but is still flat c but used on microcontrollers.

    Im just trying to write a structure to eeprom and im unsure of the c syntax. I can write data to a single address fine, but i now want to write a structure but am getting lost. Please could you offer me some advice. In the instance below the varibles already have the data added and now i need to write and read from eeprom. Below is my example:

    Thanks for your input in advance.

    Regards,

    Rocketman

    Code:
    /******************************
    Function: EEPROM structure
    *******************************/
    
    struct 
         {
          char one;
          char two;
          char three;
         }Memory_Block
    
    /******************************
    Function: EEPROM write
    *******************************/
    
    Write_EEPROM(&Memory_Block, sizeof(Memory_Block)); // (address, data)
    
    /******************************
    Function: EEPROM read
    *******************************/
    
    void EEPROM_Startup_Routine(void)
    {
    	Read_EEPROM(&Memory_Block); // (address)
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Writing a struct is no different from any other data object, tho' your struct Memory_Block w/o the terminating semi-colon maybe the reason for errors, if any.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by itCbitC View Post
    Writing a struct is no different from any other data object, tho' your struct Memory_Block w/o the terminating semi-colon maybe the reason for errors, if any.
    Well with structures, you need to take alignment and padding into account. It is possible to write out a structure, recompile your application with different compile flags (or a different compiler version), and then read in different data than you originally wrote out. That's why it is best to serialize/deserialize each member of the structure individually.
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Alignment is intrinsic to every object comes into play when reading an object from storage and different compilers align differently.
    However for this post I fail to see why it is of concern when writing to memory as the hardware would align it automatically.

    Edit: note we are dealing with chars in this struct - so none of the alignment restrictions apply.
    Last edited by itCbitC; 11-16-2009 at 02:16 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Fragmentation with Dynamic FIFO Queue
    By fguy817817 in forum Linux Programming
    Replies: 17
    Last Post: 10-31-2009, 04:17 AM
  2. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  3. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  4. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  5. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM