Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #10621

Re: evaluate a generic swap function

From Eric Sosman <esosman@ieee-dot-org.invalid>
Newsgroups comp.lang.c
Subject Re: evaluate a generic swap function
Date 2011-09-20 21:15 -0400
Organization A noiseless patient Spider
Message-ID <j5bdtu$i41$1@dont-email.me> (permalink)
References <fae63bac-e9c4-48e2-9c4f-e0269b2b3f2c@w8g2000yqi.googlegroups.com>

Show all headers | View raw


On 9/20/2011 9:51 AM, Stanley Rice wrote:
> Hello all
>
> I wrote a "generic" swap function, which can swap some type, such as
> int, double, long. But I don't know how to evaluate this function, and
> don't know whether it's a good idea to use such a function. I hope you
> can give me some suggestions.
>
>   void swap(void *left, void *right, size_t siz)
> {
>      char *buf;
>
>      buf = (char *)malloc(sizeof(*buf) * siz);


     The cast is harmless but unnecessary.  And since you're already
assuming that `siz' is a byte count, the multiplication by unity is
also unnecessary, although harmless.  Just saying.

>      memcpy(buf, left, siz);

     If malloc() fails to deliver the requested amount of memory and
returns NULL, you'll be in trouble.  You'd be in even worse trouble
using a C99 variable-length array instead of calling malloc(), because
malloc() can at least tell you when it fails, while a VLA cannot.  Also,
malloc(0) is well-defined, while a VLA of zero elements is a constraint
violation.  ("Swapping" two zero-length objects is something of a corner
case, it's true, but functions should behave nicely in the corners if
possible: consider the memxxx() functions with zero size.)

>      memcpy(left, right, siz);
>      memcpy(right, buf, siz);
>      free(buf);
> }
>
> call this function in the following ways:
>      int a = 5;
>      int b = 4;
>      swap(&a,&b, sizeof(a));

     Although your function will work (if malloc() succeeds), I don't
think it's very attractive.  An operation is worth encapsulating in
a utility function if it's difficult or tricky in some way, or if the
function can somehow perform the operation more cheaply or conveniently
than free-hand code.  I doubt that this particular function scores on
either point: It encapsulates a simple operation, and probably uses
more time and memory than a simple implementation would.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

evaluate a generic swap function Stanley Rice <heconghui@gmail.com> - 2011-09-20 06:51 -0700
  Re: evaluate a generic swap function jacob navia <jacob@spamsink.net> - 2011-09-20 16:01 +0200
    Re: evaluate a generic swap function jt@toerring.de (Jens Thoms Toerring) - 2011-09-20 14:29 +0000
      Re: evaluate a generic swap function jacob navia <jacob@spamsink.net> - 2011-09-20 21:13 +0200
        Re: evaluate a generic swap function jt@toerring.de (Jens Thoms Toerring) - 2011-09-20 20:30 +0000
          Re: evaluate a generic swap function Keith Thompson <kst-u@mib.org> - 2011-09-20 17:05 -0700
            Re: evaluate a generic swap function gazelle@shell.xmission.com (Kenny McCormack) - 2011-09-21 15:45 +0000
              Re: evaluate a generic swap function Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-09-21 09:33 -0700
              Re: evaluate a generic swap function jacob navia <jacob@spamsink.net> - 2011-09-21 19:17 +0200
                Re: evaluate a generic swap function gazelle@shell.xmission.com (Kenny McCormack) - 2011-09-21 17:34 +0000
                Re: evaluate a generic swap function Jack Foot <foot@dev.null.invalid> - 2011-09-21 18:12 +0000
  Re: evaluate a generic swap function Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-09-20 07:33 -0700
    Re: evaluate a generic swap function ImpalerCore <jadill33@gmail.com> - 2011-09-20 08:26 -0700
      Re: evaluate a generic swap function pete <pfiland@mindspring.com> - 2011-09-20 23:10 -0400
  Re: evaluate a generic swap function "BartC" <bc@freeuk.com> - 2011-09-20 22:20 +0100
    Re: evaluate a generic swap function jacob navia <jacob@spamsink.net> - 2011-09-20 23:23 +0200
      Re: evaluate a generic swap function Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-20 23:07 +0100
  Re: evaluate a generic swap function Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-20 21:15 -0400
    Re: evaluate a generic swap function Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-09-21 03:17 -0700
  Re: evaluate a generic swap function Gene <gene.ressler@gmail.com> - 2011-09-21 02:40 -0700
  Re: evaluate a generic swap function Seebs <usenet-nospam@seebs.net> - 2011-09-23 01:46 +0000
    Re: evaluate a generic swap function Nelu <tandauioan@gmail.com> - 2011-09-23 10:26 -0700
      Re: evaluate a generic swap function Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-23 18:36 +0100
        Re: evaluate a generic swap function Nelu <tandauioan@gmail.com> - 2011-09-23 10:49 -0700
          Re: evaluate a generic swap function Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-23 20:18 +0100
            Re: evaluate a generic swap function Nelu <tandauioan@gmail.com> - 2011-09-23 12:54 -0700
              Re: evaluate a generic swap function James Kuyper <jameskuyper@verizon.net> - 2011-09-23 16:43 -0400
                Re: evaluate a generic swap function Nelu <tandauioan@gmail.com> - 2011-09-23 13:49 -0700
                Re: evaluate a generic swap function James Kuyper <jameskuyper@verizon.net> - 2011-09-23 17:44 -0400
                Re: evaluate a generic swap function Nelu <tandauioan@gmail.com> - 2011-09-23 18:04 -0700
                Re: evaluate a generic swap function Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 13:52 -0800
              Re: evaluate a generic swap function Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-23 21:46 +0100
                Re: evaluate a generic swap function Nelu <tandauioan@gmail.com> - 2011-09-23 13:59 -0700
                Re: evaluate a generic swap function Seebs <usenet-nospam@seebs.net> - 2011-09-26 22:59 +0000
          Re: evaluate a generic swap function pete <pfiland@mindspring.com> - 2011-09-23 18:19 -0400
            Re: evaluate a generic swap function Nelu <tandauioan@gmail.com> - 2011-09-23 16:36 -0700

csiph-web