const gchar* vs gchar *
Derek Atkins
derek at ihtfp.com
Sat Dec 22 07:19:55 EST 2012
Hi,
No, they are not equivalent.
The 'const' basically tells the compiler that the object is immutable.
It's used in an argument to promise that the function will not modify the
object. It's used in a return value to say that the caller may not modify
or free the object because the callee will free it later.
So no, your second function will have a memory leak, because g_strdup is
expecting the caller to free the object.
-derek
On Sat, December 22, 2012 6:51 am, Geert Janssens wrote:
> And now a question to show that I never had a formal c/c++ education.
>
> Are the below functions equivalent ?
>
> void funcA ()
> {
> gchar *varA = g_strdup ("Test");
> <do something with a>
> g_free (varA);
> }
>
> and
>
>
> void funcA ()
> {
> const gchar *varA = g_strdup ("Test");
> <do something with a>
> }
>
> I'm mostly wondering if the second function would have a memory leak or
> not. If varA is defined as a const gchar *, will the program
> automatically free the memory allocated with g_strdup ?
>
> I don't expect so, but I'm seeing mixed uses in GnuCash and want to
> determine for once and for all what is the proper way to handle this.
>
> Geert
> _______________________________________________
> gnucash-devel mailing list
> gnucash-devel at gnucash.org
> https://lists.gnucash.org/mailman/listinfo/gnucash-devel
>
--
Derek Atkins 617-623-3745
derek at ihtfp.com www.ihtfp.com
Computer and Internet Security Consultant
More information about the gnucash-devel
mailing list