home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!igor!thor!rmartin
- From: rmartin@thor.Rational.COM (Bob Martin)
- Newsgroups: comp.lang.c++
- Subject: Re: C++ Memory Management
- Keywords: Memory, Heap, new, delete
- Message-ID: <rmartin.715700914@thor>
- Date: 5 Sep 92 13:48:34 GMT
- References: <37674@sdcc12.ucsd.edu>
- Sender: news@Rational.COM
- Distribution: ca
- Lines: 44
-
- acanter@sdcc3.ucsd.edu (Adriaan Canter) writes:
-
-
- |Hello, I've noticed that Borland Turbo C++ (v 1.0) will actually reserve 16
- |bytes when the "new" function is used to allocate space for a 2 byte
- |integer, i.e. the following code:
-
- |int * intptr;
- |intptr = new int;
-
- |will allocate 16 bytes, as measured using the "coreleft()" function,
- |under the small memory model. A subsequent second allocation of this type will
- |fit the second integer into the first 16 byte space allocated, but a third call
- |will block off an additional 16 bytes, and so on, so that 4 two-byte integers
- |will take up 32 bytes of space, for example. I guess this happens because some
- |space is reserved to point to the next location in the heap, although the
- |amount seems excessive. This is pretty wasteful of course, if I want to
- |dynamically allocate alot of structures. Is there anyway I can control how
- |much memory is actually reserved when the "new" function is called? Do the
- |newer versions of C++ have some memory managment classes (as I've over-heard)
- |that can fix this? Or do I have to finagle my own scheme, overloading the
- |"new" and "delete" operators. Forgive me if this is a dumb question, but my
- |manuals don't say very much about it. Thank you.
-
- Some minimal amount of storage overhead must be incurred by the
- general memory management schemes. In the case you describe above,
- this overhead comes to 6 bytes. That's not too bad. It amounts to a
- pointer and a size...
-
- If you want to decrease your overhead below the empirical 6 bytes,
- then you must concoct your own memory management scheme. This is
- easiest to do if the objects you want to allocate are all the same
- size. You can overload operator new for that class, and then allocate
- large arrays of storage. Two arrays will do. One serves as that
- actual storage that you will dish out with your overloaded new, and
- the other array can act as a bitmap to record whether a cell in your
- array is free or not. This amounts, theoretically, to a one bit
- overhead per block.
-
- --
- Robert Martin Training courses offered in:
- R. C. M. Consulting Object Oriented Analysis
- 2080 Cranbrook Rd. Object Oriented Design
- Green Oaks, Il 60048 (708) 918-1004 C++
-