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


Groups > comp.lang.c > #41539

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-10 10:20 +0100
Organization A noiseless patient Spider
Message-ID <lfk055$noo$1@dont-email.me> (permalink)
References <401aa474-05e6-4cf4-a3c2-5e2c2343157e@googlegroups.com> <20140308224204.214@kylheku.com> <lfiui1$p8q$1@dont-email.me> <ef422329-dc31-4670-ac9b-a486542dd6fa@googlegroups.com>

Show all headers | View raw


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>
> 
>> 
>> 
>> 
>> There are (at least) two types of program for which an empty free()
>> and no garbage collection would be perfectly acceptable.  The first
>> is programs that don't allocate heap memory at all - you can do a
>> lot without it.  In the world of small-system embedded programming,
>> malloc() and free() are rare, and are often banned entirely by
>> coding standards. The second would be programs that don't need much
>> re-use of memory, and run under an OS - they can simply keep all
>> their memory until the program ends and the OS frees it.  There is
>> also the combination - embedded programs that allocate memory
>> initially, but never need to free it because they never end (until
>> someone turns the power off).
>> 
>> 
>> The few times I have used malloc() and free() in small embedded
>> systems, the malloc() just used a simple index in a
>> statically-allocated memory pool, and free() was empty.
> 
> 
> If I understand correctly, the standard makes allowance for embedded
> applications which it calls freestanding environment which do not
> benefit from an operating system.
> 

Yes.

> 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.

Even in embedded systems, of course, these functions are usually
implemented "sensibly" in the library.  It is the choice of the user to
overrule them with a simple implementation (such as an empty free()) if
that suits the application.

> 
> I too have used C on little micros - and I agree, you don't really
> need malloc to manage 256 bytes of ram! And if you did, the standard
> does allow a lot of leeway in how those functions are implemented, so
> an empty free() would be allowed.

People writing high reliability, safety-critical, or real-time embedded
systems will generally avoid any sort of dynamic memory if they can -
memory size has nothing to do with it.  If it is unavoidable (it is
difficult to implement a network stack efficiently without some dynamic
memory handling, for example), then they normally write specific and
targeted dynamic memory handling with fixed size pools.  The trouble
with standard malloc() and free() is that it is extremely difficult to
reason about them, it is very difficult to guess their timing, they can
sometimes fail, and general malloc() algorithms are prone to memory
fragmentation which can be a huge problem in embedded systems (without
virtual memory).

> 
> However, the context of my original question was for a system where
> the program was running with the benefit of an operating system and
> as such, "shall conform to the following specifications...", one of
> which free() is. (See section Hosted Environment.)
> 

Fair enough - I was just pointing out some wider variety.

My own reading of the standards is that an empty free() is allowed in
hosted environments, and it could be useful since the OS will re-claim
the memory on program termination anyway.  (That assumes an
appropriately powerful OS, of course.)

> Thanks,
> 
> ~Jesse
> 

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