CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2003
    Location
    Pasadena, CA
    Posts
    48

    'exception' question

    Can someone quickly tell me what type of exception is thrown when an STL container is full...?


    imagine...

    int main(void) {
    std::vector<int> myVec;

    // loop only exited via exception (which exception???)
    for ( ; ; ) {
    myVec.push_back(42); // the answer
    }

    return 0;
    }

    what exception is generated???
    The views expressed are those of the author and do not reflect any position taken by the Goverment of the United States of America, National Aeronautics and Space Administration (NASA), Jet Propulsion Laboratory (JPL), or California Institute of Technology (CalTech)

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    There is no specific exception for "container full". In your case, you would probably get bad_alloc when you run out of memory.
    Last edited by Graham; March 3rd, 2004 at 04:30 AM.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    I dont' think there is any size limit for all containers in the STL. Even if the number of reserved element has been exceeded, the container is capable of resizing itself by allocating more memory.

  4. #4
    Join Date
    Nov 2003
    Location
    Pasadena, CA
    Posts
    48
    Originally posted by Kheun
    I dont' think there is any size limit for all containers in the STL. Even if the number of reserved element has been exceeded, the container is capable of resizing itself by allocating more memory.
    I think you missed the infinite loop. Regardless of container resizing - it will run out of resources and at very least a bad_alloc exception will occur.

    Graham is on the right track...

    Graham - assuming the std::allocator is the exception guarenteed to be 'bad_alloc' (this can probably be inferred from the std::allocator spec in the standard) or is that implementation dependent?

    Now down to the tricky part of my question, intrestingly reserve throws length_error if the size request is larger than max_size. If the std::vector<>:: push_back (or any other modifier) uses reserve internally and their has been enough free memory that the new requested size (based on the growth strategy) is both larger than max_size and unsatisifiable by the allocator - do I get a 'bad_alloc', 'length_error' or is it all just implementation dependent.
    The views expressed are those of the author and do not reflect any position taken by the Goverment of the United States of America, National Aeronautics and Space Administration (NASA), Jet Propulsion Laboratory (JPL), or California Institute of Technology (CalTech)

  5. #5
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Originally posted by mclark
    Now down to the tricky part of my question, intrestingly reserve throws length_error if the size request is larger than max_size. If the std::vector<>:: push_back (or any other modifier) uses reserve internally and their has been enough free memory that the new requested size (based on the growth strategy) is both larger than max_size and unsatisifiable by the allocator - do I get a 'bad_alloc', 'length_error' or is it all just implementation dependent.
    I've had a quick look at the standard and I can't find the answer. As you say, it boils down to whether internal reallocation uses reserve(), and, if so, does it check for a possible length_error before it does it? I couldn't find anything covering that. All it says about the allocator is "reserve() uses Allocator::allocate() which may throw an appropriate exception". I did try this out once but, of course, being VC++, it doesn't throw bad_alloc, it returns NULL, so the test was inconclusive, and I didn't have time to try to persuade VC++ to throw the exception like it's supposed to.

    I suppose to some extent, the question may be moot, if max_size() is moderately huge, then you're likely to hit bad_alloc before length_error anyway.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  6. #6
    Join Date
    Feb 2004
    Location
    Montréal, Québec, Canada
    Posts
    49
    Searching on the SGI website, I found a bunch of exceptions in stdexcept. I'm too lazy to check them all (waiting for an infinite loop to run out of ressource is not my idea of a good time), but I have a good feeling on std::runtime_error

    Would be interresting to check though.

    F.
    After three days without programming, life becomes meaningless.
    - The Tao of Programming, book 2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured