Thread: What happens with returned pointer

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    6

    What happens with returned pointer

    Hi everybody.

    Question is about pointers returned from functions.

    There is function
    Code:
    char *PQescapeLiteral(PGconn *conn, const char *str, size_t length);
    that returns escaped version of passed str parameter.


    What happens with this pointer and memory allocated in case I use it in some way like this:
    Code:
    printf("SELECT * FROM lala WHERE lolo = '%s'", 
    PQescapeLiteral(DBConnection, "'''ddsa", 3))
    I mean if this pointer and allocated memory for it hang till the end of program, or it's automatically freed after printf?


    Thanks in advance.





  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You have a memory leak:
    PQescapeLiteral returns an escaped version of the str parameter in memory allocated with malloc(). This memory should be freed using PQfreemem() when the result is no longer needed.
    (PostgreSQL 11 - 34.3. Command Execution Functions)

    In the future, read the manual before asking questions.
    Last edited by laserlight; 05-01-2019 at 11:32 PM.
    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
    Apr 2019
    Posts
    6
    Laserlight,
    Thanks for your response.
    Now it's clear.

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    62
    Quote Originally Posted by datswd View Post
    I mean if this pointer and allocated memory for it hang till the end of program, or it's automatically freed after printf?
    C has no way of automatically freeing things. It's not like in C++ where you have smart pointers that free themselves when they go out of scope or a garbage collected language like C# or Java where things get automatically freed periodically if nothing references them. C has none of that at all. You must always, always read the manual or source code for functions you're working with that return pointers so you know what to do with the pointer when you're finished. One of two things generally happen when something returns a char*, it either allocated something that you're expected to malloc or it returned a pointer to a static array or some other value that you're not expected to free. There's no way to tell just from the function prototype which one it is, you must always, always read the manual or source code carefully to know what to do with it.

    Things are freed, however, at the end of the program as you implied. The OS knows what memory you allocated and will clean it up. For short programs that don't allocate large amounts of memory it's common to see no free calls at all. It's probably not the best practice, but there's no sense in wasting your time freeing something if you're just ending the program anyway. So if you're looking at code that uses this function and they don't free it, they're either making a mistake, or leaving it to the OS.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What happens behind the scene when value is returned ?
    By afesheir in forum C Programming
    Replies: 6
    Last Post: 12-01-2011, 11:41 AM
  2. *** process returned -1 ***
    By bilbo67216 in forum C Programming
    Replies: 16
    Last Post: 05-28-2010, 12:35 AM
  3. What exactly is iterator returned by .end()?
    By pheres in forum C++ Programming
    Replies: 21
    Last Post: 12-09-2008, 09:50 AM
  4. Wrong returned pointer
    By intmail in forum C Programming
    Replies: 3
    Last Post: 03-31-2007, 05:24 AM
  5. I've Returned
    By Krak in forum Game Programming
    Replies: 11
    Last Post: 05-06-2003, 11:53 AM

Tags for this Thread