ConsoleApplication5.exe中0x0F640E09(ucrtbased.dll)抛出异常:0xC0000005:访问冲突写入位置0x014C3000?

时间:2016-07-09 20:54:43

标签: c++ arrays pointers dynamic-memory-allocation c-strings

我刚刚编译了这段代码,它向我显示了这个错误:
在ConsoleApplication5.exe中0x0F640E09(ucrtbased.dll)抛出异常:0xC0000005:访问冲突写入位置0x014C3000。我真的不知道这个错误意味着什么,因为我刚刚使用C ++几个月,我也试过寻找任何其他网站寻求帮助,但我没有找到任何。

对于这段代码,我只允许使用c-string函数和<cstring>库。我不能使用字符串对象或包含库。我也可以使用辅助方法/函数。

#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;

class MyString {
public:

    //default constructor
    MyString();

    MyString(char* chars);

    //copy constructor
    MyString(const MyString &);

    int length() const;

    //destructor
    ~MyString();

    //operator overloads
    char& operator[](int index);
    friend MyString operator+(const MyString& newWord, const MyString& newWord2);
    MyString& operator+=(const MyString& newWord);
    friend ostream& operator<<(ostream& newWord, const MyString& newWord2);
    friend istream& operator >> (istream& newWord, MyString& newWord2);
    friend bool operator==(const MyString& newWord, const MyString& newWord2);
    friend bool operator!=(const MyString& newWord, const MyString& newWord2);
    friend bool operator<(const MyString& newWord, const MyString& newWord2);
    friend bool operator<=(const MyString& newWord, const MyString& newWord2);
    friend bool operator>(const MyString& newWord, const MyString& newWord2);
    friend bool operator>=(const MyString& newWord, const MyString& newWord2);

private:
    char* value;
    int size;
};

//default constructor
MyString::MyString() {

    value = 0;
    size = 0;
}

//copy constructor
MyString::MyString(const MyString& newWord) {

    //perform a deep copy to copy each of the value to a new memory
    size = newWord.size;
    value = new char[size];

    for (int ii = 0; ii < size; ii++) {
        value[ii] = newWord.value[ii];
    }
}

//constructor with an argument
MyString::MyString(char* chars) {

    size = strlen(chars);
    value = new char[size];
    for (int i = 0; i < size; i++) {
        value[i] = chars[i];
    }

}

//find length
int MyString::length() const {

    return size;
}

//find the value of each index
char& MyString::operator[](int index) {

    return value[index];
}

//operator + (concatenate)
MyString operator+(const MyString& newWord, const MyString& newWord2) {

    MyString concatenated;
    concatenated = strcat(newWord.value, newWord.value);
    return concatenated;

}

//operator += (append)
MyString& MyString::operator+=(const MyString& newWord) {

    char * newMemory = value;
    value = new char[strlen(value) + newWord.length() + 1];
    strcpy(value, newMemory);
    strcat(value, newWord.value);
    if (size != 0)
    {
        delete[] newMemory;
    }
    size = strlen(value);
    return *this;
}

//ostream operator
ostream& operator<<(ostream& newWord, const MyString& newWord2) {

    newWord << newWord2.value;
    return newWord;
}


//istream operator
istream& operator >> (istream& newWord, MyString& newWord2) {

    const int MAX = 100;
    char* ptr = new char[MAX];
    newWord >> ptr;
    newWord2 = MyString(ptr);
    delete ptr;
    return newWord;
}

//all boolean operators
bool operator==(const MyString& newWord, const MyString& newWord2) {
    if (newWord.value == newWord2.value) {
        return true;
    }
    else {
        return false;
    }
}

bool operator!=(const MyString& newWord, const MyString& newWord2) {
    if (newWord.value != newWord2.value) {
        return true;
    }
    else {
        return false;
    }
}

bool operator<(const MyString& newWord, const MyString& newWord2) {
    if (newWord.value < newWord2.value) {
        return true;
    }
    else {
        return false;
    }
}

