delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/01/05:00:43

From: Gavin Sinclair <spammers AT must DOT die>
Newsgroups: comp.os.msdos.djgpp
Subject: Problems with malloc/new
Date: Sat, 27 Sep 1997 17:05:55 +1100
Organization: University of New South Wales
Lines: 47
Message-ID: <342CA243.7035@must.die>
Reply-To: spammers AT must DOT die
NNTP-Posting-Host: max314092.servers.unsw.edu.au
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

I have been having trouble with malloc and new, and I was wondering if
there is anything obvious I might be doing wrong.  I am using DJGPP 2.01
and programming in C++.

With malloc, I was finding that it SOMETIMES fails.  That is, I'd run
the exact same executable several times in a row, and about half the
time my debugging apparatus (printing to a file) would post-humously
report that the pointer I had tried to allocate was 0.

Giving up, I used new instead, and now, again about half the time, the
entire program fails, saying "Virtual memory exceeded by 'new'".  At
least with malloc the program ran!  (With only one side-effect, too).

To make my description more concrete, I'll provide the key code.

	Baddie **badDude = (Baddie **)malloc(total * sizeof(Baddie *));

OR
--

	Baddie **badDude = new (Baddie *)[total];


FOLLOWED BY
-----------


	for (int i = 0; i < total; i++)
		badDude[i] = new Baddie();



(of course, all memory is 'free'd or 'delete'd in the destructor.


What is _really_ strange is that, in the case of malloc failing, the
last piece of code above (the for loop) still works.  How can you
reference an offset from a NULL pointer?


Does any of this sound familiar???

-- 
Gavin Sinclair
Sydney, Australia

Real mail address is s2174215 AT cse DOT unsw DOT edu DOT au

- Raw text -


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