Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #13216

Re: C++ Memory Management Question

From darylew@gmail.com
Newsgroups comp.lang.c++
Subject Re: C++ Memory Management Question
Date 2012-01-10 10:19 -0800
Organization http://groups.google.com
Message-ID <32052065.558.1326219596801.JavaMail.geo-discussion-forums@vbzc6> (permalink)
References <nn4uf7hmc7c5r5jac8cq8qjhsguk8alh77@4ax.com>

Show all headers | View raw


On Saturday, December 31, 2011 8:56:29 AM UTC-5, Datesfat Chicks wrote:
> I'm just learning C++ (after being a 19-year C veteran), so forgive
> any naive questions or concerns.
> 
> It seems to me that C++ has more inherent reliance on dynamic memory
> allocation than C.  It doesn't have to be that way in all cases (many
> classes would be implemented without dynamic allocation), but it seems
> more natural in C++ that dynamic allocation would appear in programs.

No it doesn't.  In fact, each new version of C++ expands other forms of programming outside the "dynamically allocate OOP objects and dispatch messages" model.

However, the big advantage C++ has over C is encapsulation and modeling.  What you had to do in C with manually with "gentleman's agreements" you can now have C++ handle automatically.  For example, the latest version of C++ (2011) lets you specify with built-in integer type will be used as an enum's base type, something you had to do before with a gentleman's agreement via adding a sentinel value of MAX_LONG (or whatever).  Also, this change allows base types smaller than int.

Part of the standard library is several kinds of containers, which encapsulate memory calls for you.

> 
> Here are my questions:
> 
> a)I'm assuming that malloc() and free() are still available in C++?

Yes, all of the C library is accessible from C++.  (C-1999 for C++-2011, C-1989 for previous C++ versions.)  C++ will describe some C functions with added "const" and/or removed "restrict" qualifications.

> 
> b)Do these draw from the same memory pool as "new" and "delete"?

They may or may not.  The only restriction (AFAIK) is that malloc/free cannot secretly use new/delete.  (I guess because this lets the reverse happen without infinite recursion.)  You must use the allocator's designated counter function to do deallocations; no mix-and-match.

> 
> My reason for asking that question is that I'm looking for a graceful
> strategy if dynamic memory gets exhausted.
> 
> The obvious strategy that comes to mind is:
> 
> a)When the program starts, grab a good chunk of memory using malloc().
> 
> b)If the dynamic memory gets exhausted via "new" and friends, handle
> the exception.  In the exception handler, free() the memory allocated
> by (a) and set a flag to signal the program to gracefully exit as soon
> as it can.
> 
> c)Hopefully the memory released by free() would be enough to allow the
> program to exit gracefully (rather than a sudden less controlled
> exit).
> 
> Is this sane?  Is there a better way?

I don't think it's sane.  Think about it: any reserve block large enough to make a difference would be big enough to contribute to the initial problem!  And what if the deallocation routine doesn't marks the memory as free-for-all, but holds on to it a little longer in case of another allocation?  (This may not be a problem depending on which allocation routine is used next.)

I would say just let the memory crunch happen.  If you design your objects right, only the ones directly affected by the lack of memory should be broken down, leaving the others as-is.

Daryle W.

Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

