堆损坏检测:在正常块之后(#153)
HEAP CORRUPTION DETECTED: after Normal block (#153)
CRT检测到应用程序在堆缓冲区结束后写入内存。
然而我不确定我在堆后面写的地方。当我调用delete[] data;
时,错误就发生了。Data是指向双精度数组的指针。我已经试着去掉一些不必要的代码。
源代码:#include "sequence2.h"
using namespace main_savitch_4;
// CONSTRUCTORS and DESTRUCTORS
// The default sequence constructor
// Purpose: To create an instance of a sequence
// Parameters: (int) initial_capacity
// Returns: None
sequence::sequence(int initial_capacity)
{
// Allocate memory for the new sequence
data = new value_type[capacity];
// Set the capacity to the passed in capacity
capacity = initial_capacity;
// Set used to 0 because there are currently no objects in the sequence
used = 0;
// Set the current index to the 0 for the first item
current_index = 0;
}
// The sequence copy constructor
// Purpose: To create a deep copy of a sequence
// Parameters: A source sequence
// Returns: None
sequence::sequence(const sequence& source)
{
// Allocate memory to the new sequence using the capacity of the passed sequence
data = new value_type[source.capacity];
// Set the capacity to the passed capacity
capacity = source.capacity;
// Set used to the number of used indexes in the previous sequence
used = source.used;
// Set the current_index to the index of the passed sequence
current_index = source.current_index;
// Copy all data from the passed sequence into the new sequence
for (int i = 0; i < used; i++)
{
data[i] = source.data[i];
}
}
// The default sequence destructor
// Purpose: To de-allocate memory for a sequence
// Parameters: None
// Returns: None
sequence::~sequence()
{
// De-allocate dynamic memory
delete[] data;
}
// MODIFICATION MEMBER FUNCTIONS
// The overload assignment operator function
// Purpose: To assign a sequence with the data from another sequence
// Parameters: A source sequence
// Returns: None
void sequence::operator=(const sequence& source)
{
value_type *new_data;
// Check for possible self-assignment
if (this == &source)
return;
// If the capacity of the source is not equal to the original
if (capacity != source.capacity)
{
// Create a new sequence using the source capacity
new_data = new value_type[source.capacity];
// Delete the old sequence
delete[] data;
// Assign data pointer to the new sequence
data = new_data;
// Set capacity to the new source capacity
capacity = source.capacity;
}
// Set used equal to the source "used"
used = source.used;
// Copy data from the source sequence into the new sequence
for (int i = 0; i < used; i++)
{
data[i] = source.data[i];
}
}
// The resize function
// Purpose: To grow a sequence when necessary
// Parameters: (int) new_capacity
// Returns: None
void sequence::resize(int new_capacity)
{
// Declare a larger array
value_type *larger_array;
if (new_capacity == capacity)
return; // The allocated memory is already the right size
if (new_capacity < used)
new_capacity = used; // Can't allocate less than we are using
// Dynamically allocate memory for the larger array with the new capacity
larger_array = new value_type[new_capacity];
// Copy the data from the old array to the larger array
for (int i = 0; i < used; i++)
{
larger_array[i] = data[i];
}
// Delete the old array
delete[] data;
// Set the pointer to the larger array
data = larger_array;
// Set the capacity to the new capacity
capacity = new_capacity;
}
// The insert function
// Purpose: To insert an item before the current index
// Parameters: (const value_type&) entry
// Returns: None
void sequence::insert(const value_type& entry)
{
// If used and capacity are the same, resize the sequence
if (used == capacity)
resize(used + 1);
// If the current index is an item...
if (is_item())
{
// Block of code that moves objects ahead of insert forward
for (int i = used; i > current_index; i--)
{
data[i] = data[i-1];
}
}
// Set the current index to the new entry
data[current_index] = entry;
// Increment used because we've added an item
++used;
}
// The attach function
// Purpose: To attach an item after the current index
// Parameters: (const value_type&) entry
// Returns: None
void sequence::attach(const value_type& entry)
{
// If used and capacity are the same, resize the sequence
if (used == capacity)
resize(used + 1);
// If the current index is an item...
if (is_item())
{
// Block of code that moves objects ahead of attach forward
for (int i = used; i > current_index; i--)
{
data[i] = data[i-1];
}
// Set the current index to the recently attached item
++current_index;
// Store the entry in the new current index
data[current_index] = entry;
}
else
{
// Set the current index to the end of the sequence
current_index = used;
// Put that entry into that new index
data[current_index] = entry;
}
// Increment used because we've added an item
++used;
}
// CONSTANT MEMBER FUNCTIONS
// The is_item function
// Purpose: To check if the current index contains an item
// Parameters: None
// Returns: None
bool sequence::is_item() const
{
// If the current index is greater than used, or used is equal to 0
// there is no current item
if (current_index >= used || used == 0)
return false;
else // otherwise there is a current item
return true;
}
LOL,这就是解决方案
sequence::sequence(int initial_capacity)
{
// Allocate memory for the new sequence
data = new value_type[**initial_capacity**];
// Set the capacity to the passed in capacity
capacity = initial_capacity;
// Set used to 0 because there are currently no objects in the sequence
used = 0;
// Set the current index to the 0 for the first item
current_index = 0;
}
相关文章:
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 在类定义之后定义一个私有方法
- 在循环C++中指定字符串之后,不会打印该字符串
- C++宏忽略之后的内容
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- strncpy之后的char数组的错误行为
- 计算十进制 c++ 之后的数字
- "x += x--"之后的 x 是什么?
- 类的前向声明之后的类成员函数定义,在类声明之前
- 为什么将双精度转换为 int 似乎在第 16 位数字之后将其四舍五入?
- execlp() 在 fork() 之后无法正常工作
- 我认为我的代码很好,但它在 cin a 之后停止并且没有进一步?
- 如何在MISRA C++之后实施CRTP
- 在 OpenCV 的 namedWindow 之前或之后初始化 Tesseract
- 检测到堆损坏:在正常块 c++ 动态 2D 数组之后
- C++ 如果在 if 为 true 之后运行,为什么还会这样做
- 在 fork() 之后,我在我的程序中不断得到相同的 pid
- OpenSSL C API:如何在程序exec()之后恢复TLS连接?
- 了解带有命名空间污染的 TOTW 153 示例
- 堆损坏检测:在正常块之后(#153)