繁体   English   中英

制作第二维是常数的动态二维数组是愚蠢的吗?

[英]Is it dumb to make a dynamic 2d array where the second dimension is a constant?

我正在尝试创建一个行数可变的数组,但它总是有 4 列。 正在做类似的事情:

int** numGrades = new int* [num_exams];
for (int i = 0; i < num_exams; ++i)
{
    numGrades[i] = new int[4];
}

这样做的好方法吗? 我觉得有一种更简单的方法,但我想不出一个。 此外,该数组不断给我内存泄漏,所以我想知道这是否是因为我在做我不应该做的事情。 仅供参考,该程序禁止使用载体。

你可以制作一个行数组。

struct Row{
   int values[4];
};

Row* numGrades = new Row[num_exams];

您可以跳过 for 循环:

int* numGrades = new int[num_exams*4];
int firstElement = numGrades[x];
int secondElement = numGrades[x+1];
int thirdElement = numGrades[x+2];
int fourthElement = numGrades[x+3];

通过跳过 for 循环,您可以获得:

  1. 您不必使用 for 循环来释放内存:

    删除[] numGrades;

  2. 堆没有那么多碎片,因为您不会多次调用“新”。

但这一切都取决于您使用它的目的。 在现代 C++ 中,使用动态但在 std::vector 中创建结构并不是一个好主意。

也许你可以试试这个。

typedef int row[4];
//or
using row = int[4];

row *numGrades = new row[num_exams];

在许多情况下,分配一定数量的固定大小的数组是好的和有利的。

除了struct (这是一个很好的选择)之外,另一种选择是声明一个包含固定数量元素的Pointer-To-Array 这样做的好处是您可以为内存块提供单一分配单一空闲 (就像使用struct 数组一样)如果您需要增加内存块(使用 -- 声明更大的块,将现有复制到更大,删除现有重新分配),它会简化过程。 在你的情况下:

 int (*numGrades)[4] = new int[num_exams][4];

这将一次分配num_exams数量的 4 个int数组。 这提供了单个delete[] numGrades; 当你完成记忆时。

使用std::istringstream保存示例值以读取到包含修复大小数组的内存块的简短示例可能是:

#include <iostream>
#include <sstream>

int main (void) {
    
    std::istringstream iss { "1 2 3 4 5 6 7 8 9" };
    
    int npoints = 3,
        (*points)[3] = new int[npoints][3],
        n = 0;
    
    while (n < 3 && iss >> points[n][0] >> points[n][1] >> points[n][2])
        n++;
    
    for (int i = 0; i < n; i++)
        std::cout << points[i][0] << "  " << points[i][1] << "  " << points[i][2] << '\n';
    
    delete[] points;
}

注意:如果不是出于教育目的,您应该避免使用newdelete来支持诸如std::vector之类的容器)

示例使用/输出

$ ./bin/newptr2array3
1  2  3
4  5  6
7  8  9

值得注意的是, struct的好处是它允许您使用std::istreamstd::ostream重载>><<以提供方便的函数来读取和写入您需要的数据。

所以无论哪种方式,一个指针到阵列固定元件,或创建一个struct ,然后数组struct是完全没有问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM