什么是“目标平台上最大可能物体的尺寸"?根据size_t [英] What is "size of the largest possible object on the target platform" in terms of size_t

查看:133
本文介绍了什么是“目标平台上最大可能物体的尺寸"?根据size_t的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读C/C ++中有关size_t的文章 http://web.archive.org/web/20081006073410/http://www.embedded.com/columns/programmingpointers/200900195 (通过Stackoverflow找到的链接).

I am reading article about size_t in C/C++ http://web.archive.org/web/20081006073410/http://www.embedded.com/columns/programmingpointers/200900195 (link found through Stackoverflow).

文章引用:

type size_t是一个typedef,它是一些无符号整数的别名 类型,通常为unsigned int或unsigned long,但可能甚至 无符号长久.每个标准C实现都应该 选择足够大但不大于的无符号整数 需要-表示广告上最大可能对象的大小 目标平台.

Type size_t is a typedef that's an alias for some unsigned integer type, typically unsigned int or unsigned long, but possibly even unsigned long long. Each Standard C implementation is supposed to choose the unsigned integer that's big enough--but no bigger than needed--to represent the size of the largest possible object on the target platform.

如何确定机器上最大可能物体的大小?

How can I determine the the size of the largest possible object on my machine ?

什么会影响最大对象的大小(除了处理器之外)?

What affect the size of the largest object (aside from the processor) ?

欢迎提供详细说明的链接.

Link on detailed explanation are welcomed.

推荐答案

我认为重要的是要考虑这种类型并不严格意味着您可以拥有该大小的对象-只是它是一个整数,即足够大以容纳最大可能的对象的大小-这并不意味着您可以使用SIZE_MAX分配内存.它只是保证最大的对象不能大于SIZE_MAX.

I think it's important to consider that this type doesn't strictly mean that you CAN have an object of that size - just that it's an integer that is LARGE ENOUGH to hold the size of the largest possible object - that doesn't mean that you can use SIZE_MAX to allocate memory. It just guarantees that the largest possible object can not be LARGER than SIZE_MAX.

这是由编译器的实现决定的体系结构决定(通常又基于编译器针对的操作系统,但是该操作系统可以提供比编译器更多的功能,或者编译器可以支持的理论量更多超出了操作系统的允许范围,只是它会在您要求时失败).

This is an architectural decision by the implementation of the compiler (typically in turn based on the OS that the compiler is targetting, but the OS could offer MORE than the compiler does, or the compiler could support a theoretical amount that is more than the OS allows, just that it will fail when you ask for it).

实际上,几乎总是由处理器来确定这一点-size_t几乎总是与处理器的位匹配-例如在32位处理器中为32位,在64位处理器中为64位.但是有可能设计一个在64位处理器上为32位的系统-一个对象"不能大于4GB并不是一个很大的限制.这仅表示您不能使用int的单个矢量来填充超过4GB,因此vector中的条目(或4G char条目)不能超过1G.

In practical terms, it is nearly always the processor that determines this - size_t nearly always matches the bitness of the processor - e.g. it's 32 bits in a 32-bit processor, and 64 bits in a 64-bit processor. But it would be possible to design a system where it is 32-bits on a 64-bit processor - one "object" can't be bigger than 4GB isn't that big a limitation, really. It just means that you can't use one single vector of int to fill more than 4GB, so no more than 1G entries in the vector (or 4G char entries).

当然,另一个限制因素是可用内存-如果您有一台非常老的机器具有256MB的RAM,即使size_t允许,也不会分配4GB的内存.但是给同一台计算机更多的内存,您可以使用更大的内存.

Of course, the other limiting factor is available memory - if you have a very old machine with 256MB of RAM, it's not going to allow you to allocate 4GB, even if the size_t allows it. But give the same machine more memory, and you can go to a much larger size.

在许多32位系统上,应用程序允许的最大内存小于4GB(完整的32位范围),因为内存的某些部分被保留"用于其他用途.同样,size_t是32位,因此允许4GB,但是它实际上不支持单个应用程序使用的全部内存-另一方面,一台32位计算机可能具有更多的存储空间. 4GB的RAM,并在多个应用程序之间分配它.

On many 32-bit systems, the maximum memory allowed for an application is less than 4GB (the full 32-bit range), because some portion of memory is "reserved" for other uses. So again, the size_t is 32 bits, so would allow 4GB, but it doesn't actually support the full amount of memory to be used by a single application - on the other hand, a 32-bit machine could have more than 4GB of RAM, and dole it out between multiple applications.

此外,如果由于某些体系结构原因而将系统限制为16MB内存,则size_t很可能仍是32位无符号整数-因为大多数处理器不执行24位整数[某些DSP可以做到这一点,但常规的16位或32位处理器则不能].

Also, if the system was limited (for some architectural reasons), say, to 16MB of memory, size_t is most likely still a 32-bit unsigned integer - because most processors don't do 24-bit integers [some DSP's may do that, but regular 16 or 32 bit processors don't].

这篇关于什么是“目标平台上最大可能物体的尺寸"?根据size_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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