C++ 在ConsoleApplication5.exe中的0x0F640E09(基于UCRTBASE.dll)处引发异常:0xC0000005:访问冲突写入位置0x014C3000?
我刚刚编译了这段代码,它向我显示了以下错误:C++ 在ConsoleApplication5.exe中的0x0F640E09(基于UCRTBASE.dll)处引发异常:0xC0000005:访问冲突写入位置0x014C3000?,c++,arrays,pointers,dynamic-memory-allocation,c-strings,C++,Arrays,Pointers,Dynamic Memory Allocation,C Strings,我刚刚编译了这段代码,它向我显示了以下错误: 在ConsoleApplication5.exe中的0x0F640E09(基于UCRTBASE.dll)处引发异常:0xC0000005:访问冲突写入位置0x014C3000。我真的不知道这个错误意味着什么,因为我刚刚使用C++几个月,我也尝试过任何其他网站寻求帮助,但我没有找到任何。 对于这段代码,我只允许使用c字符串函数和库。我无法使用字符串对象或包含库。我还可以使用助手方法/函数 #include <iostream> #inclu
在ConsoleApplication5.exe中的0x0F640E09(基于UCRTBASE.dll)处引发异常:0xC0000005:访问冲突写入位置0x014C3000。我真的不知道这个错误意味着什么,因为我刚刚使用C++几个月,我也尝试过任何其他网站寻求帮助,但我没有找到任何。 对于这段代码,我只允许使用c字符串函数和
库。我无法使用字符串对象或包含库。我还可以使用助手方法/函数
#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;
}
#包括
#包括
#包括
使用名称空间std;
类MyString{
公众:
//默认构造函数
MyString();
MyString(char*chars);
//复制构造函数
MyString(constmystring&);
int length()常量;
//析构函数
~MyString();
//运算符重载
字符和运算符[](整数索引);
friend MyString操作符+(const MyString和newWord,const MyString和newWord2);
MyString和operator+=(const MyString和newWord);
friend ostream&operator(istream&newWord、MyString&newWord2);
friend bool运算符==(const MyString和newWord,const MyString和newWord2);
friend bool操作符!=(const MyString和newWord,const MyString和newWord2);
friend bool运算符=(const MyString和newWord,const MyString和newWord2);
私人:
字符*值;
整数大小;
};
//默认构造函数
MyString::MyString(){
数值=0;
尺寸=0;
}
//复制构造函数
MyString::MyString(constmystring和newWord){
//执行深度复制以将每个值复制到新内存中
size=newWord.size;
值=新字符[大小];
用于(int ii=0;iiptr;
newWord2=MyString(ptr);
删除ptr;
返回新词;
}
//所有布尔运算符
布尔运算符==(常量MyString和newWord,常量MyString和newWord2){
if(newWord.value==newWord2.value){
返回true;
}
否则{
返回false;
}
}
接线员=(const MyString和newWord,const MyString和newWord2){
if(newWord.value!=newWord2.value){
返回true;
}
否则{
返回false;
}
}
布尔运算符=(常量MyString和newWord,常量MyString和newWord2){
如果(newWord.value>=newWord2.value){
返回true;
}
否则{
返回false;
}
}
//释放内存的析构函数
MyString::~MyString(){
删除[]值;
}
无效测试、复制和析构函数(MyString S){
cout您的错误很可能发生在以下代码上:
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:您的运算符+
正在写入已分配的内存(缓冲区溢出)。您可以轻松地将其更改为非常简单的:
MyString operator +(const MyString& newWord, const MyString& newWord2) {
MyString concatenated;
return concatenated += newWord2;
}
然后它甚至不需要是该类的朋友
另外,运算符+=
是错误的,因为字符串不是以空结尾创建的,所以实际上根本不应该使用strlen()
、strcpy()
和strcat()
,因为这样会将任意内存连接在一起(读取过去分配的内存也可能会出错)。因此,您应该考虑是否希望字符串以NULL结尾(并以此方式使用)
无论如何,运算符+=
的定义也不是很有效,更干净、更有效的可能是,例如:
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;
}
请参阅?不使用遍历字符串的strlen()
(在您的示例中不是以null结尾),因为您知道字符串的大小。您也可以在构造函数中使用memcpy()
,而不是(int i=0;i
循环。或者strcpy()
并使字符串以null结尾(但即使这样,也可以使用memcpy()
,因为它速度更快-不会在复制的每个字符上测试'\0'
)
运算符=
,!=
似乎也不太正确,因为只比较指针(因此字符串将只等于自身,而不等于具有存储在值中相同字符的其他字符串)
此外,由于初始字符串不是以null结尾的,运算符代码还有其他问题,大多数问题都与比较运算符有关
您的运算符strlen(chars)
不够长,无法容纳整个c字符串。此代码优于。您仍然在比较指针而不是值,并且运算符+仍然误用strcat。您的MyString
赋值运算符(运算符=
)在哪里?您的<
#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&)