C++のコードサンプルでは、アクセス違反やヒープの破損によってランダムにアプリケーションが失敗する理由を説明できません。私は、サンプルに現在悩んでいるコードが含まれていることを知っています(charへのポインタで直接作業しています)が、これは単なる学習目的のためのものです。誰かがコードを見て、私が見逃していることが分かったら教えてもらえれば、とても感謝しています。ありがとう。新しいチャンクでのアクセス違反/ヒープの破損
class Operand
{
private:
double value;
char* message;
public:
Operand(double value);
~Operand();
char* operator+(const Operand& other);
};
Operand::Operand(double value)
{
this->value = value;
}
char* Operand::operator+(const Operand& other)
{
char s[256];
double sum = this->value + other.value;
sprintf_s(s, "The sum of %f and %f is %f", this->value, other.value, sum);
this->message = new char[strlen(s)]; //this is where it sometimes crashes with an access violation
strcpy_s(this->message, sizeof(s), s);
return this->message;
}
Operand::~Operand()
{
if (this->message != nullptr)
delete[] this->message;
}
int main()
{
double operand1, operand2;
cout << "Please input the calculator parameters: operand1 operand2: ";
cin >> operand1 >> operand2;
auto Operand1 = Operand(operand1);
auto Operand2 = Operand(operand2);
char* message1 = Operand1 + Operand2;
char* message2 = Operand2 + Operand1;
cout << message1 << "\n";
cout << message2 << "\n";
return 0; //and sometimes it crashes right after outputting to cout
}
2つのchar *メッセージポインタが異なるオペランドインスタンスに属しているため、2つのchar *メッセージポインタが互いに干渉する理由はわかりません。それが本当にそれの原因であれば。それとも、私は何かを逃しています。
アイデアが足りないので、私は目の第二のペアを本当に感謝します。ありがとう。
良いフィードバックをいただきありがとうございます。私のサンプルでは複数のベストプラクティスが壊れているかもしれませんが、主に学習目的のためです。これは完全に設計されたものではありません。しかし、本当にありがとうございます。 –
初期化されていないポインタについて、nullptrに初期化すると、問題がなくなります。私は既にnullptrをチェックしていますので、削除する前に –
はい、その特定の問題に対処します。 –