Application keeps receiving segmentation fault - except if I add a for loop?

There are some extreme corner cases in which delete this actually does the right thing. This is not one of them. Specially since your Queue is placed in the stack, and you further try to delete it.

If you intend to call the destructor instead do this->~Queue() however after a manual destruction the only sensible thing to do next is a placement new . Assigning to this is almost always a bad idea (if you bring inheritance into the picture, you have just caused a slice object to be created and more problems ahead the road). Also, your class should be implementing a copy constructor and an assignment operator, to correctly handle the resources allocated.

Up vote 0 down vote favorite share g+ share fb share tw.

Hi everyone: Here I have created a queue from two stacks: You add to the one and remove from the other - when you want to remove the first stack dumps all its data into the second one, and it works perfectly - BUT whenever I try to execute this loop without the bottom for loop or cin the program receives a segmentation fault, I mean the most bottom for loop doesn't even execute but take it out and see what happens. Could this be some sort of buffer overflow and Gcc needs time to manage the memory? ===================================================================== struct Node { int DataMember; Node* Next; }; class Que { public: Que(); ~Que(); void Add(int); void Pop(); int getSize(); void Purge(); private: Node* Head; bool StackOrQue; //True = Que False = Stack int Size; int Remove(); void Reverse(); }; void Que::Purge() { while(Head!

= NULL) Pop(); if(StackOrQue) StackOrQue = false; } int Que::getSize() { return Size; } Que::Que() { Head = NULL; Size = 0; StackOrQue = false; } Que::~Que() { Head = NULL; } void Que::Add(int q) { if(StackOrQue) Reverse(); Size += 1; Node* Temp = new Node; Temp->DataMember = q; Temp->Next = Head; Head = Temp; } int Que::Remove() { int I = Head->DataMember; Node* Temp = Head->Next; delete Head; Size -= 1; Head = Temp; return i; } void Que::Pop() { if(!StackOrQue) Reverse(); cout Remove()); delete this; *this = TempStack; if(!StackOrQue) StackOrQue = true; else StackOrQue = false; } ===================================================================== Que q; char a = NULL; while(a! = 'x') { q.Purge(); q. Add(1); q.

Add(2); q. Add(3); q. Add(4); q.

Add(5); q. Add(6); q. Add(7); q.

Add(8); int size = q.getSize(); for(int I = 0; I > a; for(int I = 0; I.

1 Show the code of what Add(), Pop() member functions are doing. – Mahesh Oct 13 '11 at 23:35 1 "Gcc needs time to manage the memory" -- No, that's not the problem. Most likely you have invoked undefined behavior in your implementation of Que, and in that case even the slightest difference in the rest of the code can make the difference between a segmentation fault, or appearing to work.

If you learn how to run your program under the debugger (gdb), you can see exactly where it's failing. – Jim Lewis Oct 13 '11 at 23:40 Did you mean queue? – K-ballo Oct 13 '11 at 23:43.

Delete this; *this = TempStack; There are some extreme corner cases in which delete this; actually does the right thing. This is not one of them. Specially since your Queue is placed in the stack, and you further try to delete it.

If you intend to call the destructor instead do this->~Queue(), however after a manual destruction the only sensible thing to do next is a placement new. Assigning to *this is almost always a bad idea (if you bring inheritance into the picture, you have just caused a slice object to be created and more problems ahead the road). Also, your class should be implementing a copy constructor and an assignment operator, to correctly handle the resources allocated.

Thank You k-ballo that makes sense. I shall heed your warning – Aiden Strydom Oct 13 '11 at 23:53 Just to let you know K-Ballo you were right calling the destructor directly made all the difference. Ps I though using delete would automatically call the object's constructor But thanks - its 2am here and now I can finally go to sleep – Aiden Strydom Oct 13 '11 at 23:56.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions