malloc() and implicit cast

Discussion in 'C Programming' started by arnuld, Apr 15, 2008.

  1. arnuld

    arnuld Guest

    I have checked the FAQ: http://c-faq.com/malloc/mallocnocast.html


    FAQ discusses a special case when programmer has forgotten to do
    #include <stdlib.h>. I am including this header and I am not doing any
    explicit cast:



    #include <stdlib.h>


    enum ARRSIZE { MAXSIZE = 100 };


    struct dummy
    {
    int i;
    };


    int main( void )
    {

    char *pc;
    struct dummy *ptrDummy;

    pc = malloc( MAXSIZE );
    ptrDummy=malloc(sizeof(struct dummy));

    return 0;
    }

    ============ OUTPUT ============
    /home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra test.c
    /home/arnuld/programs/C $ ./a.out
    /home/arnuld/programs/C $



    malloc(size_t n) returns a void pointer and here in my program, I am
    assigning malloc returned pointers to 2 different types and I am not
    getting any warnings about <implicit cast>.


    It has something to do with C90 ?




    --
    http://lispmachine.wordpress.com/
    arnuld, Apr 15, 2008
    #1
    1. Advertising

  2. "arnuld" <> wrote in message
    news:p...
    >I have checked the FAQ: http://c-faq.com/malloc/mallocnocast.html
    >
    > FAQ discusses a special case when programmer has forgotten to do
    > #include <stdlib.h>. I am including this header and I am not doing any
    > explicit cast:


    There is no such thing as an "explicit cast". There are implicit and
    explicit conversions; the latter uses a cast, and the former does not.

    > #include <stdlib.h>

    ....
    > char *pc;
    > struct dummy *ptrDummy;
    >
    > pc = malloc( MAXSIZE );
    > ptrDummy=malloc(sizeof(struct dummy));

    ....
    > malloc(size_t n) returns a void pointer and here in my program, I am
    > assigning malloc returned pointers to 2 different types and I am not
    > getting any warnings about <implicit cast>.


    There is no such thing as an "implicit cast". There are implicit and
    explicit conversions; the latter uses a cast, and the former does not.

    Second, a warning is only expected when you _don't_ include the proper
    header and you _don't_ use a cast. Since you're including the proper
    header, there is no reason for a warning.

    What's the problem?

    S

    --
    Stephen Sprunk "God does not play dice." --Albert Einstein
    CCIE #3723 "God is an inveterate gambler, and He throws the
    K5SSS dice at every possible opportunity." --Stephen Hawking
    Stephen Sprunk, Apr 15, 2008
    #2
    1. Advertising

  3. Jack Klein wrote:
    > Andrey Tarasevich <> wrote:
    > > arnuld wrote:
    > > > I have checked the FAQ:
    > > > http://c-faq.com/malloc/mallocnocast.html
    > > > FAQ discusses a special case when programmer has
    > > > forgotten to do #include <stdlib.h>. I am including
    > > > this header and I am not doing any explicit cast:

    > >
    > > Yes, that how it should be, if you really read the
    > > FAQ: include the header, don't use the cast.


    I _really_ read the FAQ and took notice of the parenthetical
    comment at the end. It highlights that the real issue lies
    with using unprototyped functions. Sensible programmers
    will use compilers that advise of such things. [Of course,
    conforming C90 compilers are not required to issue
    diagnostics, but nevertheless the issue has been around
    long enough that you'll be hard pressed to find any
    conforming compiler that isn't capable of alerting you
    to the use of an unprototyped function. Personally, I
    think a C programmer be insane not to use that feature
    if it was available.]

    > > > malloc(size_t n) returns a void pointer and here
    > > > in my program, I am assigning malloc returned
    > > > pointers to 2 different types and I am not
    > > > getting any warnings about <implicit cast>.

    > >
    > > Of course, you don't. In C language 'void*' pointers
    > > are implicitly convertible to and from other pointer
    > > types. What warnings did you expect and why?

    >
    > To and from other object pointer type. There is no
    > defined conversion between pointers to functions and
    > pointers to object types, even incomplete object types
    > like void.


    Except for the case of null pointer constants.

    --
    Peter
    Peter Nilsson, Apr 16, 2008
    #3
  4. arnuld wrote:
    > Jack Klein wrote:
    > > Andrey Tarasevich:
    > > > Of course, you don't. In C language 'void*' pointers are implicitly
    > > > convertible to and from other pointer types. What warnings did you
    > > > expect and why?

    > >
    > > To and from other object pointer type. There is no defined conversion
    > > between pointers to functions and pointers to object types, even
    > > incomplete object types like void.

    >
    > so an int* is implicitly converted to a void*


    What int*?

    > which then can be implicitly converted to char* without any
    > warning at all.


    Consider...

    int *ip = malloc(N * sizeof *ip);

    The malloc function knows nothing about the type being allocated.
    It returns a void * to a region suitably aligned for any object.

    There is an implicit conversion from void * to int * in the assignment
    of the void * to ip, but as you say there is (generally) no warning.
    Nor should you expect there to be one. The implicit conversion of
    void * to and from other object or incomplete types is a language
    _feature_. [Not necessarily a good one, but a deliberate feature
    nonetheless.]

    --
    Peter
    Peter Nilsson, Apr 16, 2008
    #4
  5. Richard Heathfield wrote:
    > ...A pointer to function, of no matter what return type, cannot
    > be implicitly converted into *any* other type, let alone a char *.


    Actually, they can be implicitily converted in limited cases...

    int main(int argc, char **argv)
    {
    int (*fp)() = main; /* okay */
    return 0;
    }

    --
    Peter
    Peter Nilsson, Apr 16, 2008
    #5
  6. arnuld

    arnuld Guest

    > On Tue, 15 Apr 2008 21:00:17 -0500, Jack Klein wrote:

    >> On Tue, 15 Apr 2008 08:13:43 -0700, Andrey Tarasevich
    >> Of course, you don't. In C language 'void*' pointers are implicitly
    >> convertible to and from other pointer types. What warnings did you
    >> expect and why?


    > To and from other object pointer type. There is no defined conversion
    > between pointers to functions and pointers to object types, even
    > incomplete object types like void.


    so an int* is implicitly converted to a void* which then can be
    implicitly converted to char* without any warning at all.





    --
    http://lispmachine.wordpress.com/

    find my email ID at the above address.
    arnuld, Apr 16, 2008
    #6
  7. arnuld

    arnuld Guest

    > On Tue, 15 Apr 2008 21:00:17 -0500, Jack Klein wrote:

    > To and from other object pointer type. There is no defined conversion
    > between pointers to functions and pointers to object types, even
    > incomplete object types like void.


    so I conlcude:

    1.) Function Pointers: pointer to function returning an int can be
    implicitly converted into char* without any any warning message.

    2.) Pointers to object types: compiler can implicitly convert and int*
    into char* without any warning message to the programmer. C doe snot
    require any warning in this case



    Is that what you mean ?




    --
    http://lispmachine.wordpress.com/
    arnuld, Apr 16, 2008
    #7
  8. On 16 Apr 2008 at 14:37, Eric Sosman wrote:
    > In the particular case of thing* to or from char* it's
    > not ipso facto an error: C specifically permits accessing
    > an object's representation as an array of char.
    >
    > A better example might be double* to void* to int*,
    > which is either an error or at best a dubious practice.


    I don't see why isn't necessarily dubious. Portability isn't everything,
    and in speed-critical sections of code, using bit-twiddling to
    manipulate floating-point numbers can be a very valuable practice.
    Antoninus Twink, Apr 16, 2008
    #8
    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. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,077
    Dan Pop
    Feb 10, 2004
  2. EvilRix
    Replies:
    8
    Views:
    641
    Martin Dickopp
    Feb 14, 2004
  3. nick
    Replies:
    2
    Views:
    679
    Martin Ambuhl
    Oct 16, 2005
  4. Paminu
    Replies:
    7
    Views:
    2,395
    FlyingBird
    Feb 7, 2006
  5. sandeep

    malloc() and implicit cast

    sandeep, Jun 3, 2010, in forum: C Programming
    Replies:
    49
    Views:
    1,060
    Seebs
    Jun 27, 2010
Loading...

Share This Page