是否可以在 C++ 中动态创建恒定大小的数组? [英] Is it possible to dynamically create an array of constant size in C++?

查看:29
本文介绍了是否可以在 C++ 中动态创建恒定大小的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想向大家保证,我问这个问题是出于好奇.我的意思是,不要告诉我如果我需要这个,那么我的设计就有问题,因为我在实际代码中不需要这个.希望我说服了你 :) 现在问题来了:

First of all, I want to reassure you all that I am asking this question out of curiosity. I mean, don't tell me that if I need this then my design has problems because I don't need this in real code. Hope I convinced you :) Now to the question:

对于大多数类型 T 我们可以写

For most types T we can write

T* p = new T;

现在如果 T 是数组类型呢?

now what if T is an array type?

int (*p)[3] =  new ???; //pointer to array of 3 = new ???

我试过了:

typedef int arr[3];
arr* p = new arr;

但这不起作用.

对此是否有任何有效的语法,或者在 C++ 中是不可能的.如果不可能,那为什么?谢谢

Is there any valid syntax for this or it is impossible in C++. If it is impossible, then why? Thanks

编辑:我想我还不够清楚.我希望能够在这种情况下使用它:

Edit: i am guessing I wasn't clear enough. I want to be able to use it in this situation:

void f(int(&)[3]);
int (*p)[3] = new ???;
f(*p);

推荐答案

你不能这样做的原因是 new int[3] 已经准确地分配了你想要的类型的对象int[3].只是 new 表达式返回,是一个指向它的第一个元素的指针.5.3.4/1:

The reason you can't do it is that new int[3] already allocates exactly what you want, an object of type int[3]. It's just that what the new-expression returns, is a pointer to its first element. 5.3.4/1:

如果实体是非数组对象,new 表达式返回一个指针到创建的对象.如果它是一个数组,新表达式返回一个指向初始元素的指针数组.

If the entity is a non-array object, the new-expression returns a pointer to the object created. If it is an array, the new-expression returns a pointer to the initial element of the array.

返回指向第一个元素的指针是允许 3 在运行之前未知的原因,所以我想通过提前知道它,你已经绊倒了你没有使用的灵活性.

Returning a pointer to the first element is what allows the 3 to be unknown until runtime, so I suppose that by knowing it in advance, you've tripped over flexibility that you aren't using.

我想解决这个问题的方法是将 reinterpret_cast 重新解释为您想要的指针类型(不一定是可移植的),或者分配一个包含 int[3] 的结构(并使用指向其的指针数据成员).

I guess the ways around this are to reinterpret_cast back to the pointer type you want (not necessarily portable), or to allocate a struct containing an int[3] (and use a pointer to its data member).

而不是 delete.]

我想道德是,如果你写的模板天真地分配一些未知类型 Tnew,那么当有人传递数组类型时模板将不起作用作为 T.你将把它分配给错误的指针类型,如果你修复它(也许用 auto),你就会错误地删除它.

I guess the moral is, if you write templates that naively allocate some unknown type T with new, then the template won't work when someone passes an array type as T. You'll be assigning it to the wrong pointer type, and if you fix that (perhaps with auto), you'll be deleting it wrongly.

编辑回答 j_kubik 的问题:

Edit in answer to j_kubik's question:

这是区分数组和非数组类型的一种方法.如果您编写这样的函数,该函数返回一个包含指针并能够正确删除它的对象,那么您就有了一个适用于任何类型 T 的通用 new/delete.

Here's one way to distinguish between array and non-array types. If you write a function like this, that returns an object that holds the pointer and is capable of correctly deleting it, then you have a generic new/delete for any type T.

#include <iostream>

template <typename T>
void make_thing_helper(T *) {
    std::cout << "plain version\n";
}

template <typename T, int N>
void make_thing_helper(T (*)[N]) {
    std::cout << "array version\n";
}

template <typename T>
void make_thing() {
    make_thing_helper((T*)0);
}

int main() {
    typedef int T1;
    typedef int T2[3];
    make_thing<T1>();
    make_thing<T2>();
}

这篇关于是否可以在 C++ 中动态创建恒定大小的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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