new client plans and libpurple
Mark Doliner
mark at kingant.net
Mon Apr 1 14:49:59 EDT 2013
On Sat, Mar 30, 2013 at 1:48 PM, Zoltán Sólyom <z-ismeretlen at freemail.hu> wrote:
> I was wondering why is that, that some functions return char* and not const char*?
"const" is a loosely enforced contract that describes who is
responsible for the memory. Generally if one of our functions returns
a const value then a caller should not modify that value. In this
case the caller does not own the return value. It does not need to
worry about freeing the memory. It should be careful about keeping a
reference to the return value, because something else in libpurple
might decide to free that value. A lack of const generally means the
caller owns the memory and can do whatever it wants with it. And the
caller is expected to free the memory.
Sometimes we choose to return "const char*" because it's feasible for
us to use a fixed size, static buffer, which is more efficient than
allocating memory. Sometimes it's infeasible to use a fixed buffer,
so we allocate memory.
> Same about some that are expecting const arguments while others not.
If a function does not modify the argument then it should be const.
If the function modifies or takes ownership of the memory then the
argument should not be const.
More information about the Devel
mailing list