home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!stanford.edu!agate!usenet.ins.cwru.edu!magnus.acs.ohio-state.edu!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!usc!news.service.uci.edu!unogate!mvb.saic.com!macro32
- From: REGI's <NUNEZ@esevvx.cica.es>
- Newsgroups: vmsnet.internals
- Subject: (None)
- Message-ID: <01GPZCQEJM800001ZZ@esevvx.cica.es>
- Date: Thu, 15 Oct 1992 15:00:00 UTC+0100
- Organization: Macro32<==>Vmsnet.Internals Gateway
- X-Gateway-Source-Info: Mailing List
- Lines: 101
-
- >X-Envelope-to: MACRO32@WKUVX1.bitnet
- >X-VMS-To: IN::"MACRO32@WKUVX1.BITNET"
-
- 1
- 14-OCT-1992 14:39:48.11
- 1 00:00:00.00
- 15-OCT-1992 14:39:48.11
- @UKCC.uky.edu:MacroMan@WKUVX1.BITNET
- gutier@SEVAXU.CICA.ES
-
- Received: from UKCC.uky.edu by ESEVVX.CICA.ES ; 11-OCT-1992 14:39:46.15
- Received: from ukcc.uky.edu by UKCC.uky.edu (IBM VM SMTP V2R2)
- with BSMTP id 0348; Sun, 11 Oct 92 09:38:42 EDT
- Received: from WKUVX1.BITNET by ukcc.uky.edu (Mailer R2.08) with BSMTP id 8854;
- Sun, 11 Oct 92 09:38:41 EDT
- Errors-To: MacroMan@WKUVX1.BITNET
- X-ListName: "VMS Internals, MACRO, and BLISS Discussions"
- <MACRO32@WKUVX1.BITNET>
- Received: from CUNYVM.BITNET (MAILER) by WKUVX1 (MX V3.1C) with BSMTP; Sun, 11
- Oct 1992 08:30:12 CDT
- Received: from CUNYVM by CUNYVM.BITNET (Mailer R2.08) with BSMTP id 2186; Sun,
- 11 Oct 92 08:42:28 EDT
- Received: from MVB.SAIC.COM by CUNYVM.CUNY.EDU (IBM VM SMTP V2R2) with TCP;
- Sun, 11 Oct 92 08:42:26 EDT
- Relay-Version: VMS News - V6.0-3 14/03/90 VAX/VMS V5.5; site arizona.edu
- X-Newsgroups: vmsnet.internals,vmsnet.misc,comp.lang.c
- Subject: Re: malloc() as a user defined function
- Message-ID: <1992Oct11.054708.26380@organpipe.uug.arizona.edu>
- From: <dave@cs.arizona.edu>
- Date: 11 Oct 92 05:47:08 GMT
- Reply-To: MACRO32@WKUVX1.BITNET
- Sender: news@organpipe.uug.arizona.edu
- Followup-To: vmsnet.internals
- References: <Bvv6pH.Gzy@cs.psu.edu>
- Organization: University of Arizona
- Summary: a new() idea
- In-Reply-To: ytang@red.crayola.cs.psu.edu (Yuan-Ling Tang)
- Lines: 59
- Xref: arizona.edu vmsnet.internals:1490 vmsnet.misc:1304 comp.lang.c:27846
- To: MACRO32@WKUVX1.BITNET
- X-Gateway-Source-Info: USENET
-
- In article <Bvv6pH.Gzy@cs.psu.edu>, ytang@red (Yuan-Ling Tang) writes:
- > In programming on dynamic memory allocation, I prefer doing check on
- > every malloc(). I.e.:
- >
- > if ((p=(SOME_TYPE *)malloc(sizeof(SOME_TYPE))) == NULL) {
- > printf("Malloc error.\n");
- > exit(0);
- > }
- >
- > It turned out it's a pain to type in the above four lines if I have
- > lots of malloc() invocations in the program, also, the program looks
- > ugly. So I tried to use a function called my_malloc():
- >
- > SOME_TYPE *my_malloc()
-
- [deleted, coded as above]
- >
- > [...] what if I have many distinct types of memory space to allocate?
-
- Here's an idea I cribbed off of a compiler I worked on last semester (the
- source is up for ftp, so it should be OK to use & distribute freely):
-
- Somewhere, I have the following function:
-
- void *mustalloc( size_t size ) {
- void *t = malloc(size) ;
- if( t == NULL ) FATAL( "malloc returns NULL" ) ;
- return t ;
- }
-
- FATAL(), of course, is just a simple print 'n' exit routine, which I made
- a seperate subroutine since such a thing is often genrally useful in a
- large program.
-
-
- Along with it, I have the following macros:
-
- extern void *mustalloc( size_t ) ;
-
- #define new(t ) ((t *)mustalloc( sizeof(t) ))
- #define anew(t,s) ((t *)mustalloc( sizeof(t) * (s) ))
-
- With this, I can write code like
-
- struct foo *p = new(struct FOO) ;
- struct foo foo_array = anew(struct FOO, 100) ;
-
- This provides the advantages of a clean interface to a properly typed
- dynamic allocation routine, while hiding the ugly details. An added
- benefit is that if you do all your allocation through mustalloc(), you
- automatically have a place to put malloc() wrapper code if you need to
- do malloc()/free() debugging (or a place to do garbage collection, if
- you prefer).
-
- I've been using this pretty much since I saw it. IMHO, it works really
- great, and I would encourage others to try it too.
-
- --
- Caught an internal error--.brainrc restored
-