Thread: Assert expression

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

    Assert expression

    Hi everyone,

    I am looking a code snippet for ring buffering nRF5 SDK v15.2.0: Ring buffer library

    Code:
    void foo(void)
    {
        ret_code_t err_code;
        uint8_t data_in[] = {1,2,3,4};
        uint8_t data_out[5];
        size_t  len_in = sizeof(data_in);
        size_t  len_out = sizeof(data_out);
    
        err_code = nrf_ringbuf_cpy_put(&m_ringbuf, data_in, &len_in);
    
        err_code = nrf_ringbuf_cpy_get(&m_ringbuf, data_out, &len_out);
        ASSERT(len_out == len_in);
        ASSERT(memcmp(data_in, data_out, sizeof(data_in)) == 0);
    } 
    I do not understand the last line. I have two questions:
    1. What does it means sizeof(data_in)) == 0?
    2. When the expression will assert?

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    886
    Read the code again... the result of memcmp is being asserted, not if sizeof(data_in)==0

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,351
    Quote Originally Posted by Nikosant03
    1. What does it means sizeof(data_in)) == 0?
    That doesn't mean anything. The expression is: memcmp(data_in, data_out, sizeof(data_in)) == 0

    That is, it is checking that the memory represented by data_in and data_out contain equal values.

    Quote Originally Posted by Nikosant03
    2. When the expression will assert?
    Presumably at run time when compiled such that it is not disabled. For example, the standard library's assert will only take effect when NDEBUG is not defined.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    72
    Thank you, this is clear now!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assert() in C++
    By Richardcavell in forum C++ Programming
    Replies: 5
    Last Post: 03-10-2017, 06:38 AM
  2. Assert: When to and when not to.
    By Shamino in forum C++ Programming
    Replies: 22
    Last Post: 01-27-2012, 09:55 AM
  3. initializer expression list treated as compound expression
    By karthikeyanvisu in forum C Programming
    Replies: 7
    Last Post: 02-26-2011, 05:19 PM
  4. Replies: 2
    Last Post: 11-25-2009, 07:38 AM
  5. Assert
    By Shamino in forum C++ Programming
    Replies: 8
    Last Post: 01-24-2006, 11:02 AM

Tags for this Thread