public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: R.E.Wolff@BitWizard.nl (Rogier Wolff)
To: Matti Aarnio <matti.aarnio@zmailer.org>
Cc: Lyle Coder <x_coder@hotmail.com>,
	David Schwartz <davids@webmaster.com>,
	RAJESH BALAN <atmproj@yahoo.com>,
	linux-kernel@vger.kernel.org
Subject: Re: malloc(1/0) ??
Date: Wed, 8 Nov 2000 01:29:27 +0100 (MET)	[thread overview]
Message-ID: <200011080029.BAA06851@cave.bitwizard.nl> (raw)
In-Reply-To: <20001107104634.G13151@mea-ext.zmailer.org> from Matti Aarnio at "Nov 7, 2000 10:46:34 am"

Matti Aarnio wrote:
> needed size is bound to get user burned.   malloc(0)  is insane thing
> (IMO), but at least glibc supports it for some reason.  Likely just due
> to padding and minimum size issues.

Part of the desing of the C language and the library is intended to
make boundary conditions go well automatically. 

So, a program that does:

	fscanf (file, "%d", &numsquares);
	squares = malloc (sizeof (struct square) * numsquares);
	for (i=0;i<numsquares; i++)
	   read_square_from_file (file, &squares[i]);

	fscanf (file, "%d", &numtriangles);
	triangles = malloc (sizeof (struct triangle) * numtriangles);
	for (i=0;i<numtriangles; i++)
	   read_triangle_from_file (file, &triangles[i]);


	[use the stuff....]

	free (triangles);
	free (squares);

should work. See, the "for" loop nicely executes 0 times when
numtriangles is zero. Similarly, malloc/free don't have any
"exception" case for the "numtriangles is zero" case.

Now, at first you might say that "malloc" could return NULL, as long
as "free" accepts it. However, that's not true: The result of the
malloc call can and acutally should be checked for "NULL", and the
program might abort.

A valid implementation MAY:
	

	#define NIL ((void *) 1)

	void * malloc (int size)
	{
	if (size == 0) NIL;
	[....]
	}

	void free (void *ptr)
	{
	if (ptr == NIL) return;
	[...]
	}

This way all should work. However someone mentioned that the returns
from "malloc" should be unique. Why would that be? That would prohibit
my "1" trick. The statement implies you want to go about checking
pointers for equality. If for example you have a memcmp (a, b) that
has "if (a == b) return 0;" at the beginning. That would be allowed
for the NIL pointers. (all malloc-0 results SHOULD compare equal
anyway: there are 0 differences....)

				Roger. 

-- 
** R.E.Wolff@BitWizard.nl ** http://www.BitWizard.nl/ ** +31-15-2137555 **
*-- BitWizard writes Linux device drivers for any device you may have! --*
*       Common sense is the collection of                                *
******  prejudices acquired by age eighteen.   -- Albert Einstein ********
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/

  reply	other threads:[~2000-11-08  0:29 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-11-07  3:59 malloc(1/0) ?? RAJESH BALAN
2000-11-07  7:54 ` David Schwartz
2000-11-07  7:59   ` Andrej Hosna
2000-11-07  8:50     ` David Schwartz
2000-11-07  8:09   ` Lyle Coder
2000-11-07  8:46     ` Matti Aarnio
2000-11-08  0:29       ` Rogier Wolff [this message]
2000-11-08  0:36         ` David Schwartz
2000-11-08  0:54     ` Igmar Palsenberg
2000-11-08  0:50   ` Igmar Palsenberg
2000-11-08 22:11     ` H. Peter Anvin
2000-11-08 22:11       ` Rasmus Andersen
2000-11-09 16:03       ` Igmar Palsenberg
2000-11-08  0:41 ` Igmar Palsenberg
2000-11-07 23:58   ` Tim Waugh
2000-11-08 12:38     ` Igmar Palsenberg
  -- strict thread matches above, loose matches on Subject: below --
2000-11-07  6:45 Dan Kegel
2000-11-07  7:13 ` J. Dow
2000-11-07  7:52   ` David Schwartz
2000-11-08  0:47   ` Igmar Palsenberg
2000-11-07  9:26 malloc (1/0) ?? David Feuer
2000-11-07 16:12 malloc(1/0) ?? Jesse Pollard
2000-11-07 16:38 ` lost

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200011080029.BAA06851@cave.bitwizard.nl \
    --to=r.e.wolff@bitwizard.nl \
    --cc=atmproj@yahoo.com \
    --cc=davids@webmaster.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=matti.aarnio@zmailer.org \
    --cc=x_coder@hotmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox