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


Groups > comp.lang.c > #24286

Re: Trying to figure out how to dynamically re-allocate strings while concatenating

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Trying to figure out how to dynamically re-allocate strings while concatenating
Date 2012-08-05 13:17 -0700
Organization None to speak of
Message-ID <lnvcgxxfei.fsf@nuthaus.mib.org> (permalink)
References <eb47af15-f8ff-4737-983d-3f54307c1081@googlegroups.com> <501D50E6.532@mindspring.com> <lnhasiyzqu.fsf@nuthaus.mib.org> <501E1F32.2060700@loria.fr> <0.59d46f792d5add280c7a.20120805124912BST.87boip36fb.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> Jens Gustedt <jens.gustedt@loria.fr> writes:
>> Am 05.08.2012 02:00, schrieb Keith Thompson:
> <snip>
>>> On the other hand, whether it matters depends on what you intend to do
>>> once you've detected an allocation failure.
> <snip>
>>> A very common response to a memory allocation failure is to
>>> terminate the program, ideally after doing some cleanup (such as
>>> saving temporary files, restoring the display to a sane state,
>>> printing and/or logging an error message, etc.)  But if the
>>> program is about to terminate anyway, then losing the pointer to
>>> your original buffer isn't going to make much difference, and you
>>> probably might as well write:
>>> 
>>>     buf = malloc(buf, 200);

Oops, what I *meant* was

    buf = realloc(buf, 200);

>>> On the other other hand, cleaning up after yourself is a good habit, and
>>> it's possible that calling free(buf) will free up memory that's needed
>>> by your cleanup code.
>>
>> Very well stated, this error checking for valid dynamic allocations is
>> often overestimated, treated almost religiously.
>>
>> If you don't know how to recover from such a failure other than to
>> abort the program, there is really no point in it.
>
> I don't entirely agree, at least not literally.  Even in the "no
> recovery" case a test for a NULL return, an explicit message saying
> what's wrong, and an immediate abort is better than doing nothing.  This
> is partly because it's so easy: a two-line wrapper around malloc is all
> that's needed, and you only need to write it once in a lifetime!  And if
> you ever port to a system where low-numbered addresses are valid, the
> pay-off is huge.

The point I was making upthread was specifically about realloc() (and of
course I messed that up by typoing "malloc" in place of "realloc").

Assigning the result of realloc() to the pointer object given as the
first argument is considered a bad idea because, if the realloc() call
fails and returns a null pointer, you've lost your only pointer to the
original buffer.  My point was merely that losing that pointer is bad
only if you would have made use of it.  If your response to an realloc()
failure is going to be to abort the program anyway, then you might as
well write

    buf = realloc(buf, new_size);

I did *not* mean to suggest omitting the check for a null pointer.  If
an allocation call fails, you want to deal with it immediately -- even
if you're just going to abort the program.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

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


Thread

Trying to figure out how to dynamically re-allocate strings while concatenating Shriramana Sharma <jamadagni@gmail.com> - 2012-08-04 07:12 -0700
  Re: Trying to figure out how to dynamically re-allocate strings while concatenating Xavier Roche <xroche@free.fr.NOSPAM.invalid> - 2012-08-04 16:17 +0200
  Re: Trying to figure out how to dynamically re-allocate strings while concatenating Xavier Roche <xroche@free.fr.NOSPAM.invalid> - 2012-08-04 16:22 +0200
    Re: Trying to figure out how to dynamically re-allocate strings while concatenating Shriramana Sharma <jamadagni@gmail.com> - 2012-08-04 07:25 -0700
      Re: Trying to figure out how to dynamically re-allocate strings while concatenating Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-08-04 16:04 +0100
  Re: Trying to figure out how to dynamically re-allocate strings while concatenating "Heinrich Wolf" <invalid@invalid.invalid> - 2012-08-04 16:29 +0200
  Re: Trying to figure out how to dynamically re-allocate strings while concatenating Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-04 10:52 -0400
  Re: Trying to figure out how to dynamically re-allocate strings while concatenating pete <pfiland@mindspring.com> - 2012-08-04 12:27 -0400
    Re: Trying to figure out how to dynamically re-allocate strings while concatenating Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-04 13:56 -0400
      Re: Trying to figure out how to dynamically re-allocate strings while  concatenating pete <pfiland@mindspring.com> - 2012-08-04 17:12 -0400
  Re: Trying to figure out how to dynamically re-allocate strings while concatenating pete <pfiland@mindspring.com> - 2012-08-04 12:42 -0400
    Re: Trying to figure out how to dynamically re-allocate strings while concatenating Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-04 14:00 -0400
    Re: Trying to figure out how to dynamically re-allocate strings while concatenating Keith Thompson <kst-u@mib.org> - 2012-08-04 17:00 -0700
      Re: Trying to figure out how to dynamically re-allocate strings while concatenating pete <pfiland@mindspring.com> - 2012-08-04 20:48 -0400
      Re: Trying to figure out how to dynamically re-allocate strings while concatenating Jens Gustedt <jens.gustedt@loria.fr> - 2012-08-05 09:22 +0200
        Re: Trying to figure out how to dynamically re-allocate strings while concatenating Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-08-05 12:49 +0100
          Re: Trying to figure out how to dynamically re-allocate strings while concatenating Jens Gustedt <jens.gustedt@loria.fr> - 2012-08-05 17:12 +0200
          Re: Trying to figure out how to dynamically re-allocate strings while concatenating Keith Thompson <kst-u@mib.org> - 2012-08-05 13:17 -0700
            Re: Trying to figure out how to dynamically re-allocate strings while concatenating Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-08-05 22:53 +0100
  Re: Trying to figure out how to dynamically re-allocate strings while concatenating Shriramana Sharma <jamadagni@gmail.com> - 2012-08-04 19:20 -0700
  Re: C Vs Asm i use for cat function "io_x" <a@b.c.invalid> - 2012-08-05 08:15 +0200
    Re: C Vs Asm i use for cat function "BartC" <bc@freeuk.com> - 2012-08-05 10:22 +0100
      Re: C Vs Asm i use for cat function "Heinrich Wolf" <invalid@invalid.invalid> - 2012-08-05 17:00 +0200
        Re: C Vs Asm i use for cat function Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-08-05 16:20 +0100
      Re: C Vs Asm i use for cat function "io_x" <a@b.c.invalid> - 2012-08-05 19:51 +0200
        Re: C Vs Asm i use for cat function "BartC" <bc@freeuk.com> - 2012-08-05 21:04 +0100
  Re: Trying to figure out how to dynamically re-allocate strings while concatenating Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-08-10 10:45 +0300

csiph-web