Re: (MS-)DOS PC on a microcontroller??



Jack Klein wrote:
CBFalconer <cbfalconer@xxxxxxxxx> wrote:
larwe wrote:
Vladimir Vassilevsky <antispam_bo...@xxxxxxxxxxx> wrote:

For example: in the MS DOS, it is perfectly valid to allocate a
memory block of zero bytes. In PTS-DOS and some others it results
in an error. It appears that surprisingly many programs are
actually making the requests of zero bytes, so here is a
compatibility problem.

Let me guess... the 0 byte malloc code is in the runtime libraries
for Microsoft C, so that no kosher program will run properly on
non-kosher DOS clones.

See the following from the C standard. Note the provision for a
size request of zero. This is easily missed, because it applies to
all of malloc, realloc, and calloc and thus is isolated in the
standard.

7.20.3 Memory management functions

[#1] The order and contiguity of storage allocated by
successive calls to the calloc, malloc, and realloc
functions is unspecified. The pointer returned if the
allocation succeeds is suitably aligned so that it may be
assigned to a pointer to any type of object and then used to
access such an object or an array of such objects in the
space allocated (until the space is explicitly freed or
reallocated). Each such allocation shall yield a pointer to
an object disjoint from any other object. The pointer
returned points to the start (lowest byte address) of the
allocated space. If the space cannot be allocated, a null
pointer is returned. If the size of the space requested is |
zero, the behavior is implementation-defined: either a null |
pointer is returned, or the behavior is as if the size were |
some nonzero value, except that the returned pointer shall |
not be used to access an object. The value of a pointer |
that refers to freed space is indeterminate. |
|
This is the critical provision ----------------------'

Chuck, I think you're missing the point here. There is not
necessarily a relationship between the C library being required to
support malloc/calloc requests for 0 bytes and whether or not the
operating system chokes on zero bytes.

Given any real world implementation of malloc etc., a C library will
do one of the following:

-- return a NULL pointer without even calling the OS allocation
routine.

But that return normally is treated as an error signal. Using such
a library is valid, but the caller has to detect the zero size and
ignore that false error signal. I don't think the world realizes
that.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

.



Relevant Pages

  • Re: (part 10) More Schildt-like errors in Dicky Heathens book
    ... like mallocif mallocreturns a non-NULL pointer. ... If one is using a debugging malloc which checks that all accesses are ... when sz is zero, it gives the debugging allocator an opportunity to ...
    (comp.lang.c)
  • Re: (MS-)DOS PC on a microcontroller??
    ... memory block of zero bytes. ... The pointer returned if the ... Each such allocation shall yield a pointer to ... support malloc/calloc requests for 0 bytes and whether or not the ...
    (comp.arch.embedded)
  • Re: Strtol vs sscanf
    ... sscanf() format string. ... every pointer requires a dynamic allocation! ... > the malloc error's before exiting which variables failed to get ...
    (comp.lang.c)
  • Re: (MS-)DOS PC on a microcontroller??
    ... memory block of zero bytes. ... The pointer returned if the ... Each such allocation shall yield a pointer to ...
    (comp.arch.embedded)
  • Re: malloc() and alignment
    ... types of allocation function: ... malloc, generic allocator, same as current malloc ... And if you already need alignment sufficient ... pointer value invokes undefined behavior. ...
    (comp.lang.c)