如何正确地在C动态分配的多维数组工作++ [英] How to properly work with dynamically-allocated multi-dimensional arrays in C++

查看:122
本文介绍了如何正确地在C动态分配的多维数组工作++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何定义C ++动态多维数组?例如,二维阵列?我试图用一个指针的指针,但不知何故,它失败。

How do I define a dynamic multi-dimensional array in C++? For example, two-dimensional array? I tried using a pointer to pointer, but somehow it is failing.

推荐答案

应该认识到,有在C ++中没有多维数组的支持,无论是作为一个语言功能和标准库的第一件事。所以什么我们可以内做的是它的一些仿真。我们怎么可以模仿,也就是说,整数的2维数组?这里有不同的选择,从最不适合于最适合的。

The first thing one should realize that there is no multi-dimensional array support in C++, either as a language feature or standard library. So anything we can do within that is some emulation of it. How can we emulate, say, 2-dimensional array of integers? Here are different options, from the least suitable to the most suitable.

如果阵列与指针仿真的类型,肯定的二维数组,应的指针的指针的类型仿真?像这样的事情?

If an array is emulated with pointer to the type, surely two-dimensional array should be emulated with a pointer to pointer to the type? Something like this?

int** dd_array = new int[x][y];

这是一个编译器错误的时候了。没有新的[] [] 运营商,所以编译器高兴地拒绝了。好了,怎么样?

That's a compiler error right away. There is no new [][] operator, so compiler gladly refuses. Alright, how about that?

int** dd_array = new int*[x];
dd_array[0][0] = 42;

这编译。在执行时,不愉快的消息崩溃。出事了,但什么?当然!我们没有分配给第一个指针内存 - 它指向持有点¯x指针指向int的内存块。但是,我们从来没有初始化,这些指针!让我们再试试。

That compiles. When being executed, it crashes with unpleasant messages. Something went wrong, but what? Of course! We did allocate the memory for the first pointer - it now points to a memory block which holds x pointers to int. But we never initialized those pointers! Let's try it again.

int** dd_array = new int*[x];
for (std::size_t i = 0; i < x; ++i)
   dd_array[i] = new int[y];

dd_array[0][0] = 42;

这不会给任何编译错误,并且正在执行时程序不会崩溃。任务完成?没有这么快。请记住,每次我们做一次叫,我们的必须拨打一个删除。所以,在这里你去:

That doesn't give any compilation errors, and program doesn't crash when being executed. Mission accomplished? Not so fast. Remember, every time we did call a new, we must call a delete. So, here you go:

for (std::size_t i = 0; i < x; ++i)
    delete dd_array[i];
delete dd_array;

现在,这太可怕了。语法是丑陋的,所有这些指针的人工管理......罗。让我们遍布掉落,做更好的东西。

Now, that's just terrible. Syntax is ugly, and manual management of all those pointers... Nah. Let's drop it all over and do something better.

确定。我们知道,在C ++中,我们真的不应该使用手动内存管理,并有一个方便的的std ::矢量躺在这里。所以,可能是我们能够做到这一点?

Ok. We know that in C++ we should not really use manual memory management, and there is a handy std::vector lying around here. So, may be we can do this?

std::vector<std::vector<int> > dd_array;

这还不够,很明显 - 我们从来没有规定这些数组的大小。因此,我们需要这样的事情:

That's not enough, obviously - we never specified the size of those arrays. So, we need something like that:

std::vector<std::vector<int> > dd_array(x);
for(auto&& inner : dd_array)
    inner.resize(y);

dd_array[0][0] = 42;

那么,是不是好了吗?没那么多。首先,我们仍然有这个循环,这是一个痛的眼球。什么是更重要的,我们很受伤我们的应用程序的性能。由于每个人的内矢量分配独立的,像这样的循环:

So, is it good now? Not so much. Firstly, we still have this loop, and it is a sore to the eye. What is even more important, we are seriously hurting performance of our application. Since each individual inner vector is independently allocated, a loop like this:

int sum = 0;
for (auto&& inner : dd_array)
    for (auto&& data : inner)
       sum += data;

将导致迭代在许多独立分配的内部载体。而且,由于CPU只缓存连续存储,那些小的独立载体水湿被完全缓存。它伤害的表现,当你无法缓存!

will cause iteration over many independently allocated inner vectors. And since CPU will only cache continuous memory, those small independent vectors cann't be cached altogether. It hurts performance when you can't cache!

那么,我们该怎么做是正确的?

So, how do we do it right?

我们根本就没有!当情况需要2维向量,我们只是以编程方式使用单维向量和访问它与偏移元素!这是我们如何做到这一点:

We simply don't! When situation calls for 2-dimensional vector, we just programmatically use single-dimensional vector and access it's elements with offsets! This is how we do it:

vector<int> dd_array(x * y);
dd_array[k * x + j] = 42; // equilavent of 2d dd_array[k][j]

这给了我们精彩的语法,性能和所有的荣耀。为了使我们的生活稍微好一点,我们甚至可以建立在一维矢量之上的适配器 - 但那是留给功课

This gives us wonderful syntax, performance and all the glory. To make our life slightly better, we can even build an adaptor on top of a single-dimensional vector - but that's left for the homework.

这篇关于如何正确地在C动态分配的多维数组工作++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