Xref: news2.mv.net comp.os.msdos.djgpp:2689 From: Massimiliano Mantione Newsgroups: comp.os.msdos.djgpp Subject: Re: malloc crash Date: Fri, 12 Apr 1996 10:22:52 +0200 Organization: Computer Science Dep. - Milan University Lines: 102 Message-ID: <316E12DC.5B3@mailer.cefriel.it> References: <316CE14F DOT 5DDE AT matrust DOT es> NNTP-Posting-Host: puccini.cefriel.it Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: Jesus Canal DJ-Gateway: from newsgroup comp.os.msdos.djgpp Jesus Canal wrote: > > Eli Zaretskii wrote: > > Btw, allocating 5000 chunks of 10KB is a symptom of a very > > memory-inefficent program (IMHO). If a program indeed needs so many > > allocations, it should allocate memory in a few large chunks and then > > overload `new' with something that serves allocations off those chunks. > > > > I implemented a ZBuffer class as: > > (...) > > int theHeight; > int theWidth; > double **theArray > > (...) > > The constructor allocated one (double *) array, theWidth items long, > and then theWidth double arrays, theHeight items long. > Then accessing the element (X, Y) was theArray [X][Y]. > > I will try to avoid the malloc crash using only a double * array: > > (...) > > int theHeight; > int theWidth; > double *theArray > > (...) > > and accessing the element (X,Y) as theArray [theWidth * Y + X] I'm not a true hacker, but I think your program will also get a small speed improvement from this change. And if you are using C++, you could put there a: inline double ZBufferElement(int X, int Y) { return theArray[theWidth * Y + X]}; and take the benefits of ``information hiding'' (actually, in C++ the info are not hidden at all, you see them in the header file but you ``cannot'' -should not- use them). If (for some reason) you do not like this memory access method, you can always use your previous one, but implementing Eli's advice like this: (...) private: int theHeight; int theWidth; double *theArray double **theArrayRows public: inline double ZBufferElement(int X, int Y) { return theArrayRows[X][Y]}; (...) and using a constructor that initializes the ``theArrayRows'' pointers to the correct address of each row in ``theArray''. With this trivial trick, you would: [1] Waste a bit of memory (but not so much, compared to the whole ZBuffer). [2] malloc only once a great amount of memory, and then manage to use it as you like (it was just what Eli was telling you). And, since you are using C++ in a proper way, your code will always be the same (i.e., ZBufferElement(X,Y) in each case). I hope this (even if trivial) helps. All the best Massimiliano P.S. To all the ``C'' fans: I know you can do *exactly* the same things without C++, defining proper macros, and using only them... I can do in C *everything* I do in C++, also polimorphic classes (using function pointers). I just like C++, and do not want to start a war. -- /---------------------------------------------------------\ 0 | Massimiliano Mantione [mantione AT mailer DOT cefriel DOT it] | 0 /--+---------------------------------------------------------+--\ | /\ ._. /\ | | __/ \__ / o \ /` ,__ /` __/ \__ | | \ \ / / \ `-' /_ ___\ / __ ____, \ \ / / | | >--()--< ,-. \ / \ / | / / \ / / / >--()--< | | /__/ \__\ |o' / / / \__/ / \__/ / / / /__/ \__\ | | \ / `--' ' ` '` ` ` ` \ / | | \/ ------------------------------------ \/ | \--+---------------------------------------------------------+--/ 0 | CEFRIEL Via Emanueli, 15 (20126 MILANO) [Italy] | 0 \---Tel: +39-2-66100083 (66100750) [Fax: +39-2-66100448]--/