Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: malloc and maximum size |
| Date | 2011-10-14 11:05 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <4E984FB1.5030903@verizon.net> (permalink) |
| References | <dc9e20a5-6a6b-4a1c-b2d2-5c8bcc50c812@a12g2000vbz.googlegroups.com> |
On 10/14/2011 09:31 AM, V.Subramanian, India wrote:
> This question is only for understanding purpose.
>
> I am using RedHat Linux kenel 2.6.9 on Intel Pentium D dual core
> processor.
>
> The malloc function takes size_t type argument. I am using gcc 3.4.3
> implementation. Under this implementation, in stdint.h, SIZE_MAX is
> defined as follows:
> # define SIZE_MAX (4294967295U)
>
> SIZE_MAX, defined in stdint.h, is the largest value that can be
> represented in type size_t. However when I pass SIZE_MAX as the
> argument to malloc, it is unable to allocate SIZE_MAX bytes of memory.
> In fact, malloc allocates much much less memory.
This is entirely normal. The implementation chose size_t to be big
enough for all of the purposes it needs to be useful for - primarily the
type of sizeof expressions, and for dynamic memory allocations, but
there's many other ways the standard library uses it.
The type chosen for size_t must be an unsigned type big enough to hold
the largest number that could reasonably be used for any of those
purposes; but keep in mind that most platforms nowadays provide hardware
support only for integers of 8, 16, 32, or 64 bytes (and not necessarily
all of those on any given machine); types of other sizes could be
implemented by software emulation, but would be very inefficient. The
maximum amount of memory allocatable by malloc() could be as small as
65536 bytes, and still be large enough to require size_t to be a 32-bit
type.
Moral: don't assume that you can allocate as much as SIZE_MAX bytes; the
actual maximum could be a LOT less.
> Here is the complete program max_size_to_malloc.c
>
> #include <stdlib.h>
> #include <stdio.h>
> #include <stdint.h>
> #include <stddef.h>
>
> int main()
> {
> size_t count = 0;
>
> do
> {
> const size_t size = SIZE_MAX - count;
>
> char* p = malloc(size);
>
> if (p)
> {
> fprintf(stderr,
> "%u - %u : ",
> SIZE_MAX,
> count);
>
> fprintf(stderr,
> "Allocated %u bytes of memory\n",
> size);
> free(p);
> p = NULL;
> return EXIT_SUCCESS;
> }
>
> } while (++count);
Keeping separate track of count and size serves little purpose. I'd
recommend dropping count, dropping the first fprintf() call, and
replacing the do-while() loop with
for(size_t size = SIZE_MAX; size; size--)
Also, setting p=NULL is pretty pointless when the lifetime of 'p' will
end almost immediately after you do so. For precisely that reason, a
decently optimizing compiler will simply ignore that statement, but it's
better to simply not write it in the first place. In fact, I normally
try to arrange that pointer variables are free()d immediately before
their lifetime ends, so I won't have to worry about them being misused.
> }
>
> This program compiles fine with gcc 3.4.3 as
> gcc -std=c99 -pedantic -Wall -Wextra max_size_to_malloc.c
>
> and produces the following output(after quite some time):
> 4294967295 - 1365266451 : Allocated 2929700844 bytes of memory
>
> What is the reason for allocating this much less memory while the
> parameter type of malloc is size_t ?
>
> Normally what factor drives the maximum size of a single chunk of
> memory allocated by malloc ?
Memory fragmentation can make the maximum size of a single allocatable
chunk of memory be MUCH smaller than the total amount of memory
available for allocation.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
malloc and maximum size "V.Subramanian, India" <subramanian100in@yahoo.com> - 2011-10-14 06:31 -0700
Re: malloc and maximum size gordonb.pey1d@burditt.org (Gordon Burditt) - 2011-10-14 09:45 -0500
Re: malloc and maximum size Lowell Gilbert <lgusenet@be-well.ilk.org> - 2011-10-14 11:10 -0400
Re: malloc and maximum size James Kuyper <jameskuyper@verizon.net> - 2011-10-14 11:40 -0400
Re: malloc and maximum size Lowell Gilbert <lgusenet@be-well.ilk.org> - 2011-10-14 15:00 -0400
Re: malloc and maximum size James Kuyper <jameskuyper@verizon.net> - 2011-10-14 15:17 -0400
Re: malloc and maximum size Lowell Gilbert <lgusenet@be-well.ilk.org> - 2011-10-14 15:46 -0400
Re: malloc and maximum size James Kuyper <jameskuyper@verizon.net> - 2011-10-14 16:38 -0400
Re: malloc and maximum size Ike Naar <ike@iceland.freeshell.org> - 2011-10-14 21:59 +0000
Re: malloc and maximum size James Kuyper <jameskuyper@verizon.net> - 2011-10-14 18:26 -0400
Re: malloc and maximum size Lowell Gilbert <lgusenet@be-well.ilk.org> - 2011-10-14 18:42 -0400
Re: malloc and maximum size Keith Thompson <kst-u@mib.org> - 2011-10-14 16:27 -0700
Re: malloc and maximum size Richard Damon <news.x.richarddamon@xoxy.net> - 2011-10-14 20:41 -0400
Re: malloc and maximum size Keith Thompson <kst-u@mib.org> - 2011-10-14 18:31 -0700
Re: malloc and maximum size Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-10-15 02:58 +0100
Re: malloc and maximum size Keith Thompson <kst-u@mib.org> - 2011-10-14 22:11 -0700
Re: malloc and maximum size James Kuyper <jameskuyper@verizon.net> - 2011-10-14 22:59 -0400
Re: malloc and maximum size Richard Damon <news.x.richarddamon@xoxy.net> - 2011-10-14 23:44 -0400
Re: malloc and maximum size Keith Thompson <kst-u@mib.org> - 2011-10-14 22:56 -0700
Re: malloc and maximum size James Kuyper <jameskuyper@verizon.net> - 2011-10-15 07:52 -0400
Re: malloc and maximum size Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 16:54 -0800
Re: malloc and maximum size Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 16:51 -0800
Re: malloc and maximum size James Kuyper <jameskuyper@verizon.net> - 2011-10-14 22:51 -0400
Re: malloc and maximum size Keith Thompson <kst-u@mib.org> - 2011-10-14 22:48 -0700
Re: malloc and maximum size James Kuyper <jameskuyper@verizon.net> - 2011-10-15 07:54 -0400
Re: malloc and maximum size Keith Thompson <kst-u@mib.org> - 2011-10-15 11:25 -0700
Re: malloc and maximum size James Kuyper <jameskuyper@verizon.net> - 2011-10-15 21:21 -0400
Re: malloc and maximum size Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 17:41 -0800
Re: malloc and maximum size Kaz Kylheku <kaz@kylheku.com> - 2012-01-25 02:11 +0000
Re: malloc and maximum size Richard Damon <news.x.richarddamon@xoxy.net> - 2011-10-15 11:50 -0400
Re: malloc and maximum size James Kuyper <jameskuyper@verizon.net> - 2011-10-15 21:38 -0400
Re: malloc and maximum size Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 17:33 -0800
Re: malloc and maximum size Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 17:06 -0800
Re: malloc and maximum size Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 16:58 -0800
Re: malloc and maximum size Kaz Kylheku <kaz@kylheku.com> - 2012-01-25 01:23 +0000
Re: malloc and maximum size Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-31 16:31 -0800
Re: malloc and maximum size James Kuyper <jameskuyper@verizon.net> - 2011-10-14 22:20 -0400
Re: malloc and maximum size Keith Thompson <kst-u@mib.org> - 2011-10-14 16:03 -0700
Re: malloc and maximum size James Kuyper <jameskuyper@verizon.net> - 2011-10-14 23:01 -0400
Re: malloc and maximum size Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 17:35 -0800
Re: malloc and maximum size Kaz Kylheku <kaz@kylheku.com> - 2012-01-25 02:33 +0000
Re: malloc and maximum size Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-31 17:14 -0800
Re: malloc and maximum size Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 16:45 -0800
Re: malloc and maximum size Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 16:32 -0800
Re: malloc and maximum size Keith Thompson <kst-u@mib.org> - 2012-01-25 02:40 -0800
Re: malloc and maximum size Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-31 17:27 -0800
Re: malloc and maximum size Richard Damon <news.x.richarddamon@xoxy.net> - 2011-10-14 16:17 -0400
Re: malloc and maximum size James Kuyper <jameskuyper@verizon.net> - 2011-10-14 16:52 -0400
Re: malloc and maximum size Keith Thompson <kst-u@mib.org> - 2011-10-14 16:12 -0700
Re: malloc and maximum size James Kuyper <jameskuyper@verizon.net> - 2011-10-14 23:04 -0400
Re: malloc and maximum size Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 16:36 -0800
Re: malloc and maximum size Ike Naar <ike@iceland.freeshell.org> - 2011-10-14 16:49 +0000
Re: malloc and maximum size James Kuyper <jameskuyper@verizon.net> - 2011-10-14 11:05 -0400
Re: malloc and maximum size Nobody <nobody@nowhere.com> - 2011-10-14 16:11 +0100
Re: malloc and maximum size Kenneth Brody <kenbrody@spamcop.net> - 2011-10-14 12:24 -0400
Re: malloc and maximum size jt@toerring.de (Jens Thoms Toerring) - 2011-10-14 17:10 +0000
Re: malloc and maximum size Stephen Sprunk <stephen@sprunk.org> - 2011-10-14 13:43 -0500
Re: malloc and maximum size Keith Thompson <kst-u@mib.org> - 2011-10-14 12:03 -0700
Re: malloc and maximum size jt@toerring.de (Jens Thoms Toerring) - 2011-10-14 20:36 +0000
Re: malloc and maximum size Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 16:26 -0800
Re: malloc and maximum size Seebs <usenet-nospam@seebs.net> - 2011-10-17 17:21 +0000
Re: malloc and maximum size Shao Miller <sha0.miller@gmail.com> - 2012-01-24 23:49 -0500
Re: malloc and maximum size Shao Miller <sha0.miller@gmail.com> - 2012-01-24 23:55 -0500
csiph-web