On Thu, Dec 13, 2012 at 9:45 PM, Keith Marshall [via MinGW-users] <[hidden email]> wrote:
>
> On 13/12/12 14:24, Palmer Eldritch wrote:
> > I am writing a program for a homework where I need to allocate many objects
> > to check things on locality and performance etc. I can't seem to catch the
> > exception thrown by `new`
> >
> >      #include "List.h"
>
> Since you neglected to show us your List class declaration, I had to
> provide my own.

 
My list declaration **is** in my email ?!?
The List :

    class List {
    long j;//etc
    };

Please read carefully.
 
>
>
> >      #include<iostream>
> >      #include <exception>
> >
> >      int main(int argc, char **argv) {
> >       cout << "size of List c++ : " << sizeof(List) << endl;  //16
> >       List * ptrList = new List();
> >       unsigned long var = 0;
> >       try {
> >       for (;; ++var) {
> >       List * ptrList2 = new  List();
> >       ptrList->next = ptrList2;
> >       ptrList2->previous = ptrList;
> >       ptrList = ptrList2;
>
> Since this is a homework question, I will not show you my solution,
> but if this is indicative of your class design, I would feel very
> generous if I gave you 3/10 for it.  Anyway, after adapting this to my
> own class design, and fixing the bugs which mean that your code should
> not even compile, let alone run to an uncaught exception...

 
My code did compile and run and if you mean having public fields and the like this was by design. And I started breaking things to pin this particular issue
I urge you to see the link in Stack overflow and the comments there - I made clear there are relevant pieces of information there
I allocated around 87921929 nodes (varying from execution to execution) and maybe the "bug" (I was *told* it was probably a bug in SO) has to do with the (rather huge) number of objects created

In SO someone posted a snippet in http://ideone.com/YN6we4

I reproduce the snippet here - if you can spot a bug in my code that might lead to this behavior then the case is closed:

// ******BEGIN FILE : ideone.com.YN6we4.cpp

class List {
    long j;
public:
    List * next;
    List * previous;
    virtual long jj() {
        return this->j;
    }
    List() {
        next = previous = 0;
        j = 0;
    }
    virtual ~List() {
        if (next) {
            next->previous = this->previous;
        }
        if (previous) {
            previous->next = this->next;
        }
    }
};
 
#include <iostream>
#include <exception>
using namespace std;
 
int main(int argc, char **argv) {
    cout << "size of List c++ : " << sizeof(List) << endl;  //16
    List * ptrList = new List();
    unsigned long var = 0;
    try {
        for (;; ++var) {
            List * ptrList2 = new  List();
            ptrList->next = ptrList2;
            ptrList2->previous = ptrList;
            ptrList = ptrList2;
        }
    } catch (bad_alloc const& e) {
        cout << "caught : " << e.what() << endl;
//  } catch (...) { //this won't work either
    }
}

// ******END FILE: ideone.com.YN6we4.cpp

When I compile and run this I get the crash :


C:\Users\MrD\Desktop\mingw bug>g++ ideone.com.YN6we4.cpp

C:\Users\MrD\Desktop\mingw bug>a.exe
size of List c++ : 16

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.


C:\Users\MrD\Desktop\mingw bug>g++ --version
g++ (GCC) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


C:\Users\MrD\Desktop\mingw bug>


Did I say thanks for your time ? :)

Btw feel free to post your solution - the deadline expired and it was a bonus anyway :)

Also
>
>
> >       }
> >       } catch (bad_alloc const& e) {
> >       cout << "caught : " << e.what() << endl;
>
> ...this works perfectly for me, both with mingw32-g++ v4.5.2 on my Vista
> box at work, and with v4.7.2 on my Win7 VM at home; i.e., with the cout
> modified to:
>
>    cout << e.what() << " after allocating " << var << " nodes.\n";
>
> I (eventually) see:
>
>    std::bad_alloc after allocating 1948534 nodes.
>
> (the count may vary depending on the amount of available free heap
> memory, and the relative sizes of our respective List objects, but this
> is exactly as I would expect, when the exception is caught.
>
> >      // } catch (...) { //this won't work either
> >       }
>
> I even enabled this, but it wasn't needed, because the preceding catch
> succeeded; I have no reason to expect that this would not work, if the
> exception hadn't been caught otherwise.
>
> --
> Regards,
> Keith.
>
> ------------------------------------------------------------------------------
> LogMeIn Rescue: Anywhere, Anytime Remote support for IT. Free Trial
> Remotely access PCs and mobile devices and provide instant support
> Improve your efficiency, and focus on delivering more value-add services
> Discover what IT Professionals Know. Rescue delivers
> http://p.sf.net/sfu/logmein_12329d2d
> _______________________________________________
> MinGW-users mailing list
> [hidden email]
>
> This list observes the Etiquette found at
> http://www.mingw.org/Mailing_Lists.
> We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.
>
> _______________________________________________
> You may change your MinGW Account Options or unsubscribe at:
> https://lists.sourceforge.net/lists/listinfo/mingw-users
> Also: mailto:[hidden email]?subject=unsubscribe
>
>
> ________________________________
> If you reply to this email, your message will be added to the discussion below:
> http://mingw-users.1079350.n2.nabble.com/unable-to-catch-bad-alloc-tp7579725p7579730.html
> To unsubscribe from unable to catch bad_alloc, click here.
> NAML



View this message in context: Re: unable to catch bad_alloc
Sent from the MinGW-users mailing list archive at Nabble.com.