创建具有可变大小尺寸的2D数组

时间:2021-08-24 04:20:08

I want to be able to create a 2d array the size of the width and height I read from a file, but I get errors when I say:

我希望能够创建一个2d数组,其大小与我从文件中读取的宽度和高度相同,但是当我说:

int array[0][0]
array = new int[width][height]

3 个解决方案

#1


18  

You should use pointer to pointers :

你应该使用指针指针:

int** array;
array = new int*[width];
for (int i = 0;i<width;i++)
    array[i] = new int[height];

and when you finish using it or you want to resize, you should free the allocated memory like this :

当你使用它或想要调整大小时,你应该释放分配的内存,如下所示:

for (int i = 0;i<width;i++)
    delete[] array[i];
delete[] array;

To understand and be able to read more complex types, this link may be useful :

要理解并能够阅读更复杂的类型,此链接可能很有用:

http://www.unixwiz.net/techtips/reading-cdecl.html

Hope that's Helpful.

希望这有帮助。

#2


1  

If the array is rectangular, as in your example, you can do it with just one allocation:

如果数组是矩形的,如在您的示例中,您可以只使用一个分配:

int* array = new int[width * height];

This effectively flattens the array into a single dimension, and it's much faster.

这有效地将阵列变平为单个维度,并且速度更快。

Of course, this being C++, why don't you use std::vector<std::vector<int> >?

当然,这是C ++,为什么不使用std :: vector >?

#3


1  

Try this way:

试试这种方式:

  int **array; // array is a pointer-to-pointer-to-int

    array = malloc(height * sizeof(int *));
    if(array == NULL)
        {
        fprintf(stderr, "out of memory\n");
        exit or return
        }
    for(i = 0; i < height ; i++)
        {
        array[i] = malloc(width * sizeof(int));
        if(array[i] == NULL)
            {
            fprintf(stderr, "out of memory\n");
            exit or return
            }
        }

    array = new int*[width];
    if(array == NULL)
        {
        fprintf(stderr, "out of memory\n");
        exit or return
         }
    else
    {
    for (int i = 0;i<width;i++)
        array[i] = new int[height];
    }

#1


18  

You should use pointer to pointers :

你应该使用指针指针:

int** array;
array = new int*[width];
for (int i = 0;i<width;i++)
    array[i] = new int[height];

and when you finish using it or you want to resize, you should free the allocated memory like this :

当你使用它或想要调整大小时,你应该释放分配的内存,如下所示:

for (int i = 0;i<width;i++)
    delete[] array[i];
delete[] array;

To understand and be able to read more complex types, this link may be useful :

要理解并能够阅读更复杂的类型,此链接可能很有用:

http://www.unixwiz.net/techtips/reading-cdecl.html

Hope that's Helpful.

希望这有帮助。

#2


1  

If the array is rectangular, as in your example, you can do it with just one allocation:

如果数组是矩形的,如在您的示例中,您可以只使用一个分配:

int* array = new int[width * height];

This effectively flattens the array into a single dimension, and it's much faster.

这有效地将阵列变平为单个维度,并且速度更快。

Of course, this being C++, why don't you use std::vector<std::vector<int> >?

当然,这是C ++,为什么不使用std :: vector >?

#3


1  

Try this way:

试试这种方式:

  int **array; // array is a pointer-to-pointer-to-int

    array = malloc(height * sizeof(int *));
    if(array == NULL)
        {
        fprintf(stderr, "out of memory\n");
        exit or return
        }
    for(i = 0; i < height ; i++)
        {
        array[i] = malloc(width * sizeof(int));
        if(array[i] == NULL)
            {
            fprintf(stderr, "out of memory\n");
            exit or return
            }
        }

    array = new int*[width];
    if(array == NULL)
        {
        fprintf(stderr, "out of memory\n");
        exit or return
         }
    else
    {
    for (int i = 0;i<width;i++)
        array[i] = new int[height];
    }