Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建动态收集错误 我有管理动态集合列表的代码,主要是用.NETSoal.List集合来启发的,但是这是用普通C++编写的。 void List<T>::Insert(int index, T item) { if(index > GetLength() && index >= 0) throw new StandartException("Index was out of array range"); if(GetLength()==length) { T * newl = new T[length + 4]; this->CopyTo(newl); delete[] this->items; this->items = newl; length += 4; } T * store = new T[length]; CopyTo(store, index); store[index] = item; CopyTo((store + index + 1), index, GetLength() - index); used++; delete[] items; items = store; } template <typename T> void List<T>::CopyTo(T * destination, int start, int count) { for(int i = start, c = 0;i < GetLength() && c < count;i++, c++) *(destination + i) = items[i]; } void List::Insert(int索引,T项) { 如果(index>GetLength()&&index>=0) 抛出新标准异常(“索引超出数组范围”); 如果(GetLength()==长度) { T*newl=新的T[长度+4]; 此->复制到(newl); 删除[]此->项; 此->项目=新建; 长度+=4; } T*存储=新的T[长度]; CopyTo(存储、索引); 存储[索引]=项目; CopyTo((存储+索引+1),索引,GetLength()-index); 使用++; 删除[]项; 物品=商店; } 模板 无效列表::复制到(T*目的地、整数开始、整数计数) { 对于(int i=start,c=0;iGetLength()&&index>=0)这似乎是错误的。长度等于的索引通常是超出范围的。如果(index>=GetLength()&&index_C++_Arrays_Pointers_Heap - Fatal编程技术网 GetLength()&&index>=0) 抛出新标准异常(“索引超出数组范围”); 如果(GetLength()==长度) { T*newl=新的T[长度+4]; 此->复制到(newl); 删除[]此->项; 此->项目=新建; 长度+=4; } T*存储=新的T[长度]; CopyTo(存储、索引); 存储[索引]=项目; CopyTo((存储+索引+1),索引,GetLength()-index); 使用++; 删除[]项; 物品=商店; } 模板 无效列表::复制到(T*目的地、整数开始、整数计数) { 对于(int i=start,c=0;iGetLength()&&index>=0)这似乎是错误的。长度等于的索引通常是超出范围的。如果(index>=GetLength()&&index,c++,arrays,pointers,heap,C++,Arrays,Pointers,Heap" /> GetLength()&&index>=0) 抛出新标准异常(“索引超出数组范围”); 如果(GetLength()==长度) { T*newl=新的T[长度+4]; 此->复制到(newl); 删除[]此->项; 此->项目=新建; 长度+=4; } T*存储=新的T[长度]; CopyTo(存储、索引); 存储[索引]=项目; CopyTo((存储+索引+1),索引,GetLength()-index); 使用++; 删除[]项; 物品=商店; } 模板 无效列表::复制到(T*目的地、整数开始、整数计数) { 对于(int i=start,c=0;iGetLength()&&index>=0)这似乎是错误的。长度等于的索引通常是超出范围的。如果(index>=GetLength()&&index,c++,arrays,pointers,heap,C++,Arrays,Pointers,Heap" />

创建动态收集错误 我有管理动态集合列表的代码,主要是用.NETSoal.List集合来启发的,但是这是用普通C++编写的。 void List<T>::Insert(int index, T item) { if(index > GetLength() && index >= 0) throw new StandartException("Index was out of array range"); if(GetLength()==length) { T * newl = new T[length + 4]; this->CopyTo(newl); delete[] this->items; this->items = newl; length += 4; } T * store = new T[length]; CopyTo(store, index); store[index] = item; CopyTo((store + index + 1), index, GetLength() - index); used++; delete[] items; items = store; } template <typename T> void List<T>::CopyTo(T * destination, int start, int count) { for(int i = start, c = 0;i < GetLength() && c < count;i++, c++) *(destination + i) = items[i]; } void List::Insert(int索引,T项) { 如果(index>GetLength()&&index>=0) 抛出新标准异常(“索引超出数组范围”); 如果(GetLength()==长度) { T*newl=新的T[长度+4]; 此->复制到(newl); 删除[]此->项; 此->项目=新建; 长度+=4; } T*存储=新的T[长度]; CopyTo(存储、索引); 存储[索引]=项目; CopyTo((存储+索引+1),索引,GetLength()-index); 使用++; 删除[]项; 物品=商店; } 模板 无效列表::复制到(T*目的地、整数开始、整数计数) { 对于(int i=start,c=0;iGetLength()&&index>=0)这似乎是错误的。长度等于的索引通常是超出范围的。如果(index>=GetLength()&&index

创建动态收集错误 我有管理动态集合列表的代码,主要是用.NETSoal.List集合来启发的,但是这是用普通C++编写的。 void List<T>::Insert(int index, T item) { if(index > GetLength() && index >= 0) throw new StandartException("Index was out of array range"); if(GetLength()==length) { T * newl = new T[length + 4]; this->CopyTo(newl); delete[] this->items; this->items = newl; length += 4; } T * store = new T[length]; CopyTo(store, index); store[index] = item; CopyTo((store + index + 1), index, GetLength() - index); used++; delete[] items; items = store; } template <typename T> void List<T>::CopyTo(T * destination, int start, int count) { for(int i = start, c = 0;i < GetLength() && c < count;i++, c++) *(destination + i) = items[i]; } void List::Insert(int索引,T项) { 如果(index>GetLength()&&index>=0) 抛出新标准异常(“索引超出数组范围”); 如果(GetLength()==长度) { T*newl=新的T[长度+4]; 此->复制到(newl); 删除[]此->项; 此->项目=新建; 长度+=4; } T*存储=新的T[长度]; CopyTo(存储、索引); 存储[索引]=项目; CopyTo((存储+索引+1),索引,GetLength()-index); 使用++; 删除[]项; 物品=商店; } 模板 无效列表::复制到(T*目的地、整数开始、整数计数) { 对于(int i=start,c=0;iGetLength()&&index>=0)这似乎是错误的。长度等于的索引通常是超出范围的。如果(index>=GetLength()&&index,c++,arrays,pointers,heap,C++,Arrays,Pointers,Heap,所以有一个方法Insert,它必须在数组中指定的索引上插入项。 首先,我检查索引是否指定在0和长度+1之间(因为我需要有一个选项在集合的ond上添加项)。然后我测试它是否不是分配数组的末尾(GetLength()=获取数组中的元素数,length=为元素分配的空间数)。如果是,我将为数组分配新空间,复制实际元素,释放旧存储空间,并设置指向新地址的指针 之后,我将再次分配新的空间。我将实际元素从零复制到索引-1,设置必须插入其位置的项,并将其他旧元素复制到它们的索引(它们以前的索引+1)。最后,我

所以有一个方法Insert,它必须在数组中指定的索引上插入项。 首先,我检查索引是否指定在0和长度+1之间(因为我需要有一个选项在集合的ond上添加项)。然后我测试它是否不是分配数组的末尾(GetLength()=获取数组中的元素数,length=为元素分配的空间数)。如果是,我将为数组分配新空间,复制实际元素,释放旧存储空间,并设置指向新地址的指针

之后,我将再次分配新的空间。我将实际元素从零复制到索引-1,设置必须插入其位置的项,并将其他旧元素复制到它们的索引(它们以前的索引+1)。最后,我释放了旧的空间,添加了新的

错误:我开始调试。一切正常,我第一次运行Insert没有问题,但在第一次删除(
delete[]this->items;
in if block)时,我收到了以下错误:

有人知道我为什么会得到这个,我怎样才能修复它吗?我想我还没有穿过阵列范围。
请帮助。

您的问题是这一行:

T * store = new T[length];

插入一个项,但不要分配比以前大的数组。当您转到新数组的
CopyTo
时,数组溢出。

设置的“长度”是什么,是全局的还是什么,因为我没有看到它的初始化或定义。类似地,如果您正在进行这种堆损坏,我的第一个想法是您正在尝试释放一个动态数组,而它并没有那么大。IE:
char*c=新字符;删除[]c可能导致此问题。
如果(index>GetLength()&&index>=0)
这似乎是错误的。长度等于的索引通常是超出范围的。如果(index>=GetLength()&&index<0)
,它不应该是
?length是存储分配内存大小的私有变量。所以在构造函数中,有分配
length=4;项目=新T[4],但没有任何项目。当我添加第一项时,它是
items[0]=item并且我只在需要时分配新空间。我很确定我没有做任何类似的事情。jrok:对不起,我有不同的代码,我在写这个问题时添加了它。。。仍然出现堆损坏错误。在调试过程中(我从未使用过Visual C++的调试器),您是否可以查看该值,以验证是否调用了构造函数?我读到的所有内容都指向在非数组的对象上使用
delete[]
。是的!没错。这是因为当我向集合中插入项并复制空空间时,会出现溢出。我以为这不会发生,因为我已经抓住了。但你是对的。它起作用了。谢谢