0

I'm currently in the second sequence of a C++ course. I'm building my own string class using c-strings & dynamic memory.

I have a majority of my code working. I'm reading in a file and putting each word in a vector of my class type "ATString". We are supposed to combine 5 of the words read in, into a jumbo word, and putting that into another vector. When I use the debugger to step through my code, I see the words combining and it is copying the words over to the new ATString variable "tempJumbo". After a few lines run, the program crashes and tells me my program has triggered a breakpoint, leaving me on line 96 of the addition operator function.

Here the operator+ definition:

ATString ATString::operator+ (ATString& string1) {
   int newSize = length() + string1.length();
   ATString newString;
   if (newSize > 20) { // increase capacity if size is greater than 20 characters
       newString.increase_cap();
   }
   else {
       cap = 20;
   }

   newString.strPtr = new char[newString.cap];
   newString.end = newSize;
   for (int i = 0; i < length(); i++) {
       newString[i] = strPtr[i];

       for (int j = length(); j < newSize; j++) {

           newString[j] = string1.strPtr[j-end];
       }   
return newString;
   } 

And here is main, reading in the file and attempting to combine the words into a jumbo word.

int main() {


   vector<ATString> words(100);
   vector<ATString> lines(100);        // calls default constructor 100 times    
   ifstream fin("infile3.txt");

   int index = 0;
   int wordCount = 0;


   //READ
   if (fin.fail()) {
       cout << "Couldn't open infile2.txt" << endl;
       system("pause");
       exit(1);
   }
   while (!fin.eof()) {
       fin >> words[index];
       index++;
   }
   wordCount = index;
   words.resize(wordCount);

   //COMBINE 5 WORDS INTO ONE JUMBO
   ATString tempJumbo;
   int j = 0;
   for (int i = 0;i < wordCount; i++) {
       tempJumbo = words[i] + words[i+1] + words[i+2] + words[i+3] + words[i+4];
       lines[j] = tempJumbo; // putting big word into lines vector
       tempJumbo = " ";  //resetting variable to hold jumbo word?
       i = i + 4;
       j++;
       if (i == wordCount) {
           break;
       }
   }

return 0;
  }

I am also have issues with the destructor I wrote.. it's pretty simple and when I have this activated, it triggers an error as well and crashes the program. Not sure what is going here.

ATString::~ATString() {  
   delete strPtr;
}

Below is my header file:


#ifndef ATSTRING_H
#define ATSTRING_H
#include <istream>
using namespace std;

class ATString {
public: 
   ATString();// default constructor
   ATString(const char* cstr); // cstring constructor
   ATString(const ATString& argstr);  // copy constructor
   ~ATString();  // destructor 
   ATString& operator = (const ATString& objToCopy); // assignment operator =
   ATString operator + (ATString& string1); // addition operator +

   int length() const;
   int capacity() const;
   void increase_cap();
   char& operator [] (int index); // indexing operator
   const char& operator [] (int index) const; // const indexing operator
   bool operator <(const ATString& argstr) ;
   bool operator > (const ATString& argstr) ;
   bool operator ==(const ATString& argstr);

   friend istream& operator >> (istream& inStrm, ATString& argstr); // extraction operator
   friend const ostream& operator << (ostream& outStrm, const ATString& argstr); // insertion opertator


private:
   char* strPtr;
   int end;
   int cap;
   int compareTo(const ATString& argStr);
};

#endif

THANK YOU!

nineduey
  • 9
  • 1
  • 1
    1.) `if (newSize > 20) { // increase capacity if size is greater than 20 characters newString.increase_cap(); }` Is weird. What does `increase_cap` do? What does it increase to what size? And why the magic number 20? – churill May 12 '20 at 07:02
  • [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – molbdnilo May 12 '20 at 07:04
  • 2.) Crashes in the destructor might be caused by double deletion and hint at a wrong implementation of the [rule of 5/3/0](https://en.cppreference.com/w/cpp/language/rule_of_three) – churill May 12 '20 at 07:04
  • 2
    I don't see where you are returning anything in `operator +`. You should post the entire function if this is not the entire function -- missing `return` statements are huge bugs. – PaulMcKenzie May 12 '20 at 07:05
  • Feels to me that the major error is probably in the assignment operator, but you haven't shown that. This is a case where you could definitely help us help you by producing an [mcve] – john May 12 '20 at 07:06
  • How does `increase_cap` know what the needed capacity is? Also, you're setting `this->cap` to 20 if the concatenation is longer, and you're copying `string1` `this->length()` times. – molbdnilo May 12 '20 at 07:09
  • Hi, thanks all for the input, the increase_cap will increase the capacity of the char array if the elements exceed 20 characters, and should be increased on my 20. The number 20 was chosen by my teacher. – nineduey May 12 '20 at 07:24
  • And you're right I did forget to include the "return newString" at the ending of the function, which it IS there in the program, but missed it to include here. – nineduey May 12 '20 at 07:26
  • I don't think the number of elements in the vector is the issue, as there are only 76 words in the input file and it reads all of the input files into the "words" vector just fine. I do think there is something in the assignment function that is triggering the crash.. my codes reads maybe 4 to 5 jumbo words into the vector "lines" but then crashes. – nineduey May 12 '20 at 07:28
  • 1:) But to _what_ amount `increase_cap` increase the size? If it's only a fixed amount you're sure going out of bounds when the string get's longer than this fixed number. 2.) `words.resize(wordCount);` probably does not what you think it does. After this `words` will have exactly `wordCount` elements. In `for (int i = 0;i < wordCount; i++)` the counter `i` can go up to `wordCount - 1`. Then `words[i+1]` equals `words[wordCount]` and this looks like out of bounds to me. – churill May 12 '20 at 07:33
  • @AmandaT You should increase the capacity by at least 1.5 times the current capacity, not a fixed size of 20. That's how most string classes accomplish this. Also, you are returning `ATLString` by value -- you have to show at the very least the copy constructor and assignment operator. If those are broken, then `operator +` becomes a moot point since it is guaranteed to fail. – PaulMcKenzie May 12 '20 at 07:46
  • Hi I did find the issues and the for loop was going out of bounds, I had to adjust `for (int i = 0; i < (wordCount - 5); i++)`Thank you guys very much for the feedback. – nineduey May 12 '20 at 19:47

1 Answers1

1

How big is your input file? If it is 100 words or more, then your index runs out of bounds on the line, when i = 96, i.e. trying to get words[100] element.

tempJumbo = words[i] + words[i+1] + words[i+2] + words[i+3] + words[i+4];

dgrandm
  • 319
  • 2
  • 9
  • 1
    Good catch! I'd say it always goes out of bounds, since `words` is resized to have only as many elements as were read from the file. – churill May 12 '20 at 07:07