Any reason why PQclear() do not assign the pointer to NULL?

  • Jump to comment-1
    Igor Korot<ikorot01@gmail.com>
    Apr 1, 2026, 6:49 AM UTC
    Hi,
    It only frees memory.
    So the following code snippet will crash:
    [code]
    PQclear( res );
    if( serverVersion > curremtrunningvrsion )
    {
    res = PQprepare();
    // error checking/
    }
    PQclear( res );
    [/code]
    Thank you.
    • Jump to comment-1
      Dominique Devienne<ddevienne@gmail.com>
      Apr 1, 2026, 7:54 AM UTC
      Well yes, the fact you're asking is surprising.
      void PQclear(PGresult* res);
      How could it? C/C++ is pass by value. So PQclear gets a copy of the pointer.
      Even if it assigns it internal to nullptr, the calling code has its
      own unchanged copy.
      It's the same reason free() doesn't either.
      It's your responsability to assign nullptr after the PQclear.
      In/out argument in general violate the Principle of Least Surprise,
      and are best avoided.
      You have no way to know reading the code the call has a side effect on
      the var (as opposed to the value). FWIW. --DD