delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/05/27/11:19:36

Xref: news2.mv.net comp.os.msdos.djgpp:4280
From: jlouwere AT galaxy DOT csc DOT calpoly DOT edu (Jan Louwerens)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Trapping failed malloc calls
Date: 24 May 1996 19:08:09 GMT
Organization: Cal Poly Computer Science Dept.
Lines: 34
Message-ID: <4o51ep$m4@waldorf.csc.calpoly.edu>
References: <4o40cb$khi AT news DOT manawatu DOT gen DOT nz>
NNTP-Posting-User: jlouwere AT galaxy DOT csc DOT calpoly DOT edu
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Malcolm Taylor (malcolm AT manawatu DOT gen DOT nz) wrote:
: Hi,
: 	I am wondering if there is an easy way of trapping failed malloc and
: new calls within a program. At present the program will just exit with
: a stack trace that often hides the fact that the program is simply
: running out of memory (eg a memory leak bug maybe). I would like to be
: able to trap a failed call to malloc (or new) and exit gracefully with
: a message about running out of memory.

: Thanks,
: Malcolm

Buffer = malloc(size);
if (Buffer == NULL) {
   printf("Error allocating memory for Buffer!\n"
   exit(-1);
}

This should work for malloc.

--
                                           _____  ___________
                                          |\    \|\          \
  _____  _____                            | \    \ \     _____\
 |\    \|\    \                           \  \    \ \    \    |
 | \    \ \    \                         _____\    \ \    \______
 \  \    \ \    \                       |\          \ \          \
  \  \    \ \    \                      | \_____     \ \     _____\
 _____\    \ \    \_____                \ |     \     \ \    \    |
|\          \ \         \                \|______\     \ \    \___|
| \__________\ \_________\                 |\           \ \    \
\ |          | |         |                 | \___________\ \____\
 \|__________|\|_________|                 \ |           | |    |
                                            \|___________|\|____|

- Raw text -


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