【问题标题】:Declaring multidimensional floating or unsigned dynamic array in C++在 C++ 中声明多维浮动或无符号动态数组
【发布时间】:2018-08-21 00:32:21
【问题描述】:

如何在 C++ 中初始化浮动或无符号多维动态数组?

我尝试了以下方法并遇到了段错误,

float **array = NULL:

array = new float* [rows];
for(unsigned int i = 0; i < rows; ++i)
{
    array[i] = new float [cols];
}

提前致谢。

【问题讨论】:

  • 那里没有足够的代码来诊断问题。请发帖minimal reproducible example。
  • 请至少显示您在数组中写入内容的位置(并消除数组的重复定义)
  • @onie 我会继续说你真的应该使用向量或类似的抽象。它会为您省去很多麻烦。
  • std::vector&lt;float&gt; array(rows * cols); 或者如果你真的不想抽象它std::vector&lt;std::vector&lt;float&gt;&gt; array(rows, std::vector&lt;float&gt;(cols));
  • float **array = NULL: 后跟 float** array = new float* [rows]; 看起来您可能遇到了 Variable Shadowing 问题。但是,如果没有前面提到的 MCVE,就无法确定。

标签: c++ arrays dynamic-memory-allocation


【解决方案1】:

我投票决定将其作为Declaring a pointer to multidimensional array and allocating the array的副本关闭

在声明多维数组arrary[col][rows] 时,[col] 需要保持不变,而[rows] 不必保持不变。这个符号应该可以解决这个问题。

const int col = someval;
const int row = someval;

int (*array)[row] = new int[col][row];

否则,您可以使用single 维度数组,使其具有[row*col] 元素。从那里你必须在indexing2D structure 的平面数组时进行计算。

通常单数组和双 for 循环看起来像这样:

type *array = new type[row * col];

for ( int i = 0; i < row; i++ ) {
    for ( int j = 0; j < col; j++ ) {
        array[i * col + j];
    }
}

这应该会有所帮助。

【讨论】:

    【解决方案2】:

    您可以以相当传统的方式为给定数量的pointers 分配一个给定数量的float,然后使用new expression 为float 的每个分配块分配一个指针,然后使用相应的delete expression 在不再需要分配的内存时重新获得分配的内存以避免内存泄漏。这与使用 malloc 和 free 大致相似,但在底层发生的事情有很大的不同。

    例如,给定您的 pointer-to-pointer-to-float,您可以构建一个简短的示例来说明:

    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    #define ROWS 10
    #define COLS 10
    
    int main (void) {
    
        float **array = NULL;               /* pointer to pointer to float */
    
        array = new float*[ROWS];           /* allocate ROWS pointers to float */
        for (int i = 0; i < ROWS; i++) {
            array[i] = new float[COLS];     /* allocate COLS floats */
            for (int j = 0; j < COLS; j++)
                array[i][j] = (i + 1) * (j + 1);
        }
    
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLS; j++)
                cout << setw(4) << array[i][j];
            cout << "\n";
            delete[] array[i];              /* delete COLS floats */
        }
        delete[] array;                     /* delete ROWS pointers to float */
    }
    

    使用/输出示例

    $ ./bin/array_new_del
       1   2   3   4   5   6   7   8   9  10
       2   4   6   8  10  12  14  16  18  20
       3   6   9  12  15  18  21  24  27  30
       4   8  12  16  20  24  28  32  36  40
       5  10  15  20  25  30  35  40  45  50
       6  12  18  24  30  36  42  48  54  60
       7  14  21  28  35  42  49  56  63  70
       8  16  24  32  40  48  56  64  72  80
       9  18  27  36  45  54  63  72  81  90
      10  20  30  40  50  60  70  80  90 100
    

    您必须注意始终保留指向您分配的内存和delete 当不再需要时分配的内存的指针,以防止内存泄漏。您还必须通过内存错误检查程序运行分配和删除内存的代码,以确保您已正确使用内存,并且在不再需要时已释放所有内存。对于 Linux,valgrind 是正常选择。你只需要通过它运行你的代码,例如

    $ valgrind ./bin/array_new_del
    ==1206== Memcheck, a memory error detector
    ==1206== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
    ==1206== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
    ==1206== Command: ./bin/array_new_del
    ==1206==
       1   2   3   4   5   6   7   8   9  10
       2   4   6   8  10  12  14  16  18  20
       3   6   9  12  15  18  21  24  27  30
       4   8  12  16  20  24  28  32  36  40
       5  10  15  20  25  30  35  40  45  50
       6  12  18  24  30  36  42  48  54  60
       7  14  21  28  35  42  49  56  63  70
       8  16  24  32  40  48  56  64  72  80
       9  18  27  36  45  54  63  72  81  90
      10  20  30  40  50  60  70  80  90 100
    ==1206==
    ==1206== HEAP SUMMARY:
    ==1206==     in use at exit: 0 bytes in 0 blocks
    ==1206==   total heap usage: 13 allocs, 13 frees, 74,208 bytes allocated
    ==1206==
    ==1206== All heap blocks were freed -- no leaks are possible
    ==1206==
    ==1206== For counts of detected and suppressed errors, rerun with: -v
    ==1206== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
    

    始终确认您已释放所有分配的内存并且没有内存错误。

    【讨论】:

      猜你喜欢
      • 2013-12-17
      • 2020-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-16
      • 2011-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多