简体   繁体   English

我可以根据C标准分配超过65535个字节吗?

[英]Can I allocate more than 65535 bytes according C standards?

malloc defined like below: malloc定义如下:

void *malloc(size_t size);

http://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html http://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html


size_t definition (stddef.h): size_t定义(stddef.h):

size_t: Unsigned integer type of the result of the sizeof operator. http://pubs.opengroup.org/onlinepubs/009604499/basedefs/stddef.h.html http://pubs.opengroup.org/onlinepubs/009604499/basedefs/stddef.h.html


But according this page, max limitation of size_t is 65535. (Section Limits of Other Integer Types): 但是根据此页面, size_t最大限制为65535。(其他整数类型的部分限制):

Limit of size_t: SIZE_MAX 65535 http://pubs.opengroup.org/onlinepubs/007904975/basedefs/stdint.h.html size_t的限制:SIZE_MAX 65535 http://pubs.opengroup.org/onlinepubs/007904975/basedefs/stdint.h.html


Does it mean I can not allocate more than 65535 bytes when I want to respect C standard? 是否要遵守C标准时不能分配超过65535个字节?

SIZE_MAX must be at least 65535. If you're running something like MS-DOS, chances are it'll actually even be that small. SIZE_MAX至少应为65535。如果您正在运行类似MS-DOS的程序,则实际上它可能会很小。 On a typical, reasonably current desktop computer (say, anything less than 10 years old) you can expect it to be larger, typically at least around 4 billion (2 32 -1, to be more exact). 在典型的,当前合理的台式计算机(例如,任何使用不到10年的台式计算机)上,您可以预期它会更大,通常至少为40亿左右(更确切地说,为2 32 -1)。

Whether you need to (try to) deal with a more limited system will depend on the range of targets to which you might care about porting your code. 是否需要(尝试)使用更受限的系统,将取决于您可能希望移植代码的目标范围。 If you really might need to deal with a 16-bit compiler on a system with less than, say, 1 megabyte of addressable memory, then you'll have to write your code with that in mind. 如果您真的可能需要在一个小于1 MB的可寻址内存的系统上处理16位编译器,则必须牢记这一点。 In all honesty, however, for most people that's simply irrelevant -- even relatively small portable systems (eg, an iPod) can address far more memory than that any more. 但是,老实说,对于大多数人而言,这根本无关紧要-即使是相对较小的便携式系统(例如iPod)也可以寻址更多的内存。 OTOH, if you're writing code for a singing greeting card, then yes, such limitations probably come with the territory (but in such cases, the standard is often something to treat more as a general guideline than an absolute law). OTOH,如果您正在为唱歌贺卡编写代码,那么是的,此类限制可能会随地区而定(但在这种情况下,该标准通常更多地被视为一般准则,而不是绝对法律)。

The minimum value of SIZE_MAX is 65535 but it can (and usually is) be more. SIZE_MAX的最小值是65535,但可以(通常是)更大。

On most non-embedded platforms, size_t is a typedef for unsigned long and SIZE_MAX is set to ULONG_MAX . 在大多数非嵌入式平台上, size_tunsigned longtypedef ,并且SIZE_MAX设置为ULONG_MAX

On a 32-bit platform SIZE_MAX is usually 2^32 - 1 and on a 64 bit platform it is 2^64 - 1. Check with a printf if unsure. 在32位平台上,SIZE_MAX通常为2 ^ 32-1,在64位平台上为2 ^ 64-1。如果不确定,请使用printf进行检查。

printf("sizeof size_t = %zx, SIZE_MAX = %zx\n", sizeof(size_t), SIZE_MAX);

Include stdint.h to get the value of SIZE_MAX. 包含stdint.h以获取SIZE_MAX的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM