Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Amount OF Memory...,

Reply
Thread Tools

Amount OF Memory...,

 
 
Pranav
Guest
Posts: n/a
 
      08-17-2008
How Much memory is allocated by the MALLOC when we call the function?
Does it return only the amount requested or it returns memory by
rounding it to the multiple of minimum no of bytes of memory decided
by the implementation?

Thank you,
Pranav
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      08-17-2008
Pranav wrote:

> How Much memory is allocated by the MALLOC when we call the function?
> Does it return only the amount requested or it returns memory by
> rounding it to the multiple of minimum no of bytes of memory decided
> by the implementation?


There is no general answer. It will obviously vary between
implementations. Many modern implementations allocate memory as pages.
On such systems even a call to malloc(1) will allocate an entire page,
if there is no space in the previously allocated pages. Also malloc
needs to somehow record the size of each allocation to make free work
properly, which is also likely to consume a minimum of few bytes of
overhead.

 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      08-17-2008
In article <g89d80$4ab$(E-Mail Removed)>,
santosh <(E-Mail Removed)> wrote:

>> How Much memory is allocated by the MALLOC when we call the function?
>> Does it return only the amount requested or it returns memory by
>> rounding it to the multiple of minimum no of bytes of memory decided
>> by the implementation?


>There is no general answer. It will obviously vary between
>implementations. Many modern implementations allocate memory as pages.
>On such systems even a call to malloc(1) will allocate an entire page,
>if there is no space in the previously allocated pages.


As this answer shows, it depends what you mean by "allocate". There's
the amount of memory returned from malloc() - which might be exactly
the amouint asked for, or might be rounded up - and the amount of
space obtained from the operating system by the implementation, which
will almost certainly be in larger chunks, probably pages.

-- Richard

--
Please remember to mention me / in tapes you leave behind.
 
Reply With Quote
 
31349 83652
Guest
Posts: n/a
 
      08-17-2008
santosh wrote:
> Pranav wrote:
> > How Much memory is allocated by the MALLOC when we call the function?

>
> Many modern implementations allocate memory as pages.
> On such systems even a call to malloc(1) will allocate an entire page,


Whatever amount of memory malloc() returns (if any), you're not
allowed
(under penalty of UB) to access anything but what you asked for.

unsigned char *buf = malloc(42);
if (buf) {
buf[41] = '\0'; /* OK */
buf[42] = '\0'; /* UB, even if malloc returned a 4096 byte page
*/
free(buf);
}

At least I think so
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      08-17-2008
"Malcolm McLean" <(E-Mail Removed)> writes:

> "Pranav" <(E-Mail Removed)> wrote in message news:
>> How Much memory is allocated by the MALLOC when we call the function?
>> Does it return only the amount requested or it returns memory by
>> rounding it to the multiple of minimum no of bytes of memory decided
>> by the implementation?
>>

> Almost always the amount will be rounded up. Fast allocation
> algorithms work by rounding up small requests to the nearest power of
> two.


Sounds very unlikely. That would map 1->1, 2->2, 3->4, etc.

--
Ben.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-17-2008
"Malcolm McLean" <(E-Mail Removed)> writes:
> "Pranav" <(E-Mail Removed)> wrote in message news:
>> How Much memory is allocated by the MALLOC when we call the function?
>> Does it return only the amount requested or it returns memory by
>> rounding it to the multiple of minimum no of bytes of memory decided
>> by the implementation?
>>

> Almost always the amount will be rounded up. Fast allocation
> algorithms work by rounding up small requests to the nearest power of
> two. For big allocations you might as well allocate whole pages. I
> don't actually know how any common system manages big allocations
> internally, but it probably makes management easier to keep the
> allocation as whole pages beyond a certain size.


Are you suggesting that, for large allocations, the value passed to
malloc() might as well be a multiple of the page size?

First, there's no portable way to know what the page size is, even if
there is such a thing.

Second, suppose the page size is 1024 bytes. The implementation will
typically need some extra space to keep track of allocations. If you
need 1000 bytes and decide to call malloc(1024), the implementation
might be forced to allocate 2048 bytes; if you had just called
malloc(1000), the metadata could have been stored in the extra 24
bytes.

If the metadata isn't stored contiguously with the allocated memory,
this probably won't apply, but it's still the kind of
micro-optimization that's better left to the implementation. If you
need 999 bytes, just call malloc(999).

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      08-17-2008
"Malcolm McLean" <(E-Mail Removed)> writes:

> "Ben Bacarisse" <(E-Mail Removed)> wrote in message
>> "Malcolm McLean" <(E-Mail Removed)> writes:
>>
>>> Almost always the amount will be rounded up. Fast allocation
>>> algorithms work by rounding up small requests to the nearest power of
>>> two.