bool operator<=(const MyString& newWord, const MyString& newWord2) {
    if (newWord.value <= newWord2.value) {
        return true;
    }
    else {
        return false;
    }
}

bool operator>(const MyString& newWord, const MyString& newWord2) {
    if (newWord.value > newWord2.value) {
        return true;
    }
    else {
        return false;
    }
}

bool operator>=(const MyString& newWord, const MyString& newWord2) {
    if (newWord.value >= newWord2.value) {
        return true;
    }
    else {
        return false;
    }
}

//destructor to release memory
MyString::~MyString() {
    delete[] value;
}

void test_copy_and_destructor(MyString S) {
    cout << "test: copy constructor and destructor calls: " << endl;
    MyString temp = S;
    cout << "temp inside function test_copy_and_destructor: " << temp << endl;
}

int main() {

    MyString st1("abc abc");
    MyString st2("9fgth");

    cout << "Copy constructor , << operator" << endl;

    MyString  st3(st1);

    cout << "st3: " << st3 << endl;

    test_copy_and_destructor(st2);

    MyString  st4;

    cout << "operator + " << endl;

    st4 = st3 + st2;

    cout << "st4: " << st4 << endl;

    cout << "st1 + st2: " << (st1 + st2) << endl;

    cout << "operators  [ ] " << endl;

    for (int i = 0; i < st2.length(); i++)
        cout << st2[i] << " ";

    cout << endl;

    cout << "operators  += , ==, != " << endl;

    st2 += st1;

    if (st3 == st1)
        cout << "st3 and st1 are identical " << endl;
    else cout << "st3 and st1 are not identical " << endl;

    if (st2 != st1)
        cout << "st2 and st1 are not identical " << endl;
    else cout << "st2 and st1 are identical " << endl;

    cout << "operators  < , <=, >, >= " << endl;

    if (st2 < st1)
        cout << "st2 < st1 " << endl;
    else cout << "st2 is not less than st1 " << endl;

    if (st1 <= st2)
        cout << "st1 <= st2 " << endl;
    else cout << "st1 is not less than or equal to st2 " << endl;

    if (st1 > st2)
        cout << "st1 > st2 " << endl;
    else cout << "not (st1 >  st2) " << endl;

    if (st1 >= st2)
        cout << "st1 >= st2 " << endl;
    else cout << "not (st1 >=  st2) " << endl;

    cout << "operator >> " << endl;

    //Open the data file
    ifstream input("A9_input.txt");
    if (input.fail()) {
        cout << "unable to open input file A9_input.txt, Exiting..... ";
        system("pause");
        return 0;
    }
    MyString temp1;
    MyString temp2("aaa");
    input >> temp1;
    input >> temp2;
    cout << "first element of input file: " << temp1 << endl;
    cout << "second element of input file: " << temp2 << endl;
    input.close();

    cout << "MyString says farewell....." << endl;
    system("pause");
    return 0;
}

3 个答案:

答案 0 :(得分:0)

您最有可能错误的是以下代码:

st4 = st3 + st2; # note that you initialize st4 with type MyString but inside operator+ you assign a char[] to MyString. Just letting you know since you dont have a operator= overloaded function 

调用:

MyString operator+(const MyString& newWord, const MyString& newWord2) {

    MyString concatenated;
    concatenated = strcat(newWord.value, newWord.value); # you have an mistake here, second parameter should be newWord2.value
    return concatenated;

}

您假设您的newWord.value拥有足够的空间来容纳newWord.value和newWord2.value。但是你不是在你的构造函数上将它作为newWord.value而不是因为你基本上写入了一个违​​反内存访问的区域。你想要做的是让newWord足够大以容纳两个字符串。

引用strcat:http://www.cplusplus.com/reference/cstring/strcat/

答案 1 :(得分:0)

您的operator +正在写入已分配的内存(缓冲区溢出)。您可以轻松地将其更改为非常简单:

