maximum possible allocation

E

Evangelista Sami

hello

is there a maximum size that can be allocated by a malloc call?
is this defined by the standard?

thanks

Sami Evangelista
 
J

Jens.Toerring

Evangelista Sami said:
is there a maximum size that can be allocated by a malloc call?

Yes. But that depends on your system, i.e. how much memory you have
and lots of details of your OS (some systems even let you allocate
much more memory than you have real memory, RAM and swap taken to-
gether, but will kill your process if you ever try to use it all).
is this defined by the standard?

No, only restriction is that the size can be stored in a number of
type size_t because of how malloc() is declared.

Regards, Jens
 
M

Mike Wahler

Evangelista Sami said:
hello

is there a maximum size that can be allocated by a malloc call?

Theoretically, as many bytes whose total number
can be expressed with type 'size_t'. But the
range of 'size_t' is implementation defined.
It must be an unsigned integer type, with a
minimum magnitude of 65535. The maximum single
allocation size possible is also limited by the
host operating system.

is this defined by the standard?

See above.

-Mike
 
E

Emmanuel Delahaye

In said:
is there a maximum size that can be allocated by a malloc call?

Can't be greater than the value of ((size_t)-1) of your implementation. That
said, non linear objects (arrays of pointers, lists, trees) can be much
bigger [1].
is this defined by the standard?

No. Sky's the limit. The standard only defines minima for the biggest
objetcts (allocated or static).

0x7FFF in C90
0xFFFFU in C99
 
D

Dan Pop

In said:
No. Sky's the limit. The standard only defines minima for the biggest
objetcts (allocated or static).

0x7FFF in C90
0xFFFFU in C99
^
Is this U supposed to make any difference? What and why?

If you use calloc to ask for an object whose size exceeds SIZE_MAX you're
in undefined behaviour territory.

Dan
 
D

Dan Pop

In said:
is there a maximum size that can be allocated by a malloc call?
is this defined by the standard?

The standard guarantees that at least one object of 32767 bytes can be
allocated, but it doesn't guarantee that it can be *dynamically*
allocated, or even that each and every program can allocate such an
object (think of an implementation with a 16-bit address space where
the text segment already exceeds 32 kB).

An implementation where *all* malloc (and friends) calls fail all the
time is still perfectly conforming. Even malloc(0) is allowed to return
a null pointer, so the answer to your question is not even zero ;-)

OTOH, if you're asking about what is the maximum value that can be
requested from a malloc call (with no guarantees of success), the
answer is (size_t)-1 (or SIZE_MAX in C99).

As a side note, C99 attempts to exclude platforms with a 16-bit
address space from having hosted implementations, as the one
allocatable object size is increased to 65535. For no redeeming
benefits, AFAICS.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top