home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky vmsnet.internals:1428 vmsnet.misc:906 comp.lang.c:14739
- Newsgroups: vmsnet.internals,vmsnet.misc,comp.lang.c
- Path: 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
-
-
- 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
-