|
|
On Mon, Jul 14, 2008 at 07:12:03PM +0400, Alexey Dobriyan wrote:
> On Mon, Jul 14, 2008 at 09:51:17AM -0500, Marco Peereboom wrote:
> > On Mon, Jul 14, 2008 at 06:38:49PM +0400, Alexey Dobriyan wrote:
> > > On Mon, Jul 14, 2008 at 03:06:14PM +0200, Marc Espie wrote:
> > > > On Mon, Jul 14, 2008 at 04:49:34PM +0400, Alexey Dobriyan wrote:
> > > > > memdup(3) has strdup(3) semantics but without strings.
> > > > >
> > > > > Canonical code for duplicating buffer looks like:
> > > > >
> > > > > rpl = malloc(src, len);
> > > > > if (!rpl)
> > > > > ...
> > > > > memcpy(rpl, src, len);
> > > > >
> > > > > Mistakes happen and two lengths in snippet above will be different.
> > > > > To prevent this memdup(3) was created:
> > > > >
> > > > > rpl = memdup(src, len);
> > > > > if (!rpl)
> > > > > ...
> > > > > ...
> > > >
> > > > This kind of code is very easy to write. The big question is: do we
> > > > want it.
> > > >
> > > > That's non-standard stuff, it's not ANSI, it's not POSIX, it's not
> > > > single
> > > > unix.
> > > >
> > > > So what's the point ?
> > >
> > > The point is writing "len" once, so it won't diverge from itself.
> >
> > But you trade an explicit free for and implicit one.
>
> malloc, you mean.
No I mean free. malloc always needs to be paired with free; in this
case you need to pair strdup with free. Not very nice when debugging
memory leaks.
example:
grep malloc * | grep mypointer
grep free * | grep mypointer
memdup FAIL
>
> > I find this a confusing interface that adds nothing.
>
> Do you find strdup() confusing?
Extremely. This is one of those APIs I always have to go back to the
man to use. It is non-obvious.
>
> > It is also not part of a standard.
>
> Eventually it will be.
Maybe but until then we might be able to keep a sane interface instead.
I'll be the first to admit that currently there is way too much crap in
these standards. You can't leave committee people alone; unmitigated
committee work always ends up in disaster.
|
|