Memory Crash on g++ 4.7.2

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Memory Crash on g++ 4.7.2

Palmer Eldritch
This post was updated on .
When I run the code below I get :

size of List c++ : 16
out of memory - created 87927191 nodes
Fast delete
Fast traverse

Then the program crashes :

[Window Title]
smaragd_hw3.exe

[Main Instruction]
smaragd_hw3.exe has stopped working

[Content]
Windows can check online for a solution to the problem.

[V] View problem details  [Check online for a solution and close the program] [Close the program]

I am on windows pro x64 fully up to date

If I replace the line :

for (;; ++var) {

with ;

for (;var < some_not_too_big_num; ++var) {

program terminates normally.

See : http://ideone.com/qBInCR#

I HAVE 6 GB of ram - THIS MAY BE RELEVANT or not

If you run the program with a command line arg (whatever) it won't crash - run it with no args so it deletes only 1/10000 of the objects created initially

The crash is when the function traverse() returns.

I REPEAT : you need to create a lot of nodes to experience the crash

I run the program in eclipse - build :

16:20:30 **** Incremental Build of configuration Debug for project smaragd_hw3 ****
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o List.o "..\\List.cpp"
g++ -o smaragd_hw3.exe List.o

16:20:30 Build Finished (took 480ms)



The code (see also http://ideone.com/qBInCR#)


#ifndef LIST_H_
#define LIST_H_
 
class List {
    long j;
public:
        List * next;
        List * previous;
        virtual long jj() {
                return this->j;
        }
        List() {
                next = previous = 0;
                j = 0; // it still initializes it to 0 - mostly :)
        }
        virtual ~List() {
                if (next) {
                        next->previous = this->previous;
                }
                if (previous) {
                        previous->next = this->next;
                }
        }
};
 
#include <time.h>
#include<iostream>
#include <exception>
 
using namespace std;
 
const static int EVERY = 10000;
 
void print(List * begin) {
        List * biter = begin;
        while (biter != 0) {
                cout << biter << " : " << biter->previous << "," << biter->next << endl;
                biter = biter->next;
        }
}
void deleteNodes(bool slow, List* iter) {
        iter = iter->next; // do not delete the beginning
        for (unsigned int var2 = 0; iter; ++var2) {
                if ((bool) (var2 % EVERY) == slow) {
                        List* temp = iter->next;
                        delete iter;
                        iter = temp;
                } else
                        iter = iter->next;
        }
}
 
void traverse(List* iter, List* const BEGIN) {
        iter = BEGIN;
        long val = 0;
        for (List *iter2 = BEGIN; iter2; iter2 = iter2->next) {
                if (++val > 87910000) {
//                      print(iter);
                        cout << val << endl;
                        cout << iter2 << endl;
//                      return;
                }
        }
}
 
int main(int argc, char **argv) {
        cout << "size of List c++ : " << sizeof(List) << endl;
        clock_t tStart = clock();
        List * ptrList = new List();
        List * const BEGIN = ptrList;
        unsigned long var = 0;
        try {
                for (;; ++var) {
//                      List * ptrList2 = new List();
                        List * ptrList2 = new (nothrow) List();
                        if (!ptrList2) {
                                cout << "out of memory - created " << var << " nodes" << endl;
                                break;
                        }
                        ptrList->next = ptrList2;
                        ptrList2->previous = ptrList;
                        ptrList = ptrList2;
                }
        } catch (bad_alloc const& e) {
                // there is an exception I can't catch for the life of me
                // tht's another bug : see http://stackoverflow.com/questions/13851244/cant-catch-bad-alloc-in-mingw
                cout << "caught : " << e.what() << endl;
//      } catch (...) { //this won't work either
        }
        //print(BEGIN);
        if (argc < 2) {
                List * iter = BEGIN;
                cout << "Fast delete" << endl;
                deleteNodes(false, iter);
                //print(BEGIN);
                cout << "Fast traverse" << endl;
                traverse(iter, BEGIN);
        } else {
                cout << "slow" << endl;
                List * iter = BEGIN;
                cout << "Slow delete" << endl;
                deleteNodes(true, iter);
                print(BEGIN);
                cout << "Slow traverse" << endl;
                traverse(iter, BEGIN);
        }
        cout << "Time taken: " << (double) (clock() - tStart) / CLOCKS_PER_SEC
                        << endl;
 
        return 0;
}


Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Memory Crash on g++ 4.7.2

Palmer Eldritch
This post was updated on .
Abbreviated code :

class List {
public:
        List * next;
        List * previous;
        List() :
                        next(0), previous(0) {
        }
};

#include <time.h>
#include<iostream>

using namespace std;

int main(int argc, char **argv) {
        List * ptrList = new List();
        for (;;) {
                List * ptrList2 = new (nothrow) List();
                if (!ptrList2) {
                        break;
                }
                ptrList->next = ptrList2;
                ptrList2->previous = ptrList;
                ptrList = ptrList2;
        }
        cout << "Time : " << (double) (clock()) << endl; // crashes here (prints Time : )
        // if I remove (double) terminates ok
}