メモリをヒープに動的に割り当ててから、割り当てられたメモリを削除しようとしています。以下は私に苦労しているコードです:
// String.cpp
#include "String.h"
String::String() {}
String::String(char* source)
{
this->Size = this->GetSize(source);
this->CharArray = new char[this->Size + 1];
int i = 0;
for (; i < this->Size; i++) this->CharArray[i] = source[i];
this->CharArray[i] = '\0';
}
int String::GetSize(const char * source)
{
int i = 0;
for (; source[i] != '\0'; i++);
return i;
}
String::~String()
{
delete[] this->CharArray;
}
コンパイラが CharArray を削除しようとしたときに発生するエラーは次のとおりです。
0xC0000005: ロケーション 0xcccccccc0 の読み取りアクセス違反。
そして、スタック上の最後の呼び出しは次のとおりです。
msvcr100d.dll!operator delete(void * pUserData) 52 行目 + 0x3 バイト C++
このコード内にエラーが存在することは間違いありませんが、必要なその他の情報を提供します。そうそう、XP に VS 2010 を使用しています。
編集:ここに私のString.hがあります
// String.h - string class
#pragma once
#define NOT_FOUND -1
class String
{
public:
String();
String(char* source);
static int GetSize(const char * source);
int Find(const char* aChar, int startPosition = 0);
~String();
private:
char* CharArray;
int Size;
};