regarding free function in c library

Discussion in 'C Programming' started by sam_cit@yahoo.co.in, Dec 15, 2006.

  1. Guest

    Hi Everyone,

    It is known that function free() of c library expects parameter of
    type void* and when we invoke them with pointers to any type, compiler
    automatically performs the typecast, can anyone let me know as to why
    pointers are typecasted to void* in many places before performing
    operation on them?
    , Dec 15, 2006
    #1
    1. Advertising

  2. Zealot Zuo Guest

    wrote:
    > Hi Everyone,
    >
    > It is known that function free() of c library expects parameter of
    > type void* and when we invoke them with pointers to any type, compiler
    > automatically performs the typecast, can anyone let me know as to why
    > pointers are typecasted to void* in many places before performing
    > operation on them?


    Hi, firstly :)
    As for void*s, these pointers are pointers with "common" properties
    which would accept any kind of data. For example, you can use malloc()
    to allocate a memory which returns a void* pointer, and you could save
    the pointer to any pointer.
    So the following declaration is right (Though malloc() returns a
    void*):

    int* a = malloc(10*sizeof(int));

    Note that C supports implicit casts from pointers to pointers, where
    C++ doesn't. To use malloc in C++, you must use explicit typecast, such
    as (int*), (void*), etc.
    It is easy to know why these pointers should be re-typecasted to void*
    calling free() on them. Both free(int*) and free(float*) and others
    should be compiled, but C doesn't support polymorphism. Actually,
    polymorphism is not required because freeing an allocated memory
    doesn't care the type allocated.
    So we use a void* pointer to receive pointers to any type. Again,
    please notice that C++ doesn't support implicit pointer-cast.

    Sincerely,
    Z.Z.
    Zealot Zuo, Dec 15, 2006
    #2
    1. Advertising

  3. wrote:

    > It is known that function free() of c library expects parameter of
    > type void* and when we invoke them with pointers to any type, compiler
    > automatically performs the typecast,


    ....performs an implicit conversion,

    > can anyone let me know as to why
    > pointers are typecasted to void* in many places before performing
    > operation on them?


    1. people are stupid

    OR

    2. people are using a C++ compiler

    but I repeat myself :)


    --
    Nick Keighley

    Quantum Boggum Sort:
    Q1. use a source of quantum noise (eg. radioactive decay) to
    randomly permutate an array.
    Q2. if the array is not ordered, destroy the universe (*)
    Q3. if you reached this step your universe has sorted the array
    in O(n) time.
    (*) [100] this is left as an exercise
    Nick Keighley, Dec 15, 2006
    #3
  4. Chris Dollin Guest

    wrote:

    > It is known that function free() of c library expects parameter of
    > type void* and when we invoke them with pointers to any type, compiler
    > automatically performs the typecast,


    It does not. It performs a /conversion/. A cast (not a "typecast", just
    a cast) specifically refers to the syntactic construct `(type) expression`.

    > can anyone let me know as to why
    > pointers are typecasted to void* in many places before performing
    > operation on them?


    They're not (mostly because there aren't many operations you can
    /do/ on a `void*`). Pointers are /converted/ to `void*`s when the
    context expects one and a pointer is provided, and the reason for
    doing so is that you can convert a `T*` pointer value to `void*`
    and back without loss: `void*` is the "generic pointer type" of C.

    Perhaps you should show us an example of what you're asking about.

    --
    Chris "Perikles triumphant" Dollin
    "Who do you serve, and who do you trust?" /Crusade/
    Chris Dollin, Dec 15, 2006
    #4
  5. Guest


    >
    > Perhaps you should show us an example of what you're asking about.
    >
    > --
    > Chris "Perikles triumphant" Dollin
    > "Who do you serve, and who do you trust?" /Crusade/


    Well, i was talking about an example of printing pointers where a cast
    to void* is done explicitly,

    char *p;
    printf("address in pointer is %p",(void*)p);

    Now, using %p how many bytes would printf try to access? and has the
    cast to void* got to do anything with it?
    , Dec 15, 2006
    #5
  6. Zealot Zuo wrote:

    > [...] C doesn't support polymorphism.


    int i, j, k;
    double f, g, h;

    i = j + k;
    f = g + h;

    --
    Nick Keighley
    Nick Keighley, Dec 15, 2006
    #6
  7. wrote:

    > > Perhaps you should show us an example of what you're asking about.

    >
    > Well, i was talking about an example of printing pointers where a cast
    > to void* is done explicitly,
    >
    > char *p;
    > printf("address in pointer is %p",(void*)p);
    >
    > Now, using %p how many bytes would printf try to access?


    as many as are in a void* address

    > and has the cast to void* got to do anything with it?


    I don't understand. Variadic functions don't perform conversions on
    pointers, %p requires a void*, therefore an explicit conversion (cast)
    must be used.


    --
    Nick Keighley
    Nick Keighley, Dec 15, 2006
    #7
  8. Zealot Zuo Guest

    Nick Keighley wrote:
    > Zealot Zuo wrote:
    >
    > > [...] C doesn't support polymorphism.

    >
    > int i, j, k;
    > double f, g, h;
    >
    > i = j + k;
    > f = g + h;
    >
    > --
    > Nick Keighley


    ....well, well, well, well, at least not for built-in type operators...

    Sincerely,
    Z.Z.
    Zealot Zuo, Dec 15, 2006
    #8
  9. Chris Dollin Guest

    wrote:

    >> Perhaps you should show us an example of what you're asking about.

    >
    > Well, i was talking about an example of printing pointers where a cast
    > to void* is done explicitly,
    >
    > char *p;
    > printf("address in pointer is %p",(void*)p);


    [Note: as written this has undefined behaviour, since `p` hasn't been
    given any value.]

    > Now, using %p how many bytes would printf try to access? and has the
    > cast to void* got to do anything with it?


    %p would try to access a void* value, because that's what it's defined
    to accept, so it would access as many bytes as a void* has.

    The cast to void* is required because that's what %p wants, and %p
    wants it because void* is the generic object pointer type.

    [I'm sure I remember that void* and char* are required to have the
    same representation, but I can't find it in my paper C90 draft.
    Bagger.]

    --
    Chris "Perikles triumphant" Dollin
    "Reaching out for mirrors hidden in the web." - Renaissance, /Running Hard/
    Chris Dollin, Dec 15, 2006
    #9
  10. Zealot Zuo said:

    >
    > wrote:
    >> Hi Everyone,
    >>
    >> It is known that function free() of c library expects parameter of
    >> type void* and when we invoke them with pointers to any type, compiler
    >> automatically performs the typecast, can anyone let me know as to why
    >> pointers are typecasted to void* in many places before performing
    >> operation on them?

    >
    > Hi, firstly :)
    > As for void*s, these pointers are pointers with "common" properties
    > which would accept any kind of data. For example, you can use malloc()
    > to allocate a memory which returns a void* pointer, and you could save
    > the pointer to any pointer.
    > So the following declaration is right (Though malloc() returns a
    > void*):
    >
    > int* a = malloc(10*sizeof(int));
    >
    > Note that C supports implicit casts from pointers to pointers,


    No, it doesn't; it performs implicit conversions. A cast is an explicit
    conversion.

    > It is easy to know why these pointers should be re-typecasted to void*
    > calling free() on them.


    Note that the term is "cast", not "typecast". Note further that the cast is
    not in fact required when passing a pointer value to free().

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Dec 15, 2006
    #10
  11. Nick Keighley said:

    > wrote:
    >
    >> can anyone let me know as to why
    >> pointers are typecasted to void* in many places before performing
    >> operation on them?

    >
    > 1. people are stupid
    >
    > OR
    >
    > 2. people are using a C++ compiler
    >
    > but I repeat myself :)


    Fortunately for my keyboard, I was not drinking coffee.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Dec 15, 2006
    #11
  12. CBFalconer Guest

    wrote:
    >
    >> Perhaps you should show us an example of what you're asking about.

    >
    > Well, i was talking about an example of printing pointers where a
    > cast to void* is done explicitly,
    >
    > char *p;
    > printf("address in pointer is %p",(void*)p);
    >
    > Now, using %p how many bytes would printf try to access? and has
    > the cast to void* got to do anything with it?


    Please don't remove attributes for material you quote. But please
    do snip signatures, which is everything following the "-- " marker
    line.

    The %p specifier attempts to access the number of bytes contained
    in a void*. This is not necessarily the same as some other pointer
    type (but, for backwards compatibility reasons, is the same as in a
    char*).

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>
    CBFalconer, Dec 15, 2006
    #12
  13. Default User Guest

    Nick Keighley wrote:

    > wrote:


    > > can anyone let me know as to why
    > > pointers are typecasted to void* in many places before performing
    > > operation on them?


    > 2. people are using a C++ compiler



    Actually, C++ doesn't require a cast from object pointer to void
    pointer, only the other direction. So malloc() requires a cast, but not
    free().




    Brian
    Default User, Dec 15, 2006
    #13
  14. On Fri, 15 Dec 2006 11:35:02 +0000, in comp.lang.c , Chris Dollin
    <> wrote:

    >
    >[I'm sure I remember that void* and char* are required to have the
    > same representation, but I can't find it in my paper C90 draft.


    6.2.5 p26
    "A pointer to void shall have the same representation and alignment
    requirements as a pointer to a character type."
    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
    Mark McIntyre, Dec 15, 2006
    #14
  15. Jack Klein Guest

    On 15 Dec 2006 00:18:30 -0800, wrote in
    comp.lang.c:

    > Hi Everyone,
    >
    > It is known that function free() of c library expects parameter of
    > type void* and when we invoke them with pointers to any type, compiler
    > automatically performs the typecast, can anyone let me know as to why


    There is no such thing as an "automatic cast" in C. The ONLY thing in
    C that performs a cast is a cast operator.

    There are conversions in C, some of them automatic and some of them
    that are only caused by a cast operator.

    > pointers are typecasted to void* in many places before performing
    > operation on them?


    If there is a prototype in scope, there is never a need to cast a
    pointer to any object type to pointer to void when calling a
    non-variadic function that requires a pointer to void as a parameter.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Dec 16, 2006
    #15
  16. Jack Klein Guest

    On 15 Dec 2006 00:38:54 -0800, "Zealot Zuo" <>
    wrote in comp.lang.c:

    >
    > wrote:
    > > Hi Everyone,
    > >
    > > It is known that function free() of c library expects parameter of
    > > type void* and when we invoke them with pointers to any type, compiler
    > > automatically performs the typecast, can anyone let me know as to why
    > > pointers are typecasted to void* in many places before performing
    > > operation on them?

    >
    > Hi, firstly :)
    > As for void*s, these pointers are pointers with "common" properties
    > which would accept any kind of data. For example, you can use malloc()
    > to allocate a memory which returns a void* pointer, and you could save
    > the pointer to any pointer.
    > So the following declaration is right (Though malloc() returns a
    > void*):
    >
    > int* a = malloc(10*sizeof(int));
    >
    > Note that C supports implicit casts from pointers to pointers, where


    No, C does not support "implicit casts", no such thing exists in the
    language. All casts in C are the result of a cast operator. C does
    support conversions, some of which are automatic and some of which

    > C++ doesn't. To use malloc in C++, you must use explicit typecast, such
    > as (int*), (void*), etc.


    "explicit cast" is a redundant term, all casts in C are explicit
    because they use a cast operator.

    > It is easy to know why these pointers should be re-typecasted to void*
    > calling free() on them. Both free(int*) and free(float*) and others
    > should be compiled, but C doesn't support polymorphism. Actually,
    > polymorphism is not required because freeing an allocated memory
    > doesn't care the type allocated.


    You are just flat out wrong above. Free does care about the value it
    receives, which must be a pointer to void. Fortunately, if there is a
    prototype of free() in scope, the conversion from pointer to any
    object type to pointer to void is automatic, and has nothing to do
    with casts.

    > So we use a void* pointer to receive pointers to any type. Again,
    > please notice that C++ doesn't support implicit pointer-cast.


    C doesn't support "implicit pointer-cast" either.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Dec 16, 2006
    #16
  17. Zealot Zuo Guest

    Jack Klein wrote:
    > On 15 Dec 2006 00:38:54 -0800, "Zealot Zuo" <>
    > wrote in comp.lang.c:
    >
    > >
    > > wrote:
    > > > Hi Everyone,
    > > >
    > > > It is known that function free() of c library expects parameter of
    > > > type void* and when we invoke them with pointers to any type, compiler
    > > > automatically performs the typecast, can anyone let me know as to why
    > > > pointers are typecasted to void* in many places before performing
    > > > operation on them?

    > >
    > > Hi, firstly :)
    > > As for void*s, these pointers are pointers with "common" properties
    > > which would accept any kind of data. For example, you can use malloc()
    > > to allocate a memory which returns a void* pointer, and you could save
    > > the pointer to any pointer.
    > > So the following declaration is right (Though malloc() returns a
    > > void*):
    > >
    > > int* a = malloc(10*sizeof(int));
    > >
    > > Note that C supports implicit casts from pointers to pointers, where

    >
    > No, C does not support "implicit casts", no such thing exists in the
    > language. All casts in C are the result of a cast operator. C does
    > support conversions, some of which are automatic and some of which
    >
    > > C++ doesn't. To use malloc in C++, you must use explicit typecast, such
    > > as (int*), (void*), etc.

    >
    > "explicit cast" is a redundant term, all casts in C are explicit
    > because they use a cast operator.
    >
    > > It is easy to know why these pointers should be re-typecasted to void*
    > > calling free() on them. Both free(int*) and free(float*) and others
    > > should be compiled, but C doesn't support polymorphism. Actually,
    > > polymorphism is not required because freeing an allocated memory
    > > doesn't care the type allocated.

    >
    > You are just flat out wrong above. Free does care about the value it
    > receives, which must be a pointer to void. Fortunately, if there is a
    > prototype of free() in scope, the conversion from pointer to any
    > object type to pointer to void is automatic, and has nothing to do
    > with casts.
    >
    > > So we use a void* pointer to receive pointers to any type. Again,
    > > please notice that C++ doesn't support implicit pointer-cast.

    >
    > C doesn't support "implicit pointer-cast" either.
    >
    > --
    > Jack Klein
    > Home: http://JK-Technology.Com
    > FAQs for
    > comp.lang.c http://c-faq.com/
    > comp.lang.c++ http://www.parashift.com/c -faq-lite/
    > alt.comp.lang.learn.c-c++
    > http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html


    The fact is the following code compiles as well as I am here:

    #include <stdio.h>
    #include <malloc.h>

    main() {
    int* k = malloc(sizeof(int)*10);
    k[1] = 10;
    k[2] = 10;
    printf("%d %d\n", k[1], k[2]);
    free(k);
    }
    Zealot Zuo, Dec 16, 2006
    #17
  18. Zealot Zuo said:

    <snip>

    > The fact is the following code compiles as well as I am here:
    >
    > #include <stdio.h>
    > #include <malloc.h>


    Non-standard header. Any compiler is entitled to reject this program right
    there.

    > main() {
    > int* k = malloc(sizeof(int)*10);


    Better: int *k = malloc(10 * sizeof *k);

    > k[1] = 10;


    That's unwise. k could be NULL.

    > k[2] = 10;
    > printf("%d %d\n", k[1], k[2]);
    > free(k);
    > }


    You forgot to return a value from main. But if you fix that, add the NULL
    check, and replace malloc.h with stdlib.h, you have a fine program there.
    Er, so what? Nothing you have written here contradicts what Jack told you.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Dec 16, 2006
    #18
  19. Chris Dollin Guest

    Mark McIntyre wrote:

    > On Fri, 15 Dec 2006 11:35:02 +0000, in comp.lang.c , Chris Dollin
    > <> wrote:
    >
    >>
    >>[I'm sure I remember that void* and char* are required to have the
    >> same representation, but I can't find it in my paper C90 draft.

    >
    > 6.2.5 p26
    > "A pointer to void shall have the same representation and alignment
    > requirements as a pointer to a character type."


    Thanks, Mark. Nice to know I wasn't hallucinating.

    --
    Chris "HO. HO. HO." Dollin
    The "good old days" used to be much better.
    Chris Dollin, Dec 18, 2006
    #19
  20. On Fri, 15 Dec 2006 20:37:07 +0000, Mark McIntyre
    <> wrote:

    > On Fri, 15 Dec 2006 11:35:02 +0000, in comp.lang.c , Chris Dollin
    > <> wrote:
    >
    > >
    > >[I'm sure I remember that void* and char* are required to have the
    > > same representation, but I can't find it in my paper C90 draft.

    >
    > 6.2.5 p26
    > "A pointer to void shall have the same representation and alignment
    > requirements as a pointer to a character type."


    In C99. In C90 as asked it was 6.1.2.5.

    - David.Thompson1 at worldnet.att.net
    Dave Thompson, Jan 3, 2007
    #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. Vijay

    Regarding free function

    Vijay, May 31, 2006, in forum: C Programming
    Replies:
    23
    Views:
    558
    Jordan Abel
    Jun 2, 2006
  2. Panduranga Chary

    How memory function free() knows how much memory to free.

    Panduranga Chary, Dec 27, 2007, in forum: C Programming
    Replies:
    2
    Views:
    401
    Keith Thompson
    Dec 27, 2007
  3. george
    Replies:
    0
    Views:
    1,080
    george
    Aug 29, 2008
  4. mohammed_a_o
    Replies:
    0
    Views:
    256
    mohammed_a_o
    Nov 30, 2010
  5. paolo

    regarding free function in c library

    paolo, Dec 17, 2011, in forum: C Programming
    Replies:
    23
    Views:
    710
    Keith Thompson
    Dec 19, 2011
Loading...

Share This Page