Take the 2-minute tour ×

malloc defined like below:

void *malloc(size_t size);


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?

share|improve this question
see stackoverflow.com/questions/745152/… –  msam Apr 11 '12 at 15:56
as always you can do that. –  madper Apr 11 '12 at 16:02
size_t must be able to represent anything that sizeof may return. What is sizeof(int[(unsigned) -1]) on a 32 bit system? And on a 64 bit system? –  Damon Apr 11 '12 at 16:05

3 Answers 3

up vote 6 down vote accepted

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).

share|improve this answer
But POSIX definition says, limit, meaning maximum limit. Also why did they define it as 65535? If something depends on the data type size, generally the standard doesn't specify such explicit limits –  Pavan Manjunath Apr 11 '12 at 16:02
@PavanManjunath: I'm not sure about the wording in POSIX, but in the C standard it says (§7.18.3/2): "Its implementation-defined value shall be equal to or greater in magnitude (absolute value) than the corresponding value given below, with the same sign." So the 65535 is the minimum value allowed, not the maximum. –  Jerry Coffin Apr 11 '12 at 16:04
@PavanManjunath: You're simply misreading POSIX. Just like C, POSIX defines the "minimum maximum", i.e. the minimum value that an implementation is allowed to impose as the maximum. It's also probably a mistake that POSIX still allows SIZE_MAX to be so small, since (1) POSIX now requires int to be at least 32-bit, and (2) it's impossible to implement POSIX in under 64k, much less have room left for an application to run. –  R.. Apr 11 '12 at 17:44

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.

share|improve this answer
Do you mean I should check SIZE_MAX before memory allocation (malloc)? –  Amir Saniyan Apr 11 '12 at 15:56
If you are using a 32-bit or a 64-bit platform I think it is safe to assume SIZE_MAX is at least UINT_MAX. –  ouah Apr 11 '12 at 15:59
@AmirSaniyan: This is no more a problem than the general need for you to know whether your data types can actually represent the values that you want to put in them. You don't have to check anything per se, since a variable of type size_t will always have a 'valid' value, but of course you must understand whether or not you're overflowing anything. –  Kerrek SB Apr 11 '12 at 16:01

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.

share|improve this answer
The correct prefix for printing size_t is z in modern C, %zu or %zx. –  Jens Gustedt Apr 11 '12 at 18:14
Thanks. My compiler actually warned about wrong modifier. –  Albert Veli Apr 11 '12 at 21:33

Your Answer


