Seg fault when deleting queue, only when large

I'm new to the forums so if this should be posted in a different category, let me know.

I have a program that uses a dynamic queue to store some data, and occasionally has to delete this queue and rebuild it. The problem is, if the queue is large when it tries to delete the program will get a segmentation fault, but if it's slightly smaller it works fine.

To debug I basically just reduced the code to a list that pushes a set number of nodes to the back and then deletes. If the number of nodes is >= 130274, the program crashes. If the number of nudes is <= 130273, it works fine. I also tried changing the size of the nodes by adding in a bunch of doubles as data members, but it still crashes at exactly the same number of nodes.

I also ran the .exe on someone else's machine and it did the same thing. However, I did not try compiling on someone else's machine yet.

I'm using Windows 8 with Emacs. There's plenty of processing power and RAM.

Please help!!



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
using namespace std;

class dPoint
{
public:
    dPoint();
    ~dPoint();
    dPoint* write();
    
private:
    dPoint* next;
};

//-----------------------------------------------------------------

int main()
{
    int i, i_max = 130274;
    dPoint *dWrite = new dPoint, *dRead = dWrite;
    for (i = 0; i < i_max; i++)
    {
	dWrite = dWrite->write();
    }
    cout << "here" << endl;
    delete dRead;
    cout << "done" << endl;
}

//------------------------------------------------------------------


dPoint::dPoint()
{
    next = NULL;
}

dPoint::~dPoint()
{
    if (next != NULL) delete next;
}

dPoint* dPoint::write()
{
    next = new dPoint;
    return next;
}
Last edited on
Your recursive destructor exhausts the default stack depth on your platform. (it works just fine on Linux with 8MB stacks, but overflows if I shrink them to 1MB)
Oh I didn't even think of that! How would I fix this?

EDIT: I wrote a non-member helper function to iterate through the queue and delete each node, using the default destructor. Works perfectly!
Thanks Cubbi!
Last edited on
Topic archived. No new replies allowed.