一旦T数组衰减为指向T的指针,是否可以再次将其制成T数组? [英] Once an array-of-T has decayed into a pointer-to-T, can it ever be made into an array-of-T again?

查看:128
本文介绍了一旦T数组衰减为指向T的指针,是否可以再次将其制成T数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以让我们说我有一个数组:

So let us say I have an array:

int a[3] = { 1, 2, 3 };

现在,如果我要检查'a'的类型,则在我的机器上会得到:

Now if I were to check the type of 'a', on my machine I get:

cout<<typeid(a).name(); // prints 'A3_i'

现在,如果我使用地址"a",然后取消引用该地址,则类型不会更改(我真的很喜欢,因为在我看来,获得地址"和取消引用"是逆运算):

Now if I take the address of 'a', then dereference that address, the type does not change (which I really like, because in my mind 'taking the address' and 'dereferencing' are inverse operations):

cout<<typeid(*&a).name(); // also prints 'A3_i'

但是,如果我先取消引用'a',然后采用该地址,类型就会改变(我承认我很难忍受,因为当我取消引用数组时,我应该得到一个int,当我获取那个int的地址时,我应该得到一个指向int的指针,事实证明我知道):

However if I dereference 'a' first, then take the address of that, the type does change (which I admit I have a hard time not liking, because when I dereferenced the array I should get an int, and when I take the address of that int, I should get a pointer-to-int, and it turns out I do):

cout<<typeid(&*a).name(); // prints 'Pi'

这是我的两个问题:

1)一旦数组类型衰减为指针类型,是否有办法将其恢复为数组类型?

1) Once an array-type has decayed into a pointer-type, is there anyway to get it back to an array-type?

我尝试了一种明显的策略,就像只在乎你一样:

I tried the obvious strategy of casting-like-you-just-don't-care:

cout<<typeid( (int[3]) &*a).name(); // does not compile
// "error: ISO C++ forbids casting to an array type `int [3]'"

是否还有其他演员可以使用?还是这种转换严格禁止进入?

Is there another cast that would work? Or it is this type of conversion strictly off-limits?

2)是否可以返回到数组类型,究竟是什么信息在衰减指针过程中被切分并丢失?

2) Whether or not you can ever get back to the array-type, exactly what information is sliced and lost in the decay-to-pointer proccess?

我知道指针类型和数组类型不相等.我假设array-type是指针类型中存储的信息的严格超集.听起来不错吗?

I understand that a pointer-type and an array-type are not equivalent. I assume that the array-type is a strict superset of the information stored in the pointer-type. Does this sound right?

我在其他问题中读到,数组类型中的额外信息是:知道数组是否在堆栈上,以及数组的大小(必须以某种方式知道数组的大小,因为它是类型的一部分,对吧?).数组类型中还有其他信息吗?

I have read in other questions that the extra information in the array-type is: knowledge of whether or not the array is on the stack, and also its size (it must know the size of the array somehow, because it is part of the type, right?). Is there any other information hidden in the array-type?

推荐答案

我不确定这是否正是您要的内容,但是您可以使用类型转换来获取与原始类型相同的对象大批.这个想法是使用鲜为人知的类型的指向数组的指针和指向数组的引用来恢复信息.例如:

I'm not sure if this is quite what you're looking for, but you can use typecasting to get back an object with the same type as the original array. The idea is to use the little-known types pointer-to-array and reference-to-array to recover the information. For example:

char arr[137];
cout << sizeof(arr) << endl; // Prints 137
cout << sizeof(arr[0]) << endl; // Prints 1
cout << sizeof(&arr[0]) << endl; // Prints 4 (on my system)
cout << sizeof(*&arr[0]) << endl; // Prints 1
cout << sizeof((char (&) [137]) *&arr[0]) << endl; // Prints 137

这个想法是我们转换通过使用*&arr[0]键入char (&)[137]创建的引用,该引用是对137个字符的数组的引用.现在,引用具有这种类型,sizeof运算符知道应该打印137,因为137个字符的数组的大小的确是137.

The idea is that we typecast the reference created by using *&arr[0] to type char (&)[137], a reference to an array of 137 characters. Now that the reference has this type, the sizeof operator knows that it should print 137, since the size of an array of 137 characters is indeed 137.

但是,这仅在您将类型转换为正确的类型时才有效!例如,这是完全合法的:

However, this only works if you typecast to the correct type! For example, this is perfectly legal:

char arr[137];
cout << sizeof((char (&) [42]) *&arr[0]) << endl; // Prints 42

因此,您可以恢复信息,但是可以很容易地将信息弄错,并导致恢复错误信息的情况.

So you can recover the information, but you can easily get that information wrong and lead to a case where you've recovered the incorrect information.

同样,我不确定这是否是您想要的,但是它表明您确实可以使用强制转换来获取数组大小信息.

Again, I'm not sure if this is what you were looking for, but it shows that you can indeed use casting to get back the array size information.

这篇关于一旦T数组衰减为指向T的指针,是否可以再次将其制成T数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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