Hi I'm doing a project for school and I'm stuck on this particular function, whenever it runs the test I get this error:

malloc(): memory corruption (fast): 0x0981f018 ***

here is what i have so far:

string& string::operator+=(char ch)
    {
        _size += 1;
        if(_capacity <= _size){
            (*this).increase_capacity(1);
        }
        _data[_size - 1] = ch;
        return (*this);
        //_size += 1;//Check to see if these go before
        //_capacity += 1;; // THIS COULD CAUSE t26 TO FAIL
    }
    
    string string::increase_capacity(int x){
        if(_capacity <= _size){
            x = 1;
            _capacity += x;
        }
        return *this;
    }

I don't know if the increase capacity function is right though, I don't think it is. Thanks in advance.

You are trying to increase the available space in your reserved memory simply by adding a 1 to it. That won't work.
Reserve a larger block of memory (probably with new), copy the old data into it, update your size and capacity values, then delete [] the original block.

Generally, it's typical to increase sizes in blocks larger then 1.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.