2016-08-10 1 views
-2

Mon programme semble lancer une exception de runtime au sujet d'un tas corrompu lorsque la méthode principale revient. J'ai pris les précautions nécessaires pour que cela n'arrive pas, y compris un constructeur de copie. Quelqu'un pourrait-il nous éclairer sur ce qui se passe?C++ corrompu Heap

MyString.cpp

#include "MyString.h" 
#include <cstdio> 
#include <Windows.h> 

MyString::MyString() { 
    str = (char*)malloc(sizeof(char)); 
    *str = '\0'; 
} 

MyString::MyString(char* src) { 
    int size = sizeof(char)*(strlen(src) + 1); 
    str = (char*)malloc(size); 
    strcpy_s(str, size, src); 
} 


MyString MyString::operator+(char* add) { 
    int addSize = sizeof(char)*strlen(add); 
    int fullSize = sizeof(char)*(strlen(str) + 1) + addSize; 
    str = (char*)realloc(str, fullSize); 
    char* temp = str; 
    temp += strlen(str); 
    strcpy_s(temp, addSize + 1, add); 
    return *this; 
} 

MyString::~MyString() { 
    if (str) 
     free(str); 
} 

MyString::MyString(const MyString &arg) { 
    int size = sizeof(char) * (strlen(arg.str) + 1); 
    str = (char*)malloc(size); 
    strcpy_s(str, size, arg.str); 
} 

main.cpp

#include <iostream> 
#include "MyString.h" 
using namespace std; 


int main(int argc, char *argv[]) { 
    MyString test = MyString("hello!"); 
    test = test + " world"; 
    cout << test.toString() << endl; 
    cout << strlen(test.toString()) << endl; 
    system("pause"); 
    return 0; //runtime error here 
} 
+2

Où est définie toString – rscarson

+0

Que contient «MyString.h»? – kkm

+1

Je voudrais encourager à utiliser 'new' et' delete' au lieu de 'malloc' et' free'. – grigor

Répondre

0

Je fixer mon post suivant @ user4581301 suggestion:

Vous sholud modifier votre surcharge de l'opérateur d'addition de sorte qu'il engendre un nouvel objet et également mettre en œuvre la surcharge de l'opérateur de l'assigment comme ceci:

MyString operator+(char* add) const { 
    int thisSize = sizeof(char)*strlen(str); 
    int addSize = sizeof(char)*(strlen(add) + 1); 
    int fullSize = thisSize + addSize; 

    char* tempStr = (char*)malloc(fullSize); 
    strcpy_s(tempStr, fullSize, str); 
    strcpy_s(tempStr + thisSize, fullSize, add); 

    return MyString(tempStr); 
} 

MyString& operator=(const MyString& assign){ 

    int assignSize = sizeof(char)*(strlen(assign.str) + 1); 

    str = (char*)realloc(str, assignSize); 

    strcpy_s(str, assignSize, assign.str); 

    return *this; 
} 
+0

Hors sujet: Il y a une très bonne astuce qui utilise '+ =' pour implémenter '+' couvert ici: http://stackoverflow.com/questions/4421706/operator-overloading/4421719#4421719 – user4581301

0

Vous devez apprendre au sujet Rule Of Three

opérateur Implicit assigment est utilisé et lorsque l'ancien objet est détruit nouveau utilise déjà libéré le pointeur et plus tard, il essaie de le libérer à nouveau.

+0

Très haute probabilité que vous ayez raison . 'test = test +" world ";' peut être fatal, mais nous devons voir l'implémentation 'operator =', s'il y en a, pour être sûr. Le constructeur de la copie et le destructeur semblent conformes à la règle de trois et nous n'avons pas vu l'opérateur d'affectation. Jusque-là, le mieux que nous puissions faire est de deviner. – user4581301