Thread: sizeof assertion problem

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    45

    sizeof assertion problem

    Hello,

    In my program I would like to assert that the size of my array is what I told it to be as a precondition for a function.

    The function is:
    Code:
    void getRow(char *token, char *input)
    {
       assert(sizeof(*input)==MAX+TERMINATOR);
    }
    - Note TERMINATOR is a constant = 1 for the null terminator.

    So this assertion fails. My guess is that it's because the input points to only 1 char. I believe my guess is correct because
    Code:
    assert(sizeof(*input)==sizeof(char));
    passes.

    In the calling function input is not a pointer but a char array.
    In the calling function this assertion works:
    Code:
    char input[MAX+TERMINATOR];
    assert(sizeof(input)==MAX+TERMINATOR);
    Is there a way to have an assertion like this in the getRow function or is it just not possible?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Not possible since input is a pointer, not an array.
    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

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    45
    Ok thank you.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    If TERMINATOR is simply the number 1 then it's simply obscuring the meaning. Just say + 1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with sizeof
    By bob887 in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2011, 03:10 PM
  2. Debug assertion problem
    By swatihurde in forum C Programming
    Replies: 1
    Last Post: 08-13-2010, 01:43 PM
  3. sizeof(): why the problem?
    By pantera in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2009, 01:36 AM
  4. Debug Assertion failure problem
    By uldaman in forum C++ Programming
    Replies: 8
    Last Post: 01-21-2008, 02:22 AM
  5. Assertion Problem
    By greggorob64 in forum C Programming
    Replies: 5
    Last Post: 12-06-2007, 09:35 AM

Tags for this Thread