>>
>> Sounds very unlikely. That would map 1->1, 2->2, 3->4, etc.
>>

> If the page size is a power of two, then you can do a jigsaw with
> powers of two, and avoid fragmentation or long searches. 1 is fine as
> a jigsaw piece size.


I must be missing how the rounding helps. If 1 and 2 are fine, why
not 3?

--
Ben.
 
Reply With Quote
 
soscpd
Guest
Posts: n/a
 
      08-17-2008
Hello List

Well... this question come in handy. If I have, say, one n dimension
array who points to some struct who, on his initialization time,
points to another struct, like (Just a sample):


typedef struct
{
char ****args;
int **index_args;
} arguments;

typedef struct
{
char ***values;
int *index_values;
} what;

struct
{
what ***ever;
/* ok... I have other stuff in here, but the main question point
is illustrated */
} mystruct;

What should be the best practice on memory allocation? All in one time
or each element, one by one?

To know better what a hell is all that about, I have x elements(char
***values). Each value should receive n arguments (before runtime, and
even on runtime - since some values should change from network input -
I don't know the number of arguments for each value. All must be
dynamic).

Lets say I have:

value[0][index_values[0]]="%s %s %s.\n";

On args, I need:

args[0][index_values[0]][index_args[0][0]]="The";

args[0][index_values[0]][index_args[0][1]]="final";

args[0][index_values[0]][index_args[0][2]]="string";

I'm trying to make this sample very very simple, but this can create a
problem. The application looks too weird to work out this simple task.
In the main context (is not here), this way make more sense (at least
to me, so far .

To:

int myvprintf(const char *format, ...)
{
int rv=0;
va_list arguments_list;

va_start(arguments_list,format);

rv=vprintf(format,arguments_list); /* vprintf here, but I'm using
vsprintf in the real code */

va_end(arguments_list);

if(rv<0)
return EXIT_FAILURE;

return EXIT_SUCCESS;
}


Thanks
Rafael
 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      08-18-2008
On Aug 18, 12:34 am, Ben Bacarisse <(E-Mail Removed)> wrote:
> "Malcolm McLean" <(E-Mail Removed)> writes:
> > "Ben Bacarisse" <(E-Mail Removed)> wrote in message
> >> "Malcolm McLean" <(E-Mail Removed)> writes:

>
> >>> Almost always the amount will be rounded up. Fast allocation
> >>> algorithms work by rounding up small requests to the nearest power of
> >>> two.

>
> >> Sounds very unlikely. That would map 1->1, 2->2, 3->4, etc.

>
> > If the page size is a power of two, then you can do a jigsaw with
> > powers of two, and avoid fragmentation or long searches. 1 is fine as
> > a jigsaw piece size.

>
> I must be missing how the rounding helps. If 1 and 2 are fine, why
> not 3?


Because 3 is two jigsaw pieces while 1 and 2 are one.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      08-18-2008
(E-Mail Removed) writes:

> On Aug 18, 12:34 am, Ben Bacarisse <(E-Mail Removed)> wrote:
>> "Malcolm McLean" <(E-Mail Removed)> writes:
>> > "Ben Bacarisse" <(E-Mail Removed)> wrote in message
>> >> "Malcolm McLean" <(E-Mail Removed)> writes:

>>
>> >>> Almost always the amount will be rounded up. Fast allocation
>> >>> algorithms work by rounding up small requests to the nearest power of
>> >>> two.

>>
>> >> Sounds very unlikely. That would map 1->1, 2->2, 3->4, etc.

>>
>> > If the page size is a power of two, then you can do a jigsaw with
>> > powers of two, and avoid fragmentation or long searches. 1 is fine as
>> > a jigsaw piece size.

>>
>> I must be missing how the rounding helps. If 1 and 2 are fine, why
>> not 3?

>
> Because 3 is two jigsaw pieces while 1 and 2 are one.


I have no idea if that is supposed to be an "aha!" moment or a joke!
Either way, due to the off-topic drift, maybe you could just point me
at something that explains what you think I have missed.

--
Ben.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Clicking a "mailto:" link on a page opens huge amount of IE windows after a while Henk Jol Firefox 1 01-04-2005 10:13 PM
Determining the amount of Memory J R Cisco 2 09-02-2004 10:20 PM
PIX 515E and amount of user supported Rachid Berrada Cisco 1 12-11-2003 04:50 PM
Amount of data Jenny ASP .Net 0 07-29-2003 10:45 AM
How to send a big amount of data from an aspx page to another Simon ASP .Net 3 07-03-2003 08:01 PM



Advertisments