0

I created a simple StringBuilder class to practice. A similar class is available in other programming languages. When the destructor of this class is called, I try to delete the memory allocated to the string using the [] delete statement. But after executing this statement, I encounter the following error : Error dialog image

My StringBuilder class code : [I deleted some unnecessary functions that I did not call at all (either directly or indirectly).]

#include <string>
namespace std {
    
    class StringBuilder {
    private:
        char* str;
        size_t extraBuffer;
        int index;
        void changeSize() {
            int len = length();
            char* tmpForDelete = str;
            char* newStr = new char[len + extraBuffer];
            for (int i = 0; i < len; i++) newStr[i] = str[i];
            newStr[len] = 0;
            delete[] tmpForDelete;
            tmpForDelete = NULL;
            str = newStr;
        }
        void changeSize(int SizeForAdd) {
            size_t len = length();
            size_t max = getMaxLength();
            if (SizeForAdd + len > extraBuffer + len) 
                setExtraBuffer(SizeForAdd + len);
        }
    public:
        StringBuilder() {
            extraBuffer = 125;
            str = new char[1];
            str[0] = 0;
            changeSize();
            index = length();
        }
        StringBuilder(const char* str) : StringBuilder() {
            int len = strlen(str) + extraBuffer;
            if (!(!str)) delete[]this->str;
            this->str = new char[len];
            for (int i = 0; i <= len; i++) this->str[i] = str[i];
            index = length();
        }
        StringBuilder(char* str) : StringBuilder() {
            int len = strlen(str) + extraBuffer;
            this->str = new char[len];
            for (int i = 0; i <= len; i++) this->str[i] = str[i];
            index = length();
        }
        ~StringBuilder() {
            delete[] str; //error occurs in this line(Heap) why?
            str = NULL;
        }
        size_t getExtraBuffer() { return extraBuffer; }
        size_t length() { return strlen(str); }
        void setExtraBuffer(size_t newSize) {
            if (newSize > extraBuffer && newSize <= getMaxLength()) {
                extraBuffer = newSize;
                changeSize();
            }
        }
        size_t getMaxLength() {
            string str("");
            return str.max_size();
        }
        size_t capacity() {
            return length() + extraBuffer;
        }
    };
}

Main program :

#include <iostream>
#include <conio.h>
#include "StringBuilder.h"
void test(StringBuilder SB) {
    SB.toString();
}
int main(int argc, char* argv[]) {
    StringBuilder SB("1234");
    test(SB);
    _getch();
}

At first I thought the error was due to the fact that when the program ends, the memory is erased and can not be accessed. But to check if my guess was correct, I used the test function. An error occurs when the test function ends and the object destructor is called. However, the program is not finished and after the test function, an error occurs in the part where I used the delete [] statement! After searching the internet, I still did not find any results. Most of the solutions were related to array indexes, which should not be negative. But I have controlled this in my class codes. If possible, please fully explain the reason for this error to me. thanks

trincot
  • 317,000
  • 35
  • 244
  • 286
MRJ
  • 1
  • 1
  • 2
    You class contains a raw pointer and does not follow the rule of three. – Retired Ninja Jan 15 '22 at 00:26
  • Also, at the duplicate "rule of 3" link, read the **Managing resources** section carefully, as your code is the same structure to the code in that section, and suffers from the same issues. *A similar class is available in other programming languages.* -- The same class exists in C++: it's called `std::string`. So your "StringBuilder" basically adds nothing to `std::string`. If anything, maybe have `std::string` as a member, and implement *all* the memory allocation using `std::string`'s internal memory handling. – PaulMcKenzie Jan 15 '22 at 03:06

0 Answers0