Thread: Bit fields and flash memory

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    97

    Bit fields and flash memory

    Hi everyone,

    I was reading about bit fields and I tried to apply them to my firmware for practice. So I created a two member struct and define the members to be one bit in lenght. Finally, I created an instance of the struct and initialized it

    Code:
    struct FM_states {
      uint8_t prevState_FMC:1;
      uint8_t prevState_FMH:1;
    };
    
    struct FM_states FM_state {
    .prevState_FMC = 0,.prevState_FMH = 0};
    After compiling a noticed that the flash memory was higher than before using the bit fields

    This is the flash occupation with bit fields:

    Bit fields and flash memory-screenshot_1-png

    This is flash occupation with out bit fields (24 bytes less):

    Bit fields and flash memory-screenshot_3-png

    So, in general bit fields do not reduce memory size?
    Last edited by Salem; 01-30-2021 at 04:34 AM. Reason: Removed crayola

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Bitfields MAY reduce your RAM footprint.
    But that usually comes at the cost of increased code space.

    Writing to a whole byte is usually a quick and simple single instruction.

    But writing to a bit field involves
    - a memory read
    - use of & and | bitwise operators to modify only selected bits
    - use of << and >> operators to get those bits into the right position
    - a memory write.

    Put it this way, without bitfields, you would start with
    Code:
    #define prevState_FMC  (1<<0)
    #define prevState_FMH  (1<<1)
    uint8_t FM_state;
    Now write the code to set / clear / test each bit.

    This is exactly what the compiler has to do, but just hidden from you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    505
    Bit fields are intended to reduce memory size. That's one of their main purposes. The other reason is to get a specific bit layout in memory for talking to hardware.

    But C compilers traditionally do an awful job on bitfields. It's a bit of a chicken and egg situation. Since compilers generate bad code for bitfields, experienced C programmers tend to avoid bitfields, so there is less motivation for compiler writers to do a good job with bitfields.

    In your case, an optimisation intended to decrease memory take has actually increased memory take. Admittedly only by 24 bytes. But all the same.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2014, 07:17 AM
  2. how to block flash memory
    By lichking in forum Windows Programming
    Replies: 8
    Last Post: 03-04-2013, 05:33 AM
  3. how to block flash memory
    By lichking in forum C Programming
    Replies: 5
    Last Post: 02-27-2013, 01:25 AM
  4. Problem with usb Flash memory
    By khpuce in forum Tech Board
    Replies: 6
    Last Post: 02-26-2004, 08:54 AM
  5. working with flash memory
    By wazilian in forum C Programming
    Replies: 3
    Last Post: 10-09-2001, 12:15 PM

Tags for this Thread