[cairo] return type of cairo_status_to_string ()

Bill Spitzak spitzak at gmail.com
Tue Aug 17 17:36:34 PDT 2010

M Joonas Pihlaja wrote:
> On Tue, 17 Aug 2010, Daniel Goldman wrote:
>> What bad thing does the const keyword prevent here? If nothing bad prevented, 
>> then I think it should be the simpler, more default "char *".
> In C, returning a const char * from a function tells the caller that 
> they do not need to call free() 

I have never heard that one, in my experience you NEVER call free() on 
something you did not allocate yourself. In fact most functions 
returning char* are pointing into larger buffers indicating a location 
to modify and you certainly should not call free() on that!

The reason is that it is desirable to store string constants in 
read-only memory. This is a great security feature as they cannot be 
altered to unexpected values, identical strings can map to the same 
location, and I think it can improve the speed as writable pages have 
more overhead in VM than read-only ones.

Making the return type const char* instead of char* means that mistakes 
using read-only strings will be detected at compile time rather than run 

Currently G++ produces a nasty warning if you try to return a string 
constant from a function declared as char*, or try to initialize a char* 
table with string constants.

More information about the cairo mailing list