c++中的动态内存点
Dynamic memory Points in C++
我试图在c++中编写代码,从文件中读取,点的序列,将其存储在动态数组中,然后打印回来。
这是我得到的规格:
"我们想要利用动态内存的优势,而不是根据我们的估计在开始分配足够大的内存量,我们实现了以下算法:
最初分配的内存很少。
在循环的每次迭代(从文件中读取并存储到动态数组)我们跟踪:
- 数组的最大大小(分配的内存)。
- 数组中元素的个数。
当,由于新插入,元素数变为大于数组的最大大小,需要进行内存重新分配位置如下:
- 分配一个更大的动态数组。
- 将先前数组中的所有元素复制到新数组中。
- 释放分配给前一个数组的内存区域。
- 获取指向前一个数组的指针以指向新数组。
- 在数组末尾添加新项。这就是我的问题所在。
从下面的代码中,我认为除了最后一个要求外,其他一切都很好,这是在数组末尾添加新项。
当数组Max_Size超过文件的数目时,代码工作正常但是当我尝试扩展num_elements时,结果是文件中多余的数字被保存为0
。
还需要补充的是,赋值还不允许使用向量。对不起,我忘了提这一点,我是新的stackoverflow和一些编程。
请帮忙
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
struct point {
double x;
double y;
};
int main () {
ifstream inputfile;
inputfile.open("datainput.txt");
if(!inputfile.is_open()){
cout << "could not open file" << endl;
exit(EXIT_FAILURE);
}
//initially very little memory is allocated
int Max_Size = 10;
int num_elements = 0;
point *pp = new point[Max_Size];
//read from file and store in dynamic array
for (int i = 0; !inputfile.eof(); i++) {
inputfile >> pp[i].x >> pp[i].y;
num_elements++; //keep track of number of elements in array
}
//detecting when number of elements exeeds max size due to new insertion:
if (num_elements > Max_Size){
// allocate another dynamic array with a greater maximum size
Max_Size *= 2; // Max_Size = 2*Max_Size to double max size whenever there's memory problem
point *pp2 = new point[Max_Size];
//copy all elements from previous array to new one
for (int j=0; j<(Max_Size/2); j++) {
pp2[j].x = pp[j].x ;
pp2[j].y = pp[j].y;
}
//deallocate memory area allocated for previous array
delete [] pp;
//get pointer to previous array to point to the new one
pp = pp2;
**//add new item at end of the array
for (int k = ((Max_Size/2)-1); k<num_elements; k++) {
inputfile.seekg(k, ios::beg) >> pp2[k].x;
inputfile.seekg(k, ios::beg) >> pp2[k].y;
}**
//print out dynamic array values
for (int l = 0; l<num_elements; l++) {
cout << pp2[l].x << ",";
cout << pp2[l].y << endl;
}
//delete dynamic array
delete [] pp2;
}
else {
//print out dynamic array values
for (int m = 0; m<num_elements; m++) {
cout << pp[m].x << ",";
cout << pp[m].y << endl;
}
//delete dynamic array
delete [] pp;
}
cout <<"Number of elements = " << num_elements <<endl;
//close file
inputfile.close();
return 0;
}
其他人已经指出了std::vector
。下面是使用它的代码的大致样子:
#include <vector>
#include <iostream>
struct point {
double x;
double y;
friend std::istream &operator>>(std::istream &is, point &p) {
return is >> p.x >> p.y;
}
friend std::ostream &operator<<(std::ostream &os, point const &p) {
return os << p.x << "," << p.y;
}
};
int main() {
// open the file of data
std::ifstream in("datainput.txt");
// initialize the vector from the file of data:
std::vector<point> p {
std::istream_iterator<point>(in),
std::istream_iterator<point>() };
// print out the data:
std::copy(p.begin(), p.end(), std::ostream_iterator<point>(std::cout, "n"));
}
除了比你发布的代码更短更简单之外,让它工作可能会简单得多(如蛋糕上的糖霜)它几乎肯定会运行得更快1(特别是如果你有很多数据)。
<一口>1. 公平地说,我觉得有必要指出,大的速度差异主要来自使用n
而不是endl
来终止每条线。这避免了在写入每行时刷新文件缓冲区,这可以很容易地提高速度。参见:https://stackoverflow.com/a/1926432/179910一口>
程序逻辑有缺陷。循环运行到EOF,但不检查是否超出了数组大小。我会在第一个循环中添加一个if语句来检查您是否已经传递了Max_Size。我还会写一个函数来重新分配内存,这样你就可以在第一个循环中调用这个函数。
你的内存分配也有问题。你应该这样做:
point temp = pp;
pp = new Point[...];
// Copy the contents of temp into pp
delete temp;
你需要先将指针指向旧数组,这样你就不会丢失它。然后,在将旧数组的内容复制到新数组中之后,可以删除旧数组。
- 在调用FreeLibrary后,释放动态链接到具有相同版本的CRT堆的DLL的内存
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- 对具有动态分配的内存和析构函数的类对象的引用
- 以下代码执行哪种内存分配(动态或静态)?
- 开放 CV 中的动态内存分配,用于视频处理
- 调用析构函数以释放动态分配的内存
- 为什么类和 main() 函数中也有动态内存分配
- 将静态内存更改为动态C++
- 在没有动态内存的世界中,我是否需要虚拟析构函数?
- 在运行时为动态分配的内存输入值
- c++ 动态内存 堆栈中的分配
- 释放动态分配的内存时是否需要执行此额外步骤
- 动态分配字符数组的内存
- 销毁C++中动态分配的内存(数组对象)
- 给定特定内存地址的数组的动态内存分配
- 释放动态内存时C++错误
- 对内存动态分配的类不使用"*"的逻辑
- 如果我将内存动态分配给静态变量,我应该释放它还是会自动释放它
- 给定内存地址的内存动态初始化
- 删除指针分配的内存(动态数组)