16 ms·
This doesn't make sense to me. You can't "allocate" more than SIZE_MAX bytes by definition. If you take "allocate" to mean "make it available in the process's a
by jstimpfle 4y ago
This doesn't make sense to me. You can't "allocate" more than SIZE_MAX bytes by definition. If you take "allocate" to mean "make it available in the process's address space", that is.
- unwind 4y agoAre you sure? The calloc() [1] function mentioned above takes two values of type size_t, and allocates their product bytes. I'm on mobile without (!) the C99 draft spec but at least the man page gives no such restriction. [1] https://linux.die.net/man/3/calloc https://linux.die.net/man/3/calloc
- mek6800d2 4y agoI read something about this recently, somewhere, maybe HN. Specifically, in calloc(), what is done and what should really be done if the multiplication overflows. As will happen, for example, if you try to calloc() two elements of size SIZE_MAX, when SIZE_MAX is the maximum representable unsigned integer value on the machine. So, I don't think calloc() is available or intended as a way to circumvent malloc()'s size restriction.
- Someone 4y agoI stand corrected. Initially, I thought that, even if it calloc can’t, an OS could provide a different way to obtain a pointer to a memory region that’s larger than SIZE_MAX. However, the standard says (https://en.cppreference.com/w/c/types/size_t https://en.cppreference.com/w/c/types/size_t): “size_t can store the maximum size of a theoretically possible object of any type (including array).” and (https://en.cppreference.com/w/c/language/pointer https://en.cppreference.com/w/c/language/pointer): “Pointer is a type of an object that refers to a function or an object of another type, possibly adding qualifiers. Pointer may also refer to nothing, which is indicated by the special null pointer value.” ⇒ pointers must either be null or point to an object, and objects aren’t larger than SIZE_MAX, so I think having a pointer pointing to a block larger than SIZE_MAX violates the standard.
- jstimpfle 4y ago> pointer pointing to a block larger than SIZE_MAX violates the standard. it's simply not possible, by definition.
- jstimpfle 4y agoHow would it be possible to allocate more address space than is addressable? calloc returns NULL when can't satisfy the request. The idea of taking two arguments is not to allow the user to specify a larger requested size, but to protect against overflows as it can happen with e.g. malloc() where the user has to compute the size of arrays by multiplying NUM_ELEMS * SIZE_PER_ELEM. And the user will normally do so less carefully than a library function.