gpt4 book ai didi

c++ - 分配动态内存以存储字符串拷贝时程序中断

转载 作者:搜寻专家 更新时间:2023-10-31 01:30:35 25 4
gpt4 key购买 nike

我想看看我的所有功能是否正常工作,程序运行到黑屏并关闭,它给我一个错误,三角形内有黄色三角形感叹号,表示

“在 milestone2.exe 0xC0000005 中抛出异常 0x01971FD0 (ucrtbased.dll):访问冲突读取位置 0x0000000”

指示 .cpp 文件的箭头 m_message = new char [strlen(str) + 1 ] 位于 void ErrorMessage::message(const char* str) at

我猜这是某种内存问题,有人知道解决方案吗?

头文件

#ifndef SICT_ERROR_H_
#define SICT_ERROR_H_
namespace sict {
class ErrorMessage {
char* m_message;
public:

ErrorMessage(const char* errorMessage = nullptr);
ErrorMessage(const ErrorMessage& em) = delete;
ErrorMessage& operator=(const ErrorMessage& em) = delete;
virtual ~ErrorMessage();
void clear();
bool isClear() const;
void message(const char* str);
const char* message() const;
};
std::ostream& operator<<(std::ostream& ostr, ErrorMessage);
}
#endif

Cpp文件

#include "ErrorMessage.h"
#include <iostream>
namespace sict {

ErrorMessage::ErrorMessage(const char* errorMessage) {
m_message = nullptr;
message(errorMessage);
}
void ErrorMessage::clear() {
delete[] m_message;
m_message = nullptr;
}
void ErrorMessage::message(const char* str) {
delete[] m_message;
m_message = new char[strlen(str) + 1];
strcpy(m_message, str);
}
const char* ErrorMessage::message() const {
return m_message;
}
ErrorMessage::~ErrorMessage() {
clear();
}
bool ErrorMessage::isClear() const {
if (m_message == nullptr) {
return true;
}
return false;
}
std::ostream& operator<<(std::ostream& ostr, ErrorMessage error) {
if (!(error.isClear())) {
ostr << error.message();
}
return ostr;
}


}

main.cpp

#include <iostream>
#include "ErrorMessage.h"

using namespace std;
using namespace sict;

int main() {
ErrorMessage T("Testing Error Message Module");
ErrorMessage e;
int ret = 0;
bool ok = true;
cout << T << endl << e << endl << "isClear(): " << (e.isClear() ? "Passed!" : "Failed!") << endl;
if (!e.isClear()) ok = false;
cout << endl;
cout << "===========| Long Message\r";
for (int i = 0; i < 10000000; i++) {
if (i % 1000000 == 0) {
cout << "*";
cout.flush();
}
e.message("Some error message that is really long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long");
}
cout << '*' << endl;
cout << e << endl << "isClear(): " << (e.isClear() ? "Failed!" : "Passed!") << endl;
if (e.isClear()) ok = false;
cout << endl;

e.message("Short Message");
cout << e << endl << e.message() << endl << "isClear(): " << (e.isClear() ? "Failed!" : "Passed!") << endl;
if (e.isClear()) ok = false;

e.clear();
cout << e << endl << "isClear(): " << (e.isClear() ? "Passed!" : "Failed!") << endl;
if (!e.isClear()) ok = false;
cout << endl;

if (ok) {
cout << "You passed all the tests!" << endl;
}
else {
cout << "You did not pass all the tests" << endl;
ret = 1;
}

return ret;
}

最佳答案

在你的 main() 中你有:

ErrorMessage e;

它将使用空指针调用您的构造函数:

ErrorMessage::ErrorMessage(const char* errorMessage) {
m_message = nullptr;
message(errorMessage);
}

然后您的构造函数将调用 message() 并将该空指针传递给它:

void ErrorMessage::message(const char* str) {
delete[] m_message;
m_message = new char[strlen(str) + 1];
strcpy(m_message, str);
}

因此,您正在尝试对空指针执行 strlen()。这就是它给出段错误的原因。

关于c++ - 分配动态内存以存储字符串拷贝时程序中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47645875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com