动态内存错误
Error with dynamic memory
我正在尝试使用动态内存方法而不是矢量方法来添加元素。最初,动态内存的最大大小设置为 5。但是,一旦我尝试增加超过当前动态内存的容量,第 0 或第 1 个索引的元素就会丢失其引用。如果我不指定动态内存的大小,程序工作正常,比如:dynamic_memory = new int
;。我想知道为什么他们失去了参考资料将动态内存的大小调整为大于初始容量。PS:我正在使用代码::块16.01
这是我的程序。
#include <iostream>
#include <cstdlib>
using namespace std;
class DynamicVector
{
public:
DynamicVector();
virtual ~DynamicVector();
void insertElement(int input);
int showCapacity();
int showSize();
void doubleSize(int * dynamic_memory);
friend ostream& operator << (ostream& outs, const DynamicVector obj);
private:
int * dynamic_memory;
int max_count; // this is similar to the capacity of the vector
int current_count; // this is similar to size of a vector
};
DynamicVector::DynamicVector()
{
max_count = 5;
dynamic_memory = new int[max_count];
current_count = 0;
}
DynamicVector::~DynamicVector()
{
delete [] dynamic_memory;
}
int DynamicVector::showCapacity(){
return max_count;
}
void DynamicVector::insertElement(int input)
{
if (current_count >= max_count)
doubleSize(dynamic_memory);
dynamic_memory[current_count] = input;
current_count++;
}
void DynamicVector::doubleSize(int * dynamic_memory){
int * tmp = new int[max_count];
for (int i = 0; i < max_count; i++)
tmp[i] = dynamic_memory[i];
delete [] dynamic_memory;
max_count = max_count * 2;
dynamic_memory = new int[max_count];
for (int i = 0; i < max_count; i++)
dynamic_memory[i] = tmp[i];
delete [] tmp;
}
int DynamicVector::showSize(){
return current_count;
}
ostream& operator <<(ostream& outs, const DynamicVector obj)
{
for (int i = 0; i < obj.current_count; i++)
outs << obj.dynamic_memory[i] << endl;
return outs;
}
int main()
{
DynamicVector v;
int numberOfIntendedElement = 11;
cout << "Previously, the capacity of vector was: " << v.showCapacity() << endl;
for (int i = 0; i < numberOfIntendedElement; i++)
v.insertElement(i);
cout << "The capacity of the new vector is: " << v.showCapacity() << endl;
cout << "The size of the new vector is: " << v.showSize() << endl;
cout << "The values in the dynamic vector are: n" << v << endl;
return 0;
}
结果:
41107976
42075512
2
3
4
5
6
7
8
9
10
in
void doubleSize(int * dynamic_memory);
这里定义的dynamic_memory
掩盖了喜剧喜剧和未定义行为的成员dynamic_memory;
。
本地dynamic_memory
将重新指向新缓冲区,但成员dynamic_memory
在函数退出后继续指向已删除的原始地址。这意味着所有后续插入都会进入无效内存,而 Crom 只知道之后会发生什么。
溶液
不传入任何内容并使用成员变量。将函数重新定义为
void doubleSize();
其他问题在评论中得到解决,需要修复。
感谢大家的宝贵意见和建议,特别是user4581301指出喜剧和未定义的行为。在我将函数重新定义为void doubleSize()
后,它工作正常。这是我的最终工作代码。
#include <iostream>
#include <cstdlib>
using namespace std;
class DynamicVector
{
public:
DynamicVector();
virtual ~DynamicVector();
void insertElement(int input);
int showCapacity();
int showSize();
void doubleSize();
friend ostream& operator << (ostream& outs, const DynamicVector obj);
private:
int * dynamic_memory;
int max_count; // this is similar to the capacity of the vector
int current_count; // this is similar to size of a vector
};
DynamicVector::DynamicVector()
{
max_count = 5;
dynamic_memory = new int[max_count];
current_count = 0;
}
DynamicVector::~DynamicVector()
{
delete [] dynamic_memory;
}
int DynamicVector::showCapacity(){
return max_count;
}
void DynamicVector::insertElement(int input)
{
if (current_count >= max_count)
doubleSize();
dynamic_memory[current_count] = input;
current_count++;
}
void DynamicVector::doubleSize(){
int * tmp = new int[max_count];
for (int i = 0; i < max_count; i++)
tmp[i] = dynamic_memory[i];
delete [] dynamic_memory;
max_count = max_count * 2;
dynamic_memory = new int[max_count];
for (int i = 0; i < max_count/2; i++)
dynamic_memory[i] = tmp[i];
delete [] tmp;
}
int DynamicVector::showSize(){
return current_count;
}
ostream& operator <<(ostream& outs, const DynamicVector obj)
{
for (int i = 0; i < obj.current_count; i++)
outs << obj.dynamic_memory[i] << endl;
return outs;
}
int main()
{
DynamicVector v;
int numberOfIntendedElement = 11;
cout << "Previously, the capacity of vector was: " << v.showCapacity() << endl;
for (int i = 0; i < numberOfIntendedElement; i++)
v.insertElement(i);
cout << "The capacity of the new vector is: " << v.showCapacity() << endl;
cout << "The size of the new vector is: " << v.showSize() << endl;
cout << "The values in the dynamic vector are: n" << v << endl;
return 0;
}
输出
Previously, the capacity of vector was: 5
The capacity of the new vector is: 20
The size of the new vector is: 11
The values in the dynamic vector are:
0
1
2
3
4
5
6
7
8
9
10
相关文章:
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 是否值得降低我的代码的可读性,以便在出现内存不足错误时提供异常安全性?
- 内存错误低于在C++年实现埃拉托色尼筛分时的预期
- 共享内存:MapViewOfFile 返回错误 5
- 将 vector<vector<int>> 传递到函数中会产生内存错误
- 释放动态内存时C++错误
- 我有一个线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x8)错误.我认为这是由于内存管理不好.我可以
- 复制内存给出分段错误
- C++程序错误:malloc():内存损坏
- 在我的以下代码中获取 MLE(内存限制错误).尝试解决 ROUND C 2019(问题 A-摆动行走)启动问题
- 为什么删除分配的阵列会导致内存错误?
- 需要找到3个小错误-内存错误
- 错误:内存位置的 std::length_error
- 修复了瓦尔格林德错误内存泄漏的错误
- 错误:“内存集”未在此范围内声明
- OpenCV 2.4.5运行时错误内存
- OpenCV错误:内存不足
- STL映射,设置错误:内存超出分配块的末尾
- GLUT:致命错误:内存不足
- MPI错误:内存不足-有哪些解决方案选项