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[0]), followed by a write to the element number 1 (_s[0] = 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.
none of the things you mentioned are causing problems with my code
Huh? the first four were actual runtime errors, each of which was fatal under memory profiler (which is why I missed the stray delete in the copy ctor, it's never called. Once called, it crashed too).
The next batch were compiler diagnostics, I had to fix some of them to even get your code to compile.
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.
Your still having the issues? You've fixed the problems already stated? Ok, so where exactly is your program crashing, run the code over and over and give us the exact line in the code it breaks.
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[0] 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:
STRING::operator+=(char) [test.cc:311]
}
cout << c << endl;
=> temp[i] = c;
delete[] _s;
_s = temp;
main [test.cc:422]
__start [test.ibm-pure.tsk]
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:
...
STRING::operator+=(char) [test.cc:304]
{
int i = 0;
=> char* temp = new char[_len];
for (; i < _len; i++)
{
temp[i] = _s[i];
main [test.cc:422]
__start [test.ibm-pure.tsk]
I received the grade from my project today, I scored 98%! Thank you all for your help. Cubbi thanks for your patience with me, especially when I was being really dense.