I have a STRING class I have created that does more or less what you would expect a class called "STRING" to do. I have implemented operator+ in terms of my constructor and operator +=. About 1/8th of the time the program crashes, the rest of the time its fine. This suggest to me its a bad pointer but I haven't a clue where the problem is. If someone could help me out I would really appreciate it.
Some of the functions I haven't yet implemented, they are commented out in my implementation.
Here is my operators plus of type STRING + STRING function. This is the function giving my problems. I have looked to my operator= functions and operator+= functions for errors but have not been able to find any.
memory access errors:
1. in operator=: zero-length array allocation (new char), followed by a write to the element number 1 (_s = c;)
2. in operator+=: allocation for _len + s._len - 1, but the loop writes at the index i+j, which reaches _len + s._len - 1.
3. In the second operator+=, same story (allocation for _len + charLen - 1, accessing temp[_len + charLen - 1]
4. in the third operator+=, allocation for _len, but the last write (temp[i] = c, outside the loop) writes to temp[_len]
besides those errors, I see some compiler diagnostics:
1. comparison between signed and unsigned in 11 places
2. exit() called without a header file
3. operator<< lacks a return statement
and many style issues:
1. "using namespace std;" in a header file
2. binary operators implemented as member functions (even though they correctly call compound assignment operators internally)
3. constructors don't use member initializers
4. toupper/tolower replacements use magic numbers
5. other library code is reimplemented needlessly
PS: totally missed the delete _s; in the copy ctor
1. That is strange yeah. I guess I could have just dynamically alocated a single character and set my pointer to it.
2. That is just a quirk in the implementation. The _len is the number of characters in the string, the first element is "1" not "0" so conversions can be odd at times. Not really related to my problem.
3. You have to create a temporary pointer to memory for that function. There isn't another way to do it.
4. I don't know whats strange about this. Temp = the contents of the object. The last character equals the content of the passed character. Again, there isn't a way to do this function without a temporary pointer.
1. I could be more careful about that.
2. My instructor is to blame for this one :p.
3. Thanks for point that out! Probably would have lost points on that. Fixed.
1. You can blame my instructor for this as well!
2. I don't know what you mean about this. Please elaborate.
3. I should do that. Need to brush up on Myers.
4. Again my instructor! I can't increment or decrement by a constant, but I can use unary operators with a constant. Strange specification but I have to follow it.
5. The purpose of this project is to re implement standard libraries.
PS: Please elaborate. As far as I know, dynamic memory is very neccessary in the copy constructor.
Cubbi, while I do appreciate your comments on my practice, suggestions for better alternatives would be very helpful.
Also, none of the things you mentioned are causing problems with my code, at least as far as I know. I would appreciate help finding exactly what is causing my errors. Thank you.
I'm sorry I missunderstood you. I am going to take a close look at my code. I am not getting runtime errors with these, and my code never had trouble compiling, which has me confused. We are using a dated IDE though. Again, thank you.
Would you mind explaining why some of these run time errors are occurring? I am not getting them in my testing (at least not yet) and I don't understand conceptually why they are causing problems. Thank you.
Would you mind explaining why some of these run time errors are occurring
Because dereferencing a pointer that's not pointing at an object is an error.
Take the last operator+= for example:
char* temp = new char[_len];
This created an array of _len char. its elements may be accessed as temp to temp[_len-1], as you do in the following loop:
1 2 3 4
for (; i < _len; i++)
temp[i] = _s[i];
Now the loop ended because i == _len, and there you have
temp[i] = c;
There is no "temp[_len]", it doesn't exist.
Here's what I see:
ABW: Array bounds write
This is occurring while in:
cout << c << endl;
=> temp[i] = c;
_s = temp;
Writing 1 byte to 0x30320f80 in the heap.
Address 0x30320f80 is 1 byte past end of a malloc'd block at 0x30320f78 of 8 bytes.
This block was allocated from:
int i = 0;
=> char* temp = new char[_len];
for (; i < _len; i++)
temp[i] = _s[i];