Testing nullptr in Char Array (C++)
<p>This is bothering my a great deal, but I was trying to do an exercise for The C++ Programming Language exercises, and the question was to simply find the size and length of a char[] array, which I did, however it encounters problems when I try to verify that it's a valid pointer:</p>

<pre><code>void size_length(const char* p, int&amp; size, int&amp; length)
{
    //I don't know why, but this causes all entries to return 0
    if (!p) size = 0; length = 0; return;
    //Copy the pointer, as I want the original intact
    const char* cp = p;
    while (*cp++) ++size; ++length;
    ++size; //Null character
    size *= sizeof(char); //For portability
}

int main()
{
    char* str{ new char[15]{"A short string"} };
    int s{ 0 }, l{ 0 };
    size_length(str, s, l);
    cout &lt;&lt; "Size: " &lt;&lt; s &lt;&lt; endl &lt;&lt; "Length: " &lt;&lt; l &lt;&lt; endl;
}
</code></pre>

<p>In the second line of size_length() when I try to verify I have a legal pointer, such as if I passed a free store nullptr, it causes ALL attempts to register as invalid and returns 0, 0 (I placed several statements on one line to save space here). I've tried variations of the statement and passed many different things, but it all returns 0, 0. If I remove the line, the program works fine, however!</p>

<p>If anyone could tell me what it is I'm doing wrong and/or why I shouldn't be testing for nullptr in apparently THIS specific circumstance (I'm trying to do the <em>right</em> thing by testing validity) I would appreciate it.</p>