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


Groups > comp.lang.c > #41605

Re: Does ANSI C allow free() to always be empty?

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: Does ANSI C allow free() to always be empty?
Date 2014-03-11 12:52 +0100
Organization A noiseless patient Spider
Message-ID <lfmte7$dg4$1@dont-email.me> (permalink)
References (1 earlier) <20140308224204.214@kylheku.com> <lfiui1$p8q$1@dont-email.me> <ef422329-dc31-4670-ac9b-a486542dd6fa@googlegroups.com> <lfk055$noo$1@dont-email.me> <e20b6973-d131-48a6-8e8f-2fdf3a2a61d0@googlegroups.com>

Show all headers | View raw


On 11/03/14 04:51, partremmaps@gmail.com wrote:
> On Monday, March 10, 2014 2:20:37 AM UTC-7, David Brown wrote:
>> On 10/03/14 06:37, partremmaps@gmail.com wrote:
>> 
>>> On Sunday, March 9, 2014 4:47:12 PM UTC-7, David Brown wrote:
>> 
>>>> On 09/03/14 07:55, Kaz Kylheku wrote:
>> 
>>>> 
>> 
>>>>> On 2014-03-09, partremmaps@gmail.com <partremmaps@gmail.com>
>> 
>>>>> wrote:
>> 
>>>> 
>> 
>>>>>> According to the ANSI C Standard, may free() be an empty
>> 
>>>>>> function?
>> 
>>>> 
>> 
>>> 
>> 
>>> 
> 
> <snip>
> 
>> 
>> 
>>> And indeed, if I understand correctly, in a freestanding
>>> environment,
>> 
>>> it appears that the standard allows library facilities to be
>> 
>>> implementation defined - which would allow for free() to be
>>> empty.
>> 
>> 
>> 
>> Yes.  I think there are some restrictions about what the library
>> 
>> facilities should do, even in a freestanding environment - but you
>> don't
>> 
>> need to implement everything.  In particular, a freestanding
>> 
>> implementation does not have to include <stdlib.h>.
>> 
>> 
>> 
>> As far as I read the wording in the C11 standards, a conforming
>> hosted
>> 
>> implementation /could/ have malloc() always return 0, and free() be
>> empty.
>> 
> 
> 
> Could you explain which part of C11 would allow malloc() to always
> return 0 and free() to be empty?
> 
> 
> I see in the standard:
> 
> 5.1.2.2 [Hosted environment]
> 
> 1   A hosted environment need not be provided, but shall conform to
> the following specifications if present.
> 
> then the description of malloc() and free() are below that at 7.22.3
> [Memory management functions].
> 
> 
> Doesn't that look like a hosted environment shall conform if
> present?
> 
> (At first when I read 5.1.2.2, I thought it meant that the following
> functions were optional in a hosted environment, but that any
> function present must conform. However, it really looks like the
> wording says that if a hosted environment is provided, it shall
> conform to all of the following specifications.)
> 
> And the description of malloc() and friends clearly all describe a
> general effort to allocate, and "7.22.3 [Memory management
> functions]" says "The pointer returned if the allocation succeeds is
> suitably aligned so that it may be ...."
> 
> Notice "if the allocation succeeds." One cannot have the chance to
> succeed if they do not attempt, and simply returning NULL is not
> attempting anything.
> 
> Also, realloc uses both the phrases "If memory for the new object
> cannot be allocated," and "or a null pointer if the new object could
> not be allocated."
> 
> I really don't see how the standard allows for malloc to simply
> return NULL without attempting to allocate.
> 
> 

Under 7.22.3.4:

"The malloc function returns either a null pointer or a pointer to the
allocated space."


I believe that this can be interpreted as allowing malloc() to return a
null pointer for every invocation.

Of course, such an implementation would be useless, and a toolchain that
had such an implementation would be unpopular.  And I don't think the
standards document intended such an "implementation" to be considered
conforming - but I think the language of the standard allows it.


It is also worth noting that there is no way (with just the
standard-required library) for a program to distinguish between an
"always null" malloc() and a real malloc that cannot allocate the memory
that was asked for.  Similarly, there is no way for a program to
distinguish between an empty free() and one that does useful work.

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


Thread

