声明使用为size_t最大的数组 [英] declaring the largest array using size_t

查看:112
本文介绍了声明使用为size_t最大的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要声明一个非常大的数组。我发现一个数组的最大尺寸为size_t,它被定义为UINT_MAX

i wanted to declare a very large array. i found that the max size of an array is size_t, which is defined as UINT_MAX

所以我写了code这样的

so i wrote the code like this

int arr[UINT_MAX];

当我编译这一点,它说溢出阵列尺寸

when i compile this, it says overflow in array dimension

但是当我这样写

size_t s = UINT_MAX;
int arr[s]; 

它正确编译。
有什么区别

it compiles properly. what's the difference

推荐答案

第一个错误:为size_t 不一定 unsigned int类型,因此它的最大值可以从一个不同的 unsigned int类型 UINT_MAX );此外,在C ++获得有关你应该使用类型的限制信息的std :: numeric_limits

First error: size_t is not necessarily unsigned int, thus its maximum value can be different from the one of unsigned int (UINT_MAX); moreover, in C++ to get informations about the limits of a type you should use std::numeric_limits.

#include <limits>

size_t s=std::numeric_limits<size_t>::max();

第二个错误:你永远不会得到一个数组那么大;因为为size_t 要求是能恩preSS任何物体上,它或许应该足够大,前preSS的对象大到整体的最大规模可用的应用程序,但是尝试分配这么大的对象地址空间将需要奉献的全部的地址空间吧,这是不可行的;此外,你的要求 INT 的数组s表示大,这意味着,这将是 UINT_MAX *的sizeof(INT)字节大,这可能会是4倍左右整个地址空间 - 胡说八道 - 顺便说一句的sizeof(ARR)将无法给前preSS这样的对象的大小,和在一般的指针甚至不能达到该阵列的顶部。编译器检测到这些故障并做阻止你。

Second error: you won't ever get an array so big; since size_t is required to be able to express the biggest size of any object, it should probably big enough to express an object big as the whole address space available to the application, but trying to allocate such a big object would require to dedicate the whole address space to it, which is infeasible; moreover, you're requesting an array of ints that big, which means that it will be UINT_MAX*sizeof(int) bytes big, which will probably be about 4 times the whole address space - clearly nonsense - and by the way sizeof(arr) wouldn't be able to express the size of such object, and in general pointers couldn't even reach the top of that array. The compiler detects these faults and stop you from doing that.

此外,我推断你想分配堆栈上的东西,也就是通常要比所有的内存,应用程序可以使用更小,一般是不存在分配大数组(一个好主意您应该使用的堆)。

Moreover, I infer that you're trying to allocate that thing on the stack, that is usually much much smaller than all the memory that the application can use, and in general it's not a good idea to allocate big arrays there (you should use the heap for that).

第三个错误:所有分配内存没有意义。如果你有大内存需求,你应该在堆上分配的东西,而不是在栈,并分配只是你需要与操作系统和其他应用程序一起玩好内存(如果你的工作这最后的考虑不适用关于你在哪里运行的唯一应用程序嵌入式系统)。

Third error: allocating all that memory doesn't make sense. If you have big memory requirements, you should allocate stuff on the heap, not on the stack, and allocate just the memory you need to play along well with the OS and the other applications (this last consideration do not apply if you're working on embedded systems where you are the only application that is running).

在C ++中的第二个片段甚至不应该工作,因为,如果这件事是在栈上分配的,你要去非标准的,因为这将是一个VLA(C99中使用,但在当前和下一个强烈反对C ++标准)。然而,在这种情况下,code分配该数组是在运行时(沃拉斯一般是不固定的尺寸)使用,所以编译器检查并不明显做的(虽然我认为这件事情会被发现容易被优化,而如果VLA语义是不是从规则排列的不同,可以优化掉的VLA并设法使一个普通的数组=>这将失败我说同样的原因)。

The second snippet in C++ shouldn't even work, since, if that thing is allocated on the stack, you're going nonstandard, since it would be a VLA (available in C99 but strongly rejected from the current and the next C++ standard). However, in that case the code to allocate that array is used at runtime (VLAs in general are not fixed in dimensions), so the check for the compiler is not obvious to do (although I suppose that this thing could be spotted easily by the optimizer, which, if VLA semantic is not different from regular arrays, could optimize away the VLA and try to make a regular array => which would fail for the same reasons I stated).

长话短说:它是没有意义分配所有内存(你甚至无法处理),特别是在堆栈中。使用堆和分配你需要什么。如果您有特殊要求,您应该调查由操作系统提供的专用虚拟内存功能。

Long story short: it makes no sense to allocate all that memory (that you couldn't even address), especially on the stack. Use the heap and allocate just what you need. If you have special requirements, you should investigate the special virtual memory functions provided by your OS.

这篇关于声明使用为size_t最大的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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