I am sorry for the vague title but honestly, I have no idea what's going on. I was making a simple C++ program using CTable - my custom class. CTable contains a pointer to an array of ints, which can be resized and manipulated by methods. Here's a short snippet:
int *table; //pointer to the table
int length; //length of the table
int SetLength(int length); //returns -1 on failure
int SetValueAt(int index, int value); //returns -1 on failure
CTable& operator+=(CTable &other) //combine 2 CTables together
int oldLength = length;
vector<string>* splitString(string sentence, char delim)
vector<string> *res = new vector<string>();
splitString() function looks ok, so it certainly not causes by istelf the kind of behavior you're describing.
operator+=() looks more suspicious. The code itself okgood, but it makes assumptions that
SetLength() changes the length, reallocates the pointer, and copies all the existing values, all without any problem. Note by the way that this code doesn't handle special case such as doing
+= on one self.
Unfortunately, the signature of this function is
int SetLength(int length);. So the name of the parameter hides the name of the member
length, which could cause some serious mismatches that could lead to buffer overflows or unchanged member
length (unless you use
length to make the difference between the two).
Finally, you are using raw pointers instead of smart pointers. So you must ensure the rule of 3. If you don't, you will end-up with shallow copies which could also lead to UB (when one of the object releases the memory that it's copy is still using).