C++ Memory Management Question Datesfat Chicks <datesfat.chicks@gmail.com> - 2011-12-31 08:56 -0500
  Re: C++ Memory Management Question Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-31 09:44 -0500
    Re: C++ Memory Management Question Datesfat Chicks <datesfat.chicks@gmail.com> - 2011-12-31 10:17 -0500
      Re: C++ Memory Management Question "MikeWhy" <boat042-nospam@yahoo.com> - 2011-12-31 12:56 -0600
      Re: C++ Memory Management Question jt@toerring.de (Jens Thoms Toerring) - 2011-12-31 19:05 +0000
    Re: C++ Memory Management Question Carlo Milanesi <carlo.nospam.milanesi@libero.it> - 2011-12-31 18:18 +0100
      Re: C++ Memory Management Question Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-31 13:04 -0500
        Re: C++ Memory Management Question Carlo Milanesi <carlo.nospam.milanesi@libero.it> - 2011-12-31 19:42 +0100
        Re: C++ Memory Management Question Goran <goran.pusic@gmail.com> - 2011-12-31 23:25 -0800
          Re: C++ Memory Management Question Victor Bazarov <v.bazarov@comcast.invalid> - 2012-01-01 09:39 -0500
            Re: C++ Memory Management Question Goran <goran.pusic@gmail.com> - 2012-01-01 08:20 -0800
            Re: C++ Memory Management Question Leigh Johnston <leigh@i42.co.uk> - 2012-01-01 19:05 +0000
              Re: C++ Memory Management Question "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2012-01-01 19:36 +0000
                Re: C++ Memory Management Question Leigh Johnston <leigh@i42.co.uk> - 2012-01-01 19:49 +0000
                Re: C++ Memory Management Question "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2012-01-02 11:26 +0000
                Re: C++ Memory Management Question Leigh Johnston <leigh@i42.co.uk> - 2012-01-02 14:32 +0000
                Re: C++ Memory Management Question "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2012-01-02 15:54 +0000
                Re: C++ Memory Management Question Leigh Johnston <leigh@i42.co.uk> - 2012-01-02 16:26 +0000
                Re: C++ Memory Management Question "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2012-01-02 16:58 +0000
                Re: C++ Memory Management Question Leigh Johnston <leigh@i42.co.uk> - 2012-01-02 21:14 +0000
                Re: C++ Memory Management Question Ian Collins <ian-news@hotmail.com> - 2012-01-03 11:11 +1300
                Re: C++ Memory Management Question scott@slp53.sl.home (Scott Lurndal) - 2012-01-03 20:53 +0000
                Re: C++ Memory Management Question Leigh Johnston <leigh@i42.co.uk> - 2012-01-03 21:21 +0000
                Re: C++ Memory Management Question Ian Collins <ian-news@hotmail.com> - 2012-01-04 12:15 +1300
                Re: C++ Memory Management Question gwowen <gwowen@gmail.com> - 2012-01-04 08:20 -0800
                Re: C++ Memory Management Question "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2012-01-02 22:24 +0000
                Re: C++ Memory Management Question Leigh Johnston <leigh@i42.co.uk> - 2012-01-02 22:55 +0000
                Re: C++ Memory Management Question "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2012-01-02 23:27 +0000
                Re: C++ Memory Management Question Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-10 01:01 -0800
                Re: C++ Memory Management Question 88888 Dihedral <dihedral88888@googlemail.com> - 2012-01-02 21:45 -0800
                Re: C++ Memory Management Question Goran <goran.pusic@gmail.com> - 2012-01-03 00:32 -0800
                Re: C++ Memory Management Question "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2012-01-03 17:34 +0000
                Re: C++ Memory Management Question Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-10 00:47 -0800
                Re: C++ Memory Management Question BCFD36 <bcfd36@cruzio.com> - 2012-01-10 22:57 -0800
              Re: C++ Memory Management Question Ian Collins <ian-news@hotmail.com> - 2012-01-03 09:31 +1300
                Re: C++ Memory Management Question Leigh Johnston <leigh@i42.co.uk> - 2012-01-02 21:02 +0000
                Re: C++ Memory Management Question Ian Collins <ian-news@hotmail.com> - 2012-01-03 11:09 +1300
          Re: C++ Memory Management Question Leigh Johnston <leigh@i42.co.uk> - 2012-01-01 18:57 +0000
      Re: C++ Memory Management Question jgk@panix.com (Joe keane) - 2012-01-01 22:04 +0000
        Re: C++ Memory Management Question Carlo Milanesi <carlo.nospam.milanesi@libero.it> - 2012-01-02 00:37 +0100
          Re: C++ Memory Management Question yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2012-01-09 17:41 +0000
            Re: C++ Memory Management Question Carlo Milanesi <carlo.milanesi@libero.it> - 2012-01-13 08:14 -0800
    Re: C++ Memory Management Question Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-01 18:05 +0000
  Re: C++ Memory Management Question Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-31 07:25 -0800
  Re: C++ Memory Management Question Torsten Mueller <dev-null@shared-files.de> - 2011-12-31 16:41 +0100
  Re: C++ Memory Management Question Nobody <nobody@nowhere.com> - 2012-01-01 09:16 +0000
    Re: C++ Memory Management Question Datesfat Chicks <datesfat.chicks@gmail.com> - 2012-01-02 16:48 -0500
      Re: C++ Memory Management Question Goran <goran.pusic@gmail.com> - 2012-01-03 00:02 -0800
        Re: C++ Memory Management Question Datesfat Chicks <datesfat.chicks@gmail.com> - 2012-01-03 12:53 -0500
          Re: C++ Memory Management Question Nobody <nobody@nowhere.com> - 2012-01-03 19:46 +0000
          Re: C++ Memory Management Question Goran <goran.pusic@gmail.com> - 2012-01-04 00:11 -0800
            Re: C++ Memory Management Question Nobody <nobody@nowhere.com> - 2012-01-04 09:30 +0000
    Re: C++ Memory Management Question Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-04 19:28 +0000
      Re: C++ Memory Management Question Nobody <nobody@nowhere.com> - 2012-01-04 22:09 +0000
        Re: C++ Memory Management Question Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-05 11:43 +0000
  Re: C++ Memory Management Question Christian Gollwitzer <auriocus@gmx.de> - 2012-01-01 18:13 +0100
    Re: C++ Memory Management Question "io_x" <a@b.c.invalid> - 2012-01-01 20:11 +0100
    Re: C++ Memory Management Question Datesfat Chicks <datesfat.chicks@gmail.com> - 2012-01-02 16:51 -0500
  Re: C++ Memory Management Question Simon Quinn <sq@nospam.com> - 2012-01-05 07:54 +0000
  Re: C++ Memory Management Question darylew@gmail.com - 2012-01-10 10:19 -0800

csiph-web