Re: g_string_free



Michael Meeks <michael@helixcode.com> writes:
> I was just using g_string_free for the umpteenth time, and I
> wondered; would it be possible to return the ->str member from
> the free method ( if free_segment == FALSE ) ?
> 
> Then I can remove lines and tedium from my code having:
> 
> 	GString *str = g_string_new ("foo");
> 	g_string_append (str, "baa");
> 	return g_string_free (str, FALSE);
> 
> Instead of:
> 
> 	GString *str = g_string_new ("foo");
> 	char    *p;
> 	g_string_append (str, "baa");
> 	p = str->_str;
> 	g_string_free (str, FALSE);
> 	return p;

That functionality seems to be too much of an exception from normal
_free semantics, and on the first glance, the usage appears to be an
error.  Maybe it should be a new function

  char *
  g_string_release(GString *str)
  {
    char *p = str->str;
    g_string_free(str, FALSE);
    return p;
  }

This is still a bit idiomatically impure ('release' usually means just
to stop managing the data), but more acceptable IMHO.

- Hari
-- 
Raja R Harinath ------------------------------ harinath@cs.umn.edu
"When all else fails, read the instructions."      -- Cahn's Axiom
"Our policy is, when in doubt, do the right thing."   -- Roy L Ash




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]