Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > where is the end of free()?

Reply
Thread Tools

where is the end of free()?

 
 
Thomas Zhu
Guest
Posts: n/a
 
      10-21-2005
would someone tell me:


s1: int *ptr = (int *) malloc (sizeof(int));

s2: int *ptr = (int *) malloc (n * sizeof(int));

when i use free(ptr),

what is the difference between the two statements?


thanks in advance.

 
Reply With Quote
 
 
 
 
Madhav
Guest
Posts: n/a
 
      10-21-2005
Thomas Zhu wrote:
> would someone tell me:
>
>
> s1: int *ptr = (int *) malloc (sizeof(int));
>
> s2: int *ptr = (int *) malloc (n * sizeof(int));


Please remove the cast. you don't need to cast the value returned by
malloc. Please include stdlib.h.

The difference in the above two statements is the number of bytes
which are marked as reusable.

 
Reply With Quote
 
 
 
 
Thomas Zhu
Guest
Posts: n/a
 
      10-21-2005
thanks.

but :

ptr = (int *) malloc (n * sizeof(int));
ptr ++;

free(ptr);

does the system free n mem-units or n-1 mem-units?

and why the cast is not necessary?

 
Reply With Quote
 
Marc Boyer
Guest
Posts: n/a
 
      10-21-2005
Le 21-10-2005, Thomas Zhu <> a écrit*:
> ptr = (int *) malloc (n * sizeof(int));
> ptr ++;
>
> free(ptr);
>
> does the system free n mem-units or n-1 mem-units?


Neither one nor the other. This is UB.

> and why the cast is not necessary?


Because malloc returns a void* pointer, and it
can be implicitely converted into int*.

Marc Boyer
 
Reply With Quote
 
Villy Kruse
Guest
Posts: n/a
 
      10-21-2005
On Fri, 21 Oct 2005 09:37:34 +0000 (UTC),
Marc Boyer <> wrote:


> Le 21-10-2005, Thomas Zhu <> a écrit*:
>> ptr = (int *) malloc (n * sizeof(int));
>> ptr ++;
>>
>> free(ptr);
>>
>> does the system free n mem-units or n-1 mem-units?

>
> Neither one nor the other. This is UB.
>


In this case very likely a painfull UB.

In many implementations the size of an allocated buffer is stored
somwhere just before the buffer itself, and free finds that using a
negative offset from the passed pointer. Obviously, if the ptr given
to free doesn't have the same value as returned from a call to malloc,
free can't find the size of the buffer and thus can't free it properly.

Villy
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      10-21-2005
Thomas Zhu <> wrote:

> ptr = (int *) malloc (n * sizeof(int));
> ptr ++;


> free(ptr);


> does the system free n mem-units or n-1 mem-units?


Neither. If you pass a pointer to free() that was not returned by a
call to malloc(), you get "undefined behavior" - in other words,
absolutely anything may happen at that point.

Furthermore, all you need to know about free() is that it deallocates
all the memory reserved by malloc(); that amount is at least, but by
no means limited to, the amount of memory you asked for.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Thomas Zhu
Guest
Posts: n/a
 
      10-21-2005
I''ve got it!!!

I often heard some words (I dont know the their English name , i
translate them from my language to English):
1/memory leak
2/wild pointer

is there any good online books on them ?

thanks a lot.

 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      10-21-2005
Thomas Zhu a écrit :
> ptr = (int *) malloc (n * sizeof(int));


What are the words you don't understand in:

"Please remove the cast. you don't need to cast the value returned by
malloc. Please include stdlib.h."

> ptr ++;
>
> free(ptr);


Undefined behaviour.

The value passed to free() must exactly be the value received from malloc().

--
C is a sharp tool
 
Reply With Quote
 
Thomas Zhu
Guest
Posts: n/a
 
      10-21-2005
Thanks a lot.

I've got it.
I supposed that the compiler would give a warning to the statement
without a cast.
But I was wrong.
Just now I tried some compilers, they all works.

 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      10-21-2005
Thomas Zhu a écrit :
> I often heard some words (I dont know the their English name , i
> translate them from my language to English):
> 1/memory leak


Meaning that some allocated memory can't be freed(). It may happen if
you loose the value of the pointer.

printf ("%p\n", (void *) malloc(123));

or more likely (Ok, strdup() not standard C but is POSIX.1, hence very
portable)

printf ("%s\n", strdup("Hello world"));

> 2/wild pointer


or 'dandling pointer'. An uninitialized pointer or a pointer to an
invalid zone (out of the limits of an array for example). As long as you
don't dereference it, it's fine (well, sort of). But if you dereference
it, it bites (UB).

--
C is a sharp tool
 
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
Help with switch configuration, ( 3 3550's, 5 2950's end to end ) ec Cisco 3 07-25-2006 10:30 AM
using translate as a REAL end-to-end x25 to tcp gateway? RedRat Cisco 1 02-01-2006 09:21 PM
Re: Private LAN: why should the gateway address be at the low end of the range, rather than at the high end. Ted Jones Cisco 11 11-04-2005 05:56 AM
Measure delay end-to-end Dave Cisco 1 07-20-2004 12:51 PM
is there a difference between CIR and CIR+end to end clear channel connection? ike lozada Cisco 0 05-27-2004 02:34 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57