Velocity Reviews > malloc() question...

# malloc() question...

Guest
Posts: n/a

 11-16-2010
Let's say I call malloc() like the following

char *a = malloc(20);

and then do

free(a);

Does 20 chars or 20 ints get freed?

Ian Collins
Guest
Posts: n/a

 11-16-2010
On 11/17/10 09:12 AM, Chad wrote:
> Let's say I call malloc() like the following
>
> char *a = malloc(20);
>
> and then do
>
> free(a);
>
> Does 20 chars or 20 ints get freed?

Why would 20 ints get freed when you have requested 20 chars? I guess
the answer would be "both" if sizeof(int) were 1!

--
Ian Collins

Guest
Posts: n/a

 11-16-2010
On Nov 16, 12:22*pm, Ian Collins <(E-Mail Removed)> wrote:
> On 11/17/10 09:12 AM, Chad wrote:
>
> > Let's say I call malloc() like the following

>
> > char *a = malloc(20);

>
> > and then do

>
> > free(a);

>
> > Does 20 chars or 20 ints get freed?

>
> Why would 20 ints get freed when you have requested 20 chars? *I guess
> the answer would be "both" if sizeof(int) were 1!
>

I thought 20 was ot type int.

Ian Collins
Guest
Posts: n/a

 11-16-2010
On 11/17/10 09:24 AM, Chad wrote:
> On Nov 16, 12:22 pm, Ian Collins<(E-Mail Removed)> wrote:
>> On 11/17/10 09:12 AM, Chad wrote:
>>
>>> Let's say I call malloc() like the following

>>
>>> char *a = malloc(20);

>>
>>> and then do

>>
>>> free(a);

>>
>>> Does 20 chars or 20 ints get freed?

>>
>> Why would 20 ints get freed when you have requested 20 chars? I guess
>> the answer would be "both" if sizeof(int) were 1!
>>

>
> I thought 20 was ot type int.

It is, but the parameter for malloc is size_t and the unit of the value
requested is bytes.

--
Ian Collins

Seebs
Guest
Posts: n/a

 11-16-2010
On 2010-11-16, Chad <(E-Mail Removed)> wrote:
> Let's say I call malloc() like the following

> char *a = malloc(20);

> and then do

> free(a);

> Does 20 chars or 20 ints get freed?

No.

The space that was allocated gets freed. It doesn't matter what you think
the pointer is; if it's the address malloc() returned, the whole space is
freed regardless.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

arnuld
Guest
Posts: n/a

 11-17-2010
> On Tue, 16 Nov 2010 20:52:39 +0000, Seebs wrote:

>> On 2010-11-16, Chad <(E-Mail Removed)> wrote:
>> Let's say I call malloc() like the following

>> char *a = malloc(20);
>> and then do

>
>> free(a);

>
>> Does 20 chars or 20 ints get freed?

> No.
>
> The space that was allocated gets freed. It doesn't matter what you
> think the pointer is; if it's the address malloc() returned, the whole
> space is freed regardless.

We have not passed anything to the free() except pointer and it still free
()s the allocated memory. It means free() knows that char* a has 20x8
bits (assuming char is 8 bit here) without even telling it ?

--
www.lispmachine.wordpress.com

James Waldby
Guest
Posts: n/a

 11-17-2010
On Wed, 17 Nov 2010 04:42:23 +0000, arnuld wrote:
>> On Tue, 16 Nov 2010 20:52:39 +0000, Seebs wrote:
>>> On 2010-11-16, Chad <(E-Mail Removed)> wrote: Let's say I call
>>> malloc() like the following

>
>>> char *a = malloc(20);
>>> and then do

>>
>>> free(a);

>>
>>> Does 20 chars or 20 ints get freed?

>
>> No.
>>
>> The space that was allocated gets freed. It doesn't matter what you
>> think the pointer is; if it's the address malloc() returned, the whole
>> space is freed regardless.

>
> We have not passed anything to the free() except pointer and it still
> free ()s the allocated memory. It means free() knows that char* a has
> 20x8 bits (assuming char is 8 bit here) without even telling it ?

See <http://c-faq.com/malloc/freesize.html> for an answer to that
question, or see <http://c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=malloc>
for more general info about malloc. If you want to find out how much
overhead an allocated block uses on your system, try a program like
the following (adjust the constants 21 and 28 if necessary).

