将新的前pression再回到一个指针数组? [英] Will the new expression ever return a pointer to an array?

查看:147
本文介绍了将新的前pression再回到一个指针数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此 href=\"http://stackoverflow.com/a/1810295/183120\">出色的答案,他解释说,在C,当一个函数需要一个数组,其尺寸为已知在编译时,这是一个重大的技术级别的错误声明

In this excellent answer by AndreyT, he explains that in C, when a function needs an array whose dimension is known at compile-time, it's a major technique-level error to declare

void process_array(int *ptr, size_t plen);

而不是

void process_array(int (*arr_ptr)[10]);

此外,认为该项许多程序员都无视第二个选项,知道只有第一。其中一个原因,他写道,对于这种行为是当一个数组需要动态分配并传递给第二个版本,程序员不知道如何做到这一点;他们是如此习惯于为int * p =的malloc(sizeof的(* P)* 10)返回一个为int * 。在C语言中,要做到这一点,正如他显示

Furthermore, he opines that many programmers are oblivious to the second option and know only about the first. One of the reasons, he writes, for this behaviour is when an array needs to be dynamically allocated and passed to the second version, programmers don't know how to do it; they're so accustomed to int *p = malloc(sizeof(*p) * 10) which returns an int *. In C, the way to do it is, as he shows

int (*arr_ptr) [10] = malloc(sizeof(*arr_ptr));


这让我思考的人会怎么做同样的在C ++中。我知道我们已经的std ::阵列的std ::矢量等,但我有兴趣了解的使用情况。于是,我试着这样做:


This got me thinking on how one would do the same in C++. I know we've std::array, std::vector, etc. but I'm interested in understanding new's usage. So I tried doing this:

typedef int Array10[10];
Array10 *p = new Array10;    // error: cannot convert ‘int*’ to ‘int (*)[10]’

当我改变的 P 类型为int * 编译器(GCC 4.8.1),是幸福的。我抬起头,C ++标准的11(n3337草案,§5.3.4/ 5 ),这进一步明白了,它说:

When I change p's type to int* the compiler (GCC 4.8.1) is happy. I looked up C++11 standard (draft n3337, §5.3.4/5) to understand this further, it says:

当分配的对象是一个数组(也就是 noptr新说明符的语法使用或新型-ID 的或的类型 - ID 的表示数组类型),在新-EX pression 的产生数组的指针初始元素(如果有的话)。 [注:的两个新INT 新建INT [10] 有型的为int * 并类型<强>新INT [I] [10] 为 INT(*)[10] - 注完的]

When the allocated object is an array (that is, the noptr-new-declarator syntax is used or the new-type-id or type-id denotes an array type), the new-expression yields a pointer to the initial element (if any) of the array. [Note: both new int and new int[10] have type int* and the type of new int[i][10] is int (*)[10]end note]

据我所知,新INT [10] 是行动;我回去应该使用释放删除[] P ,而不是删除P 。不过我需要什么,似乎是后者,这里的分配不是一个整数数组,但数组本身,作为一个单/整个对象。

I understand that new int [10] is in action; what I get back should be freed using delete [] p and not delete p. However what I need seems to be the latter, where the allocation is not an array of integers but an array itself, as a single/whole object.

有没有办法做到这一点?还是我试图做到这一点本身就表明C ++类型系统的误会?或者,它是正确的,但根本就没有被标准所允许的?

Is there a way to do it? Or my trying to do this in itself shows a misunderstanding of the C++ type system? Or it is right but is simply not allowed by the standard?

旁白::当一个函数需要一个数组,其大小是固定的,如选项2,恕我直言为调用函数做的最好的事情就是声明一个自动数组,而不是诉诸动态分配一个和担心清理。

Aside: When a function takes an array whose size is fixed, as in option 2, IMHO the best thing to do for the calling function is to declare an automatic array instead of resorting to dynamically allocating one and worrying about clean-up.

推荐答案

是的,事实上它是在建议你报的标准文本:

Yes, in fact it is suggested in the standard text you quoted:

int (*arr_ptr)[10] = new int[1][10];

由新-EX pression返回的指针的类型 INT(*)[10] ,即指向10个整数数组。您需要删除[] 将其删除。

The pointer returned by the new-expression has type int (*)[10], i.e. pointer to array of 10 ints. You need delete[] to delete it.

这篇关于将新的前pression再回到一个指针数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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