Article View: vmsnet.internals
Article #343malloc() as a user defined function
From: Yuan-Ling Tang
Date: Fri, 09 Oct 1992 17:17
Date: Fri, 09 Oct 1992 17:17
33 lines
1394 bytes
1394 bytes
Hi netters:
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()
{
SOME_TYPE *p;
if ((p=(SOME_TYPE *)malloc(sizeof(SOME_TYPE))) == NULL) {
printf("Malloc error.\n");
exit(0);
}
return(p);
}
However another problem arises, what if I have many distinct types
of memory space to allocate? That is, I can't use the same type in
calling my_malloc(). So how can I pass a type to a function?
Thanks for your reponse.
- Tang
Technical Details
Message-ID:
<Bvv6pH.Gzy@cs.psu.edu>Path:
arc1-ca.pugleaf.net!sparky!uunet!stanford.edu!ames!elroy.jpl.nasa.gov!swrinde!zaphod.mps.ohio-state.edu!uwm.edu!psuvax1!red.crayola.cs.psu.edu!ytangShow Full Headers
Complete Article Headers
Newsgroups: vmsnet.internals,vmsnet.misc,comp.lang.c Path: arc1-ca.pugleaf.net!sparky!uunet!stanford.edu!ames!elroy.jpl.nasa.gov!swrinde!zaphod.mps.ohio-state.edu!uwm.edu!psuvax1!red.crayola.cs.psu.edu!ytang From: ytang@red.crayola.cs.psu.edu (Yuan-Ling Tang) Subject: malloc() as a user defined function Message-ID: <Bvv6pH.Gzy@cs.psu.edu> Sender: news@cs.psu.edu (Usenet) Nntp-Posting-Host: red.crayola.cs.psu.edu Organization: Penn State Computer Science Date: Fri, 9 Oct 1992 17:17:40 GMT Lines: 33