where is the end of free()?

Discussion in 'C Programming' started by Thomas Zhu, Oct 21, 2005.

  1. Thomas Zhu

    Thomas Zhu Guest

    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.
     
    Thomas Zhu, Oct 21, 2005
    #1
    1. Advertising

  2. Thomas Zhu

    Madhav Guest

    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.
     
    Madhav, Oct 21, 2005
    #2
    1. Advertising

  3. Thomas Zhu

    Thomas Zhu Guest

    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?
     
    Thomas Zhu, Oct 21, 2005
    #3
  4. Thomas Zhu

    Marc Boyer Guest

    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
     
    Marc Boyer, Oct 21, 2005
    #4
  5. Thomas Zhu

    Villy Kruse Guest

    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
     
    Villy Kruse, Oct 21, 2005
    #5
  6. 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.
     
    Christopher Benson-Manica, Oct 21, 2005
    #6
  7. Thomas Zhu

    Thomas Zhu Guest

    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.
     
    Thomas Zhu, Oct 21, 2005
    #7
  8. 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
     
    Emmanuel Delahaye, Oct 21, 2005
    #8
  9. Thomas Zhu

    Thomas Zhu Guest

    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.
     
    Thomas Zhu, Oct 21, 2005
    #9
  10. 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
     
    Emmanuel Delahaye, Oct 21, 2005
    #10
  11. Thomas Zhu, le 21/10/2005, a écrit :
    > 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.

    C++ give an error whithout the cast.
    IMHO, it is not a mortal sin to cast the malloc() return in both C and
    C++.

    --
    Pierre Maurette
     
    Pierre Maurette, Oct 21, 2005
    #11
  12. Pierre Maurette a écrit :
    >> I supposed that the compiler would give a warning to the statement
    >> without a cast.
    >> But I was wrong.

    >
    > C++ give an error whithout the cast.


    Who cares... What is C++ ?

    > IMHO, it is not a mortal sin to cast the malloc() return in both C and C++.


    .... but is can hide some nasty bug, like to forget to include <stdlib.h>

    --
    C is a sharp tool
     
    Emmanuel Delahaye, Oct 21, 2005
    #12
  13. Pierre Maurette a écrit :
    >> I supposed that the compiler would give a warning to the statement
    >> without a cast.
    >> But I was wrong.

    >
    > C++ give an error whithout the cast.


    Who cares... What is C++ ?

    > IMHO, it is not a mortal sin to cast the malloc() return in both C and C++.


    .... but it can hide some nasty bug, like to forget to include <stdlib.h>

    --
    C is a sharp tool
     
    Emmanuel Delahaye, Oct 21, 2005
    #13
  14. Thomas Zhu

    TomHanks Guest

    > and why the cast is not necessary?

    Why is not needed, the cast is needed as pointer from void* to non-void
    requires an explicit cast.



    "Thomas Zhu" <> wrote in message
    news:...
    > 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?
    >
     
    TomHanks, Oct 21, 2005
    #14
  15. TomHanks <> wrote:

    > Why is not needed, the cast is needed as pointer from void* to non-void
    > requires an explicit cast.


    Wrong.

    --
    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.
     
    Christopher Benson-Manica, Oct 21, 2005
    #15
  16. Thomas Zhu

    Default User Guest

    Pierre Maurette wrote:

    > Thomas Zhu, le 21/10/2005, a écrit :
    > > 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.


    > C++ give an error whithout the cast.


    So? This is not C++;

    > IMHO, it is not a mortal sin to cast the malloc() return in both C
    > and C++.


    You shouldn't be using malloc() in C++. You should be using new.
    Writing code to be cross-language compatible is usually a waste of time
    and inefficient, outside of a few library developers.


    Brian

    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
     
    Default User, Oct 21, 2005
    #16
  17. Thomas Zhu

    Default User Guest

    Thomas Zhu wrote:

    > 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.


    Please read my .sig.


    Brian
    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
     
    Default User, Oct 21, 2005
    #17
  18. Default User, le 21/10/2005, a écrit :
    > Pierre Maurette wrote:
    >
    >> Thomas Zhu, le 21/10/2005, a écrit :
    >>> 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.

    >
    >> C++ give an error whithout the cast.

    >
    > So? This is not C++;

    My english is so bad. I prefer to b concise. And you seem to need
    verbose mode:
    Maybe
    >> C++ give an error whithout the cast.

    accounts for:
    >>> I supposed that the compiler would give a warning to the statement
    >>> without a cast.



    >> IMHO, it is not a mortal sin to cast the malloc() return in both C
    >> and C++.

    >
    > You shouldn't be using malloc() in C++. You should be using new.
    > Writing code to be cross-language compatible is usually a waste of time
    > and inefficient, outside of a few library developers.

    Yes. It is not a good idea, unless whn it is.

    --
    Pierre Maurette
     
    Pierre Maurette, Oct 21, 2005
    #18
  19. Thomas Zhu

    Default User Guest

    Pierre Maurette wrote:

    > Default User, le 21/10/2005, a écrit :
    > > Pierre Maurette wrote:


    > > > C++ give an error whithout the cast.

    > >
    > > So? This is not C++;

    > My english is so bad. I prefer to b concise.


    Your English would improve automatically by not using strange
    abreviations.

    > And you seem to need verbose mode: Maybe


    "Maybe"? Maybe what? I certainly could use more verbosity here, as I
    have no idea what you mean.


    > > You shouldn't be using malloc() in C++. You should be using new.
    > > Writing code to be cross-language compatible is usually a waste of
    > > time and inefficient, outside of a few library developers.

    > Yes. It is not a good idea, unless whn it is.


    Well, that certainly covers it.


    Brian

    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
     
    Default User, Oct 21, 2005
    #19
  20. Thomas Zhu

    Razzer Guest

    Christopher Benson-Manica wrote:
    > Thomas Zhu <> wrote:
    > > 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.


    Which techincally means that the implementation could free "n mem-units
    or n-1 mem units". Not that you should rely on this behavior, but I
    just want to point out on the broad abilities of a computer to do
    mischief :).
     
    Razzer, Oct 21, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo Geshel
    Replies:
    2
    Views:
    3,669
    Versteijn
    Aug 18, 2004
  2. William
    Replies:
    2
    Views:
    1,769
    Kevin Spencer
    Jun 1, 2005
  3. Replies:
    2
    Views:
    359
  4. george
    Replies:
    0
    Views:
    1,172
    george
    Aug 29, 2008
  5. mohammed_a_o
    Replies:
    0
    Views:
    307
    mohammed_a_o
    Nov 30, 2010
Loading...

Share This Page