blog.world3.net

Compile time asserts in C

Assert is a useful function that checks a condition and outputs an error if it is not met at run time. Aside from generating some overhead in the code it is also pretty useless on microcontrollers because the messages go to STDERR and, well, there isn’t one. A much better solution is to do the check at compile time as then there is no overhead and you can see the error messages.

 

Unfortunately the C pre-processor is just a glorified text processor and knows almost nothing about C, so you can’t use things like sizeof(). I found this solution:

 

// compile time static assertions (http://www.pixelbeat.org/programming/gcc/static_assert.html)
#define ASSERT_CONCAT_(a, b) a##b
#define ASSERT_CONCAT(a, b) ASSERT_CONCAT_(a, b)
#define ct_assert(e) enum { ASSERT_CONCAT(assert_line_, __LINE__) = 1/(!!(e)) }

 

If you then write this:

 

ct_assert(sizeof(EEP_CONFIG_t) == 32);

If the condition is not true it will cause a divide by zero error on that line. Not ideal but it seems to work. In this case an EEPROM page happens to be 32 bytes and the struct is padded. Thanks to Pixelbeat.

This entry was written by mojo, posted on 21/12/2011 at 15:48, filed under avr. Bookmark the permalink. Follow any comments here with the RSS feed for this post. Post a comment or leave a trackback: Trackback URL.
« Scheduling uTorrent on Windows
RFM12B bit stream sync issue »

Post a Comment

Your email is never shared. Required fields are marked *

*
*

たとえ溺れても梦はゆめでしかない
  •  

    December 2011
    M T W T F S S
    « Nov   Jan »
     1234
    567891011
    12131415161718
    19202122232425
    262728293031  
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
  • Categories

    • audio (1)
    • avr (21)
    • BBC (1)
    • electronics (29)
    • genius (4)
    • hardware (22)
    • idiots (39)
    • Internet (21)
    • law (20)
    • microcontrollers (12)
    • networking (17)
    • politics (29)
    • privacy (19)
    • Retro Adapter (5)
    • security (17)
    • software (32)
    • Uncategorized (18)
    • windows (25)
  • Archives

    • February 2012
    • January 2012
    • December 2011
    • November 2011
    • October 2011
    • September 2011
    • July 2011
    • June 2011
    • May 2011
    • March 2011
    • January 2011
    • December 2010
    • November 2010
    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • November 2005
  • Links:

    Main site: world3.net

    Electronics: denki.world3.net

WordPress | Sandbox