delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/01/10/21:38:36

From: "T.W. Seddon" <T DOT W DOT Seddon AT ncl DOT ac DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Another aspect of free() ?
Date: 10 Jan 1997 19:05:42 GMT
Organization: University of Newcastle upon Tyne
Lines: 44
Message-ID: <5b63u6$lbc@whitbeck.ncl.ac.uk>
References: <199701091231 DOT HAA02294 AT delorie DOT com>
NNTP-Posting-Host: glen18.ncl.ac.uk
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

DJ Delorie (dj AT delorie DOT com) wrote:

> xmalloc() checks for null return from malloc, and dies if so.
> xrealloc() checks for you passing a null, and calls malloc() if so.
> xfree() checks for you passing a null, and ignores it if so.

I wrote my own xmalloc(), xrealloc() and xfree() since I used to do this when
I used TC++. They are something like this:

void *xmalloc(size_t x) {
	void *tmp;
	
	tmp=malloc(x);
	if(tmp==NULL) err("malloc error()");
	return(tmp);
}

void *xrealloc(void *x,size_t y) {
	void *tmp;
	
	tmp=realloc(x,y);
	if(tmp==NULL) err("realloc error");
	return(tmp);
}

My program often calls xrealloc() with a NULL pointer, for example when
creating an array of structs from scratch. Is it calling my xrealloc
or djgpp's xrealloc? (The declarations for xrealloc() etc are /**/'d out
in the .h file declaring them, which I *do* include, but my program gets
linked with the standard C library which I presume contains them as well
as the object file containig my xrealloc() etc.)

I'd be interested to know since I though that
ANSI says realloc(NULL,x)==malloc(x); if my xrealloc is called, and realloc
is called from within it, am I heading for a big crash at some point? I've
never had any problems myself, but I'd like to know while I still have the
inclination to change it. 

--Tom

PS I may have the pointer and the new size argument the wrong way round above.
When writing my own code I can always look for the last point it was used to
remind me :-)

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019