The problem is in my assignment operator, I’ve forgotten to deallocate and reallocate memory for my pointer in my string class. I must have accidentally treated it like a copy constructor; This has been a great lesson in why memory management is very important. Thanks everyone for your help.
ive implemented my own string class and this seems to be the last function on the call stack before it breaks.
String::~String(){
delete [] rep;
len=0;
}
Can someone help me understand what the problem is?
Here is the function which calls it
template <class T>
void SList<T>::RemoveAfter(typename SList<T>::Iterator i){
assert(i.nodePointer !=0 && i.nodePointer->next!=0);
Node *save = i.nodePointer -> next;
i.nodePointer->next = i.nodePointer->next->next;
delete save;
}
If there is any more info you need to help me figure out why this occurs let me know.
By the way, if I use a type int, I dont have this problem, so I know the issue has to be with my string class…right?
More info as requested:
struct Node{ // Node: Stores the next Node and the data.
T data;
Node *next;
Node() {next =0;}
Node(const T& a, Node *p = 0){data=a;next=p;}
};
the error:
Windows has triggered a breakpoint in Algorithms.exe.
This may be due to a corruption of the heap, which indicates a bug in Algorithms.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while Algorithms.exe has focus.
The output window may have more diagnostic information.
Example functionality that breaks:
String item1("Example"), item2("Example");
SList<String> list1;
list1.AddFirst(item2);
list1.AddFirst(item1);
list1.AddLast("List Class");
list1.AddLast("Functionality");
SList<String>::Iterator i1;
i1 = list1.Begin();
i1++;
i1++;
list1.RemoveAfter(i1);
Example that works
SList<int> list1;
list1.AddFirst(1);
list1.AddFirst(2);
list1.AddLast(3);
list1.AddLast(4);
SList<int>::Iterator i1;
i1 = list1.Begin();
i1++;
i1++;
list1.RemoveAfter(i1);
system("pause");
More information:
//Default Constructor
String::String(){
rep = new char[1];
rep[0] = '\0';
len = 0;
}
//Constructor - Converts char* to String object
String::String(const char *s){
len=0;
const char *temp = s;
while(*temp){
++len;
++temp;
}//Sets len of rep to the length of s
rep = new char[len + 1];
for(int i=0; i<=len; ++i)
rep[i]=s[i];
}
//Copy Constructor
String::String(const String &obj){
len=0;
char *temp = obj.rep;
while (*temp){
++len;
++temp;
}//Sets len of rep to length of obj.rep
rep = new char[len + 1];
for (int i = 0; i<=len; ++i)
rep[i] = obj.rep[i];
}
//Assignment operator
const String& String::operator=(const String &rhs){
if (this != &rhs){
len=0;
char *temp = rhs.rep;
while(*temp){
++len;
++temp;
}//Sets len of this to length of rhs.rep
for(int i = 0; i<=len;++i)
rep[i]=rhs.rep[i];
}
return *this;
}
In your assignment operator you are potentially writing outside the bounds of the allocated area pointed to by
char* rep, which will probably corrupt the stack (ie. cause undefined-behavior).When you later try to deallocate this segment of memory a corrupt stack might cause things to go banans, and it’s a very plausible this is the reason for the error message you are receiving when running your application.
As stated by the message itself "this may be due to a corruption of the heap".
How do I fix my problem?
Inside your assignment operator you will need to do three things:
Currently you are only doing step 3 & 4.