Does ANSI C allow free() to always be empty? partremmaps@gmail.com - 2014-03-08 22:08 -0800
  Re: Does ANSI C allow free() to always be empty? Kaz Kylheku <kaz@kylheku.com> - 2014-03-09 06:55 +0000
    Re: Does ANSI C allow free() to always be empty? David Brown <david.brown@hesbynett.no> - 2014-03-10 00:47 +0100
      Re: Does ANSI C allow free() to always be empty? partremmaps@gmail.com - 2014-03-09 22:37 -0700
        Re: Does ANSI C allow free() to always be empty? David Brown <david.brown@hesbynett.no> - 2014-03-10 10:20 +0100
          Re: Does ANSI C allow free() to always be empty? Keith Thompson <kst-u@mib.org> - 2014-03-10 08:52 -0700
          Re: Does ANSI C allow free() to always be empty? partremmaps@gmail.com - 2014-03-10 20:51 -0700
            Re: Does ANSI C allow free() to always be empty? Robert Wessel <robertwessel2@yahoo.com> - 2014-03-11 01:08 -0500
            Re: Does ANSI C allow free() to always be empty? David Brown <david.brown@hesbynett.no> - 2014-03-11 12:52 +0100
              Re: Does ANSI C allow free() to always be empty? partremmaps@gmail.com - 2014-03-12 00:09 -0700
                Re: Does ANSI C allow free() to always be empty? David Brown <david.brown@hesbynett.no> - 2014-03-12 12:59 +0100
      Re: Does ANSI C allow free() to always be empty? Kaz Kylheku <kaz@kylheku.com> - 2014-03-10 09:01 +0000
    Re: Does ANSI C allow free() to always be empty? Nick Bowler <nbowler@draconx.ca> - 2014-03-10 15:56 +0000
  Re: Does ANSI C allow free() to always be empty? partremmaps@gmail.com - 2014-03-09 01:11 -0800
    Re: Does ANSI C allow free() to always be empty? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-09 12:39 +0000
      Re: Does ANSI C allow free() to always be empty? gazelle@shell.xmission.com (Kenny McCormack) - 2014-03-09 13:05 +0000
      Re: Does ANSI C allow free() to always be empty? partremmaps@gmail.com - 2014-03-09 22:28 -0700
        Re: Does ANSI C allow free() to always be empty? Martin Shobe <martin.shobe@yahoo.com> - 2014-03-10 07:27 -0500
          Re: Does ANSI C allow free() to always be empty? partremmaps@gmail.com - 2014-03-10 20:55 -0700
            Re: Does ANSI C allow free() to always be empty? Martin Shobe <martin.shobe@yahoo.com> - 2014-03-11 08:19 -0500
              Re: Does ANSI C allow free() to always be empty? partremmaps@gmail.com - 2014-03-11 23:22 -0700
                Re: Does ANSI C allow free() to always be empty? Martin Shobe <martin.shobe@yahoo.com> - 2014-03-12 08:09 -0500
                Re: Does ANSI C allow free() to always be empty? James Kuyper <jameskuyper@verizon.net> - 2014-03-12 11:29 -0400
      Re: Does ANSI C allow free() to always be empty? Martin Shobe <martin.shobe@yahoo.com> - 2014-03-10 07:29 -0500
  Re: Does ANSI C allow free() to always be empty? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-09 09:14 +0000
    Re: Does ANSI C allow free() to always be empty? Robert Wessel <robertwessel2@yahoo.com> - 2014-03-10 15:33 -0500
  Re: Does ANSI C allow free() to always be empty? partremmaps@gmail.com - 2014-03-09 23:05 -0700
    Re: Does ANSI C allow free() to always be empty? Martin Shobe <martin.shobe@yahoo.com> - 2014-03-10 07:43 -0500
      Re: Does ANSI C allow free() to always be empty? gordonb.3qcze@burditt.org (Gordon Burditt) - 2014-03-10 12:05 -0500
        Re: Does ANSI C allow free() to always be empty? partremmaps@gmail.com - 2014-03-10 20:16 -0700
        Re: Does ANSI C allow free() to always be empty? Tim Rentsch <txr@alumni.caltech.edu> - 2014-03-29 16:27 -0700
      Re: Does ANSI C allow free() to always be empty? partremmaps@gmail.com - 2014-03-10 21:48 -0700
        Re: Does ANSI C allow free() to always be empty? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-11 05:52 +0000
  Re: Does ANSI C allow free() to always be empty? partremmaps@gmail.com - 2014-03-20 19:14 -0700
    Re: Does ANSI C allow free() to always be empty? Kaz Kylheku <kaz@kylheku.com> - 2014-03-21 03:25 +0000
      Re: Does ANSI C allow free() to always be empty? Richard Damon <Richard@Damon-Family.org> - 2014-03-21 20:58 -0400
        Re: Does ANSI C allow free() to always be empty? Tim Rentsch <txr@alumni.caltech.edu> - 2014-03-29 16:11 -0700
          Re: Does ANSI C allow free() to always be empty? Richard Damon <Richard@Damon-Family.org> - 2014-03-29 22:27 -0400
            Re: Does ANSI C allow free() to always be empty? Tim Rentsch <txr@alumni.caltech.edu> - 2014-03-30 13:18 -0700
              Re: Does ANSI C allow free() to always be empty? Richard Damon <Richard@Damon-Family.org> - 2014-03-30 20:49 -0400
                Re: Does ANSI C allow free() to always be empty? Tim Rentsch <txr@alumni.caltech.edu> - 2014-04-17 10:52 -0700
            Re: Does ANSI C allow free() to always be empty? James Kuyper <jameskuyper@verizon.net> - 2014-03-31 11:18 -0400
    Re: Does ANSI C allow free() to always be empty? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-21 03:31 +0000
    Re: Does ANSI C allow free() to always be empty? Tim Rentsch <txr@alumni.caltech.edu> - 2014-03-29 15:39 -0700
      Re: Does ANSI C allow free() to always be empty? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-30 04:22 +0000

csiph-web