/* Re: overhead bytes for allocated memory */
#include <stdlib.h>
#include <stdio.h>
int main(void) {
int k;
char *now=NULL, *next;
for (k=21; k<28; ++k) {
next = malloc(k+1);
if (!next) exit(1);
if (now)
printf ("k=%2d next-now = %3ld now=%p\n",
k, next-now, now);
now=next;
}
return 0;
}

On my AMD x64 system this displayed the following, which
indicates that overhead is not less than 8 bytes (ie, 32-24).
k=22 next-now = 32 now=0x601010
k=23 next-now = 32 now=0x601030
k=24 next-now = 32 now=0x601050
k=25 next-now = 48 now=0x601070
k=26 next-now = 48 now=0x6010a0
k=27 next-now = 48 now=0x6010d0

--
jiw

Seebs
Guest
Posts: n/a

 11-17-2010
On 2010-11-17, arnuld <(E-Mail Removed)> wrote:
>> On Tue, 16 Nov 2010 20:52:39 +0000, Seebs wrote:
>> The space that was allocated gets freed. It doesn't matter what you
>> think the pointer is; if it's the address malloc() returned, the whole
>> space is freed regardless.

> We have not passed anything to the free() except pointer and it still free
> ()s the allocated memory.

Yes.

> It means free() knows that char* a has 20x8
> bits (assuming char is 8 bit here) without even telling it ?

Stop thinking in terms of bits! BIT SIZE IS TOTALLY IRRELEVANT TO THIS
QUESTION. That you even mention bits says you are not understanding

*STORAGE IN C HAPPENS IN TERMS OF char NOT IN TERMS OF BITS*.

Bitfields are a semi-exception, but they are really packed into objects
of some integer type, and the integer type is still some number of chars
of storage.

Yes, free() knows how much space was allocated at that address. Part
of what malloc()/free() do is keep an internal table that allows free()
to free the space allocated. That's why you can only pass free()
pointers returned by malloc() (or null pointers).

When you request 20 bytes, malloc() records in some way how what block
of space it gave you (which may well have more than 20 bytes in it), so
that when you free the memory, that hunk of space can be made available
for further allocation.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

Keith Thompson
Guest
Posts: n/a

 11-17-2010
Seebs <(E-Mail Removed)> writes:
[...]
> *STORAGE IN C HAPPENS IN TERMS OF char NOT IN TERMS OF BITS*.
>
> Bitfields are a semi-exception, but they are really packed into objects
> of some integer type, and the integer type is still some number of chars
> of storage.

You mean struct, not integer, yes?

> Yes, free() knows how much space was allocated at that address. Part
> of what malloc()/free() do is keep an internal table that allows free()
> to free the space allocated. That's why you can only pass free()
> pointers returned by malloc() (or null pointers).

It's conceivable that the memory allocation system doesn't even know
how much space was allocated, as long as it can arrange for free()
to do the right thing. A hypothetical example: You call malloc(10),
and it actually allocates 16 bytes, without remembering that you
asked for only 10. Or maybe it remembers both the 10 and the 16,
so it only has to copy 10 bytes if you call realloc(). Later, the 16
bytes following your allocation are free()d, and the system combines
them with the original 16 bytes to create a 32-byte allocated block
(perhaps because it guesses that you might want to call realloc() to
expand it further). Not necessarily plausible, but entirely legal.

> When you request 20 bytes, malloc() records in some way how what block
> of space it gave you (which may well have more than 20 bytes in it), so
> that when you free the memory, that hunk of space can be made available
> for further allocation.

Right. Note that the "table" you mentioned earlier needn't be any
particular data structure; information about allocated memory could be
scattered through the "heap".

--
Keith Thompson (The_Other_Keith) (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"

Michael Foukarakis
Guest
Posts: n/a

 11-17-2010
On Nov 16, 10:12*pm, Chad <(E-Mail Removed)> wrote:
> Let's say I call malloc() like the following
>
> char *a = malloc(20);
>
> and then do
>
> free(a);
>
> Does 20 chars or 20 ints get freed?

It's impossible to say. It depends on the implementation, specifically
how much space is actually allocated by malloc() when you request 20
bytes (20 bytes + bookkeeping + header + footer etc).