我正在尝试为堆动态分配内存,然后删除分配的内存。下面是让我很难受的代码:
// 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:访问冲突读取位置 0xccccccc0。
这是堆栈上的最后一个调用:
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;
};