gpt4 book ai didi

c++ - cout C++ 期间出现堆错误

转载 作者:行者123 更新时间:2023-11-28 03:23:43 25 4
gpt4 key购买 nike

我在尝试计算 Data[index] 的返回值时遇到错误。如果有人能帮助我,那就太棒了。我知道这些错误通常是由分配的内存冲突或指针引用已删除的索引等引起的。虽然我没有删除任何东西,所以我不知道这个错误是从哪里来的。

头文件:

#pragma once
#define INITIAL_CAPACITY 100
#define CAPACITY_BOOST 40


//Encapsulates the C-array
template <typename DATA_TYPE>
class Vector
{
public:
//Default / init-constructor hybrid
Vector(int initialCapacity = INITIAL_CAPACITY)
{
Size=0;
Capacity = initialCapacity;

//Allocate the encapsulated C-array
Data= new DATA_TYPE[Size];
}

//MUST HAVE A COPY-CONSTRUCTOR THAT PERFORMS deep-copy
Vector(const Vector& copyFrom)
{
//Necessary to prevent assignment operator from crashing
//because it will attempt to Delete[] Data whe the Data pointer is garbage.
Data=NULL;
//Use assignment operator to perform the deep copy
*this = copyFrom;
}


//The class MUST have a destructor
~Vector()
{
//Deallocate memory that our class has allocated
delete[] Data;
}
//MUST have an assignment operator that performs deep copy
Vector& operator =(const Vector& copyFrom)
{
//0. Delete the old memory
delete[] Data;
//1. Copy size and Capacity
Size = copyFrom.Size;
Capacity = copyFrom.Capacity;
//2. Allocate Memory
Data = new DATA_TYPE[Capacity];
//3. Copy elemenets
for(int i=0; i < Size; i++)
Data[i]= copyFrom.Data[i];

//All assignment operators should return *this
return *this;
}

//Get accessors to return the values of Size and Capacity
int GetSize() const
{
return this->Size;
}

int GetCapacity() const
{
return Capacity;
}

void Insert(int insertAt, const DATA_TYPE& newElement)
{
//**ASSIGNMENT**
//1. Determine if we have enough capacity for extra element(reallocate)
Size=GetSize();
if(Size>=Capacity)
{
Capacity += CAPACITY_BOOST;
}
//Use a function to check bounds.

if((insertAt > Capacity)||(insertAt < 0))
{
throw "Index is out of bounds";
}
//2.Move the tail
for (int i=Size+1; i > insertAt; i--)
Data[i]=Data[i-1];

//3.Insert element
Data[insertAt]= newElement;

}
//Inserts a new element at the end fo the Vector and increments the size
void Add(const DATA_TYPE& newElement)
{

Insert(Size, newElement);
Size++;
}

void Remove(int index)
{
delete Data[index];
for(i=index; i < Size-1; i++)
Data[i]=Data[i+1];
Size--;
Capacity=Size;
//**ASSIGNMENT**
//Resize. Shrink vector when you have too much capacity
//TEST EVERYTHING
}

// Index operator
DATA_TYPE operator[] (int index) const
{
// Check the bounds and throw an exception
if ( (index < 0) || (index >= Size) )
throw "Error";

return Data[index];
}


private:
//The count of actually used C-array elements
int Size;
//The count of the allocated C-array elements
int Capacity;
//The encapsulated C-array (pointer)

DATA_TYPE* Data;
};

主要:

    #include <iostream>
#include "vector.h"
using namespace std;

#define TEST_CAPACITY 100
#define TEST_SIZE 10

template<typename DATA_TYPE>
void PassByValueTest(Vector<DATA_TYPE>passedByValue)
{
}
void main()
{
//myVector is initialized using the default constructor
Vector<int> myVector;

//Populate myVector with some test values
for (int i=0; i< TEST_SIZE; i++)
myVector.Add(i);

//myOtherVector initialized using the init-constructor, initial capacity is 10
//Vector<int> myOtherVector(TEST_CAPACITY);


//Test by passing vector by value
/*
PassByValueTest(myVector);

myVector = myOtherVector;
*/
for(int i = 0; i < TEST_SIZE; i++)
{
cout << myVector[i];
}
system("pause");
}

最佳答案

我猜你应该切换:

Data= new DATA_TYPE[Size];

Data= new DATA_TYPE[Capacity];

关于c++ - cout C++ 期间出现堆错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14721091/

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