简体   繁体   English

在C ++中动态分配指向数组的指针

[英]Dynamically allocate pointer to array in C++

I have long used pointers to arrays in C programs of the form: 我长期使用以下形式的指向C程序中数组的指针:

int (*myarray)[2] = (int (*)[2]) malloc(n*sizeof(int[2]));

However, how can I do this in C++ using new? 但是,如何使用new在C ++中做到这一点? Can I do this? 我可以这样做吗?

int (*myarray)[2] = (int (*)[2]) new int[n][2];

EDIT: 编辑:

Looks like my original post was incomplete and confusing. 看起来我的原始帖子不完整且令人困惑。 Here is a code snippet that I compiled and tested which appears to do the right thing but I wanted to confirm from C++ experts that I was using an appropriate C++ construct. 这是我编译和测试的代码片段,看起来似乎是对的,但是我想向C ++专家确认我正在使用适当的C ++构造。

#include <iostream>

int main() {

  int n=5;
  int (*A)[2] = new int[n][2];

  for (int i = 0; i < n; i++)
    for (int j = 0; j < 2; j++)
      A[i][j] = 2*i+j;

  for (int i = 0; i < n; i++)
    std::cout << A[i][0] << " " << A[i][1] << "\n";

  delete myarray;

} }

The joys of using C++ and STL is you get a vector class that provides array like behaviour. 使用C ++和STL的乐趣在于,您可以获得一个提供类似行为的数组的向量类。 This also makes it easier to manage and read... 这也使管理和阅读变得更加容易。

std::vector< std::vector<int> > myarray(n);

If you don't want to use the STL then there is always... 如果您不想使用STL,那么总会有...

typedef int intarray[2];
intarray* ints = new intarray[n];
ints[0][0] = 1;
...
ints[n-1][1] = 6;

I personally would write an extra line of code if it made the code easier to read. 如果使代码更易于阅读,我个人会写一行额外的代码。

I think the clearest way to do what you ask for is to use a typedef for the array: 我认为做您要求的最清晰的方法是对数组使用typedef:

typedef int array_t[2];
array_t* yourarray = new array_t[n];

Don't do that though, because it requires doing manual memory management and that tends to be tedious, error-prone and brittle, in particular with respect to exception safety. 但是不要这样做,因为它需要手动进行内存管理,而且这往往很乏味,容易出错且易碎,特别是在异常安全方面。 Instead, take a look at the std::array class template (new in C++11, but otherwise available via Boost) and at the std::vector class template. 相反,请看一下std::array类模板(C ++ 11中的新增功能,但通过Boost也可以使用)和std::vector类模板。

To clarify the different between storing std::vector and std::array in a container, the latter is typically more efficient when there is a small and fixed number of elements involved. 为了阐明在容器中存储std::vectorstd::array之间的区别,当涉及的元素数量少且固定时,后者通常会更有效。 The reason is that the array class doesn't allocate things dynamically as the vector does. 原因是数组类不能像向量那样动态地分配事物。 For that, vector needs three pointers (beginning, end of used storage and end of allocated storage) plus of course the storage for the data itself (plus maybe some overhead induced by the allocator) all of which need to be loaded into the CPU cache for use. 为此,vector需要三个指针(开始,已用存储的结尾和已分配存储的结尾)加上数据本身的存储(当然还有分配器引起的一些开销),所有这些指针都需要加载到CPU缓存中用来。 Considering an LP64 system, that would require 32 bytes to store 8 bytes of data, compared to just 8 bytes using std::array . 考虑到LP64系统,与使用std::array仅为8个字节相比,将需要32个字节来存储8个字节的数据。

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

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