MyString operator +(const MyString& newWord, const MyString& newWord2) {
    MyString concatenated;
    return concatenated += newWord2;
}

那么它甚至不需要成为班级的friend

operator +=也是错误的,因为你的字符串最初没有被创建NULL,所以你不应该使用strlen()strcpy()和{{1}毕竟,因为你在一起连接任意内存(读取过去分配的内存也可以是段错误)。所以你应该想一想你是否希望字符串NULL终止(并且这样使用)。

strcat()无论如何也没有非常有效地定义,例如,更加干净和有效:

operator +=

请参阅?不使用遍历字符串的MyString& MyString::operator+=(const MyString& newWord) { size_t newSize = size + newWord.size; char * newValue = new char[newSize /* + 1 */]; // not null terminated, but cannot be printed easily then // or make the string initially null terminated and then allocate +1 and null terminate then memcpy(newValue, value, size); memcpy(newValue + size, newWord.value, newWord.size /* + 1 */); delete[] value; value = newValue; size = newSize; return *this; } (在你的情况下不是null),因为你知道字符串的大小。您还可以在构造函数中使用strlen()而不是memcpy()循环。或者for (int i = 0; i < size; i++)并使字符串以空值终止(但即使这样strcpy()也可以使用,因为它更快 - 不会在复制的每个字符上测试memcpy()

运算符'\0'==似乎也不正确,因为只比较指针(因此字符串只会等于自身,但不会与具有相同字符的其他字符串相等)存储在!=)。

此外,由于初始字符串不是空终止,value也无法正常工作(将打印出超出字符串的任意内存)。一般来说,我建议将字符串operator <<始终以null结尾以避免一些问题。

最后,你肯定需要定义value,因为现在你使用的是默认的一个浅拷贝,所以使用这个代码你将在operator =的破坏时获得双重免费(以及分配有st4的任何其他字符串)。请注意,正确实现=也很棘手 - 请注意自我分配方案。

答案 2 :(得分:0)

您的代码还存在其他问题,大多数问题涉及您的比较运算符。

operator <operator== MyString不正确。要比较c风格的字符串,请使用strcmp,而不是==。而比较运算符则是比较指针值,而不是指向的数据。

第二个问题是,您需要做的就是完全实现operator <operator ==,因为所有其他比较运算符都可以根据这些函数实现。

将所有这些放在一起,你的类的实现应该如下所示:

#include <cstring>
//...     
class MyString {
public:
    //...    
    friend bool operator==(const MyString& newWord, const MyString& newWord2);
    friend bool operator!=(const MyString& newWord, const MyString& newWord2);
    friend bool operator<(const MyString& newWord, const MyString& newWord2);
    friend bool operator<=(const MyString& newWord, const MyString& newWord2);
    friend bool operator>(const MyString& newWord, const MyString& newWord2);
    friend bool operator>=(const MyString& newWord, const MyString& newWord2);

private:
    char* value;
    int size;
};

bool operator==(const MyString& newWord, const MyString& newWord2) 
{ return strcmp(newWord.value, newWord2.value) == 0; } 

bool operator<(const MyString& newWord, const MyString& newWord2) 
{ return strcmp(newWord.value, newWord2.value) == -1; }

bool operator!=(const MyString& newWord, const MyString& newWord2)
{ return !(newWord == newWord2); }

bool operator<=(const MyString& newWord, const MyString& newWord2)
{ return !(newWord2 < newWord); }   

bool operator> (const MyString& newWord, const MyString& newWord2)
{ return newWord2 < newWord; }

bool operator>=(const MyString& newWord, const MyString& newWord2)
{ return !(newWord < newWord2); }

请注意,运营商使用<==来实施其他运营商。

第三个问题是你错过了一个赋值算子

MyString& operator=(const MyString&)

如果没有此功能,则不能在不破坏内存的情况下将MyString个对象分配给对方。