0

For this project we are suppose to cover the topic of dynamic memory allocation and deallocation. The functionality of the 'operator<<; is to output(to terminal or file depending on the type of ostream& os object passed as a parameter to it) the MyString data (the C-string representation held within m_buffer). Right after my program prints to the Terminal "Access Successful." I receive a Segmentation fault (core dumped) I am quite sure that it have something to do with the operator << but I do not know how to fix this. Hence, it fails to print "Testing Operator <<()". Any Suggestions? Thank you!

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

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

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;
};

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

//will allow by-reference accessing of a specific character at index size_tindexwithin the allocated m_bufferchar arrayof a non-constqualified object. 
char& MyString::operator[](size_t index){
size_t counter = 0;
while(counter != index){
    counter++;
}
return m_buffer[counter];
}

//will allow by-reference accessing of a specific character at index size_tindexwithin the allocated m_bufferchar array of a constqualified object.
const char& MyString::operator[](size_t index)const{
size_t counter = 0;
while(counter != index){
    counter++;
}
return m_buffer[counter];
}


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(){
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;

}
  • 1
    Give us the smallest example you can that reproduces the error. We want to be able to run the code ourselves to get the same error to see what's wrong. – David G Apr 01 '20 at 00:17
  • added my operator [], thought it might be useful, since its functionality is used before the testing of operator <<. – aperezparedes Apr 01 '20 at 00:29
  • Thanks but we need an actual program that we can copy and paste into our own compilers to get the same error. I can't run your code because it's incomplete. – David G Apr 01 '20 at 00:37
  • Ok I did this, just added the int main and class declaration/definition. The program is longer and I believe this to be the only pertinent data. Let me know! – aperezparedes Apr 01 '20 at 00:43
  • Can you provide the full declaration/definition of the `MyString` class? – David G Apr 01 '20 at 00:54
  • done, and thanks for the guidance, i appreciate the help. – aperezparedes Apr 01 '20 at 01:00
  • 1
    Silly question: why doesn't `operator[]()` just return `m_buffer[index]`? It would be much simpler. –  Apr 01 '20 at 01:24
  • 1
    You don't allocate enough space for you `m_buffer`. `strlen` returns the length of a c-style string excluding the null terminator. Your `strcpy` ends up writing a null terminator one past the end of `m_buffer`. – JohnFilleau Apr 01 '20 at 01:34
  • Re `operator[]`: you don't need to count from zero to `index` in order to make `counter` the same as `index`, you can assign `counter = index`. In fact, `counter` is also unnecessary. – molbdnilo Apr 01 '20 at 02:58
  • Another unnecessary complication: The expected implementation of `c_str` is `{ return m_buffer; }`. – molbdnilo Apr 01 '20 at 03:03

0 Answers0