new operator and RAM use

This is a discussion on new operator and RAM use within the C++ Programming forums, part of the General Programming Boards category; What happens if theres not enough memory available for your call to new? I know its pretty unlikely but one ...

  1. #1
    alloc(MEM_NEWBIE)
    Guest

    new operator and RAM use

    What happens if theres not enough memory available for your call to new? I know its pretty unlikely but one should always be sure. How can you check (before the call to new) how much RAM is used up by your program and how much is available/free?

  2. #2
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    new throws a bad_alloc if it can't find any memory. You can catch this later, although you are usually screwed, dealing with memory exhaustion other than exiting the program is rather difficult.

    Look up c++ style throw...catch exceptions or get help from someone more willing to elaborate on that subject if you need implementation ideas.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What happens if theres not enough memory available for your call to new?
    Tidy up and flag an error.

    >How can you check (before the call to new) how much RAM is
    >used up by your program and how much is available/free?
    This is system dependent and a real pain in the butt anyway.

    >dealing with memory exhaustion other than exiting the program is rather difficult.
    I'd rather have a difficult task than a ........ed off customer. Imagine that you're working in your favorite text editor, for some reason the program needs more memory and fails. All of the sudden and for no apparent reason you lose hours of work and get to read an oddly worded error message. Will it remain your favorite text editor?

    This isn't the way to write programs, if you encounter an allocation error then you try to recover by allocating smaller blocks. If this fails then you clean up the mess, either give the user the option to save or do it automatically and flag an error. Simply exiting the program at the first sign of a problem is only used in student code where the programs need to be short and will very rarely exceed their bounds.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    381
    >>Imagine that you're working in your favorite text editor, for some reason the program needs more memory and fails. All of the sudden and for no apparent reason you lose hours of work and get to read an oddly worded error message. Will it remain your favorite text editor?

    It might not be thier favorite but they'll probbly keep using it. Look at how many people use ms word.
    If you own a piece of land and there is an volcano on it and it ruins a
    nearby town, do you have to pay for the property damage?

  5. #5
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    I've yet to have a problem with MS Word.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21