简体   繁体   中英

allocating and deallocating dynamic memory seg fault in c++

The main objective of this project is to implement my knowledge of dynamic memory allocation and deallocation. So far, my program compiles and runs, but the only problem I am having is that the compiler states Segmentation fault (core dumped) at the end of the program. I do not what exactly causes this. The int main was provided, so the only has to be with my member definitions, I am thinking it has something to do with my operator overloading, (possibly operator +) but can not figure out how to fix it. Yes, I am also using namespace standard. Any Suggestions? Thank you!

int main(){

//(1)
std::cout << "Testing Default ctor" << std::endl;
MyString ms_default;

//(2)
std::cout << "Testing Parametrized ctor" << std::endl;
MyString ms_parametrized("MyString parametrized constructor!");

//(3)
std::cout << "Testing Copy ctor" << std::endl;
MyString ms_copy(ms_parametrized);

//(4)
std::cout << "Testing dtor" << std::endl;
{
    MyString ms_destroy("MyString to be destroyed...");
}

//(5),(6)
MyString ms_size_length("Size and length test");
std::cout << "Testing size()" << std::endl;
cout << ms_size_length.size() << endl;
std::cout << "Testing length()" << std::endl;
cout <<ms_size_length.length() << endl;

//(7)
std::cout << "Testing c_str()" << std::endl;
MyString ms_toCstring("C-String equivalent successfully obtained!");
cout << ms_toCstring.c_str() << endl;

//(8)
std::cout << "Testing operator==()" << std::endl;
MyString ms_same1("The same"), ms_same2("The same");
if (ms_same1==ms_same2)
    cout << "Same success" << endl;

MyString ms_different("The same (NOT)");
if (!(ms_same1==ms_different))
    cout << "Different success" << endl;

//(9)
std::cout << "Testing operator=()" << std::endl;
MyString ms_assign("Before assignment");
ms_assign = MyString("After performing assignment");

//(10)
std::cout << "Testing operator+" << std::endl;
MyString ms_append1("The first part");
MyString ms_append2(" and the second");
MyString ms_concat = ms_append1+ ms_append2;

//(11)
std::cout << "Testing operator[]()" << std::endl;
MyString ms_access("Access successful (NOT)");
ms_access[17] = 0;

//(12)
std::cout << "Testing operator<<()" << std::endl;
cout << ms_access << endl;

return 0;

}

The following is my implementation or definitions of my class members

void MyString::buffer_deallocate(){
if(m_buffer != NULL){
    delete [] m_buffer;
}
}

void MyString::buffer_allocate(size_t size){
if(m_buffer != NULL){
    buffer_deallocate();
}

m_size = size;
m_buffer = new char[m_size];
}

MyString::MyString(){
m_size = 0;
m_buffer = NULL;
}

MyString::MyString(const char * str){
m_buffer = NULL;
m_size = strlen(str);
buffer_allocate(m_size);
strcpy(m_buffer,str);
}

MyString::MyString(const MyString & other){
m_buffer = NULL;
m_size = other.m_size;
buffer_allocate(m_size);
strcpy(m_buffer,other.m_buffer);
}

size_t MyString::size() const {
return m_size;
}

size_t MyString::length() const{
return strlen(m_buffer) - 1;
}

const char * MyString::c_str() const {
char * str = NULL;

    str = new char[m_size];
    for(size_t i = 0; i < m_size; i++){
        str[i] = *(m_buffer+i);
    }

        return str;
        delete [] str;
        str = NULL;
 }

bool MyString::operator==(const MyString & other) const{
if(strcmp(m_buffer,other.m_buffer)==0){
    return true;
}
else if(strcmp(m_buffer,other.m_buffer)!=0){
    return false;
}
}

MyString & MyString::operator=(const MyString & str1){
buffer_deallocate();
m_buffer = new char[str1.m_size];
strcpy(m_buffer, str1.m_buffer);
}

MyString MyString::operator+(const MyString & other_myStr) const {
MyString myStr(strcat(m_buffer, other_myStr.m_buffer));
return myStr;
}

char & MyString::operator[](size_t index){
size_t counter = 0;
while(counter != index){
    counter++;
}
return m_buffer[counter];
}

const char & MyString::operator[](size_t index) const{
size_t counter = 0;
while(counter != index){
    counter++;
}

return m_buffer[counter];
}

std::ostream & operator<<(std::ostream & os, const MyString & myStr){
//if(&os == &std::cout){
    os << myStr.m_buffer << std::endl;
//}
}

MyString::~MyString(){
m_size = 0;
delete [] m_buffer;
}

This is my class declaration

class MyString{


public:
    MyString();
    MyString(const char * str);
    MyString(const MyString & other_myStr);
    ~MyString();

    size_t size() const;
    size_t length() const;
    const char * c_str() const;

    bool operator==(const MyString & other_myStr) const;
    MyString & operator=(const MyString & other_myStr);
    MyString operator+(const MyString & other_myStr) const;
    char & operator[](size_t index);
    const char & operator[](size_t index) const;

friend std::ostream & operator<<(std::ostream & os, const MyString & myStr);

private:
    void buffer_deallocate();
    void buffer_allocate(size_t size);

    char * m_buffer;
    size_t m_size;

};
m_size = strlen(str);
buffer_allocate(m_size);
strcpy(m_buffer,str);

strlen tells you how many characters are in the string pointed to by str , not including the nul character at the end. strcpy copies the entire string, including the nul character. So the copy runs off the end of the allocated space.

Further, that operator+ concatenates strings without ensuring that there is enough room for the result. That won't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM