Question

malloc defined like below:

void *malloc(size_t size);

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


size_t definition (stddef.h):

size_t: Unsigned integer type of the result of the sizeof operator. 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):

Limit of 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?

Was it helpful?

Solution

SIZE_MAX must be at least 65535. If you're running something like MS-DOS, chances are it'll actually even be that small. 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 (232-1, to be more exact).

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. In all honesty, however, for most people that's simply irrelevant -- even relatively small portable systems (e.g., an iPod) can address far more memory than that any more. 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).

OTHER TIPS

The minimum value of SIZE_MAX is 65535 but it can (and usually is) be more.

On most non-embedded platforms, size_t is a typedef for unsigned long and SIZE_MAX is set to 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.

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

Include stdint.h to get the value of SIZE_MAX.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top