Re: Malloc/Free - freeing memory allocated by malloc

From: Richard Bos (rlb_at_hoekstra-uitgeverij.nl)
Date: 10/22/04


Date: Fri, 22 Oct 2004 07:45:14 GMT

richard@cogsci.ed.ac.uk (Richard Tobin) wrote:

> In article <41767a4c.179165393@news.individual.net>,
> Richard Bos <rlb@hoekstra-uitgeverij.nl> wrote:
>
> >> but dynamic alloc memory is not dealloc by OS at application teminated. why?
> >
> >Says who? On most OSes it is, and I'd call any modern OS on which it
> >isn't broken.
>
> A sensible, practical view.
>
> >and dereferencing a
> >null pointer causes just as undefined behaviour as dereferencing an
> >invalid pointer.
>
> A limited, theoretical view.
>
> A modern, general-purpose operating system that doesn't make
            ^^^^^^^^^^^^^^^
That is an extra requirement. Not everybody programs on Unix or MacOS.

> dereferencing a null pointer produce an immediate exception (at least
> by default) has a similar kind of "brokenness" to one which does not
> deallocate memory when a process terminates.

The real problem is not whether or not null pointer writes cause a
crash; the problem is that _relying_ on this behaviour, even on systems
where it does happen, is almost guaranteed to cause you to let down your
guard, and write through a copy of a free()d pointer which you forgot to
set to null when you free()d the original.

Richard



Relevant Pages

  • Re: 100k X 100k data processing summary
    ... Richard Tobin told me there is a system limit that can be changed. ... You are aware that calloc sets the allocated area to all bits zero ... null pointer value when written to pointers. ... for float * to the memory block? ...
    (comp.lang.c)
  • Re: Malloc/Free - freeing memory allocated by malloc
    ... and I'd call any modern OS on which it ... >>null pointer causes just as undefined behaviour as dereferencing an ... >>invalid pointer. ... > dereferencing a null pointer produce an immediate exception (at least ...
    (comp.lang.c)
  • Re: printf() error with long double and null pointer.
    ... Richard Tobin wrote: ... Also, declaring and initialising the long double with LDBL_MAX, but not attempting to pass it to printfalso gives the correct output for the void pointer. ... Maybe it's been corrected by any recent patches, though my MinGW distribution was installed only two months before. ...
    (comp.lang.c)
  • Re: Another question related to pointers.
    ... >> prevent you from invoking undefined behavior like using an unallocated ... >> int buffer; ... > That code is illegal in modern C, though it was probably ok in the ... > the special case of a null pointer constant). ...
    (comp.lang.c)
  • Re: Request
    ... Richard Tobin wrote: ... I see nothing in the standard that prevents me from storing a pointer ...
    (comp.lang.c)