0

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.

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by rdrast
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.