malloc() and implicit cast

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • arnuld

    malloc() and implicit cast

    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 ?




    --



  • Morris Dovey

    #2
    Re: malloc() and implicit cast

    arnuld wrote:

    I don't understand. Do you think there /should/ be a problem?

    --
    Morris Dovey
    DeSoto Solar
    DeSoto, Iowa USA

    Comment

    • Stephen Sprunk

      #3
      Re: malloc() and implicit cast

      "arnuld" <ullu@kullu.com wrote in message
      news:pan.2008.0 4.15.14.28.54.6 87100@kullu.com ...
      >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

      Comment

      • Andrey Tarasevich

        #4
        Re: malloc() and implicit cast

        arnuld wrote:OK.
        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.
        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?

        --
        Best regards,
        Andrey Tarasevich

        Comment

        • Jack Klein

          #5
          Re: malloc() and implicit cast

          On Tue, 15 Apr 2008 08:13:43 -0700, Andrey Tarasevich
          <andreytarasevi ch@hotmail.comw rote in comp.lang.c:
          arnuld wrote:>
          OK.
          >
          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.
          >
          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.

          --
          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.l earn.c-c++

          Comment

          • Peter Nilsson

            #6
            Re: malloc() and implicit cast

            Jack Klein wrote:
            Andrey Tarasevich <andreytarasevi ch@hotmail.comw rote:
            arnuld wrote:
            I have checked the FAQ:

            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

            Comment

            • arnuld

              #7
              Re: malloc() and implicit cast

              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.





              --


              find my email ID at the above address.

              Comment

              • arnuld

                #8
                Re: malloc() and implicit cast

                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 ?




                --



                Comment

                • Peter Nilsson

                  #9
                  Re: malloc() and implicit cast

                  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

                  Comment

                  • Richard Heathfield

                    #10
                    Re: malloc() and implicit cast

                    arnuld said:
                    >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.
                    I don't see why you conclude this from what Jack said, because it simply
                    isn't true. A pointer to function, of no matter what return type, cannot
                    be implicitly converted into *any* other type, let alone a char *.
                    2.) Pointers to object types: compiler can implicitly convert and int*
                    into char* without any warning message to the programmer.
                    No, there is no implicit conversion between int * and char *.
                    C [does not] require any warning in this case
                    Implementations are required to diagnose an attempt to assign int * to char
                    * and vice versa.
                    Is that what you mean ?
                    I doubt it, because it's completely wrong.

                    --
                    Richard Heathfield <http://www.cpax.org.uk >
                    Email: -http://www. +rjh@
                    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                    "Usenet is a strange place" - dmr 29 July 1999

                    Comment

                    • Peter Nilsson

                      #11
                      Re: malloc() and implicit cast

                      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

                      Comment

                      • Richard Heathfield

                        #12
                        Re: malloc() and implicit cast

                        Peter Nilsson said:
                        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;
                        }
                        ANY dogmatic assertion, no matter how obviously correct, has a
                        counter-example that disproves it. :-)

                        In this case, you're dodging round the type system by missing out the
                        parameter list from fp's type - and yes, that is a valid demonstration
                        that the type system for function pointers isn't quite as bullet-proof as
                        I'd suggested. (That doesn't mean that it's a good idea, of course, to do
                        such dodgery - but it can be a very present help in time of trouble).

                        --
                        Richard Heathfield <http://www.cpax.org.uk >
                        Email: -http://www. +rjh@
                        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                        "Usenet is a strange place" - dmr 29 July 1999

                        Comment

                        • CBFalconer

                          #13
                          Re: malloc() and implicit cast

                          arnuld wrote:
                          >Jack Klein wrote:
                          >>
                          .... snip ...
                          >>
                          >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.
                          No, that is a programming error. That particular void* can be
                          converted back into an int* without loss, but other conversions
                          (implicit or not) are NOT guaranteed.

                          --
                          [mail]: Chuck F (cbfalconer at maineline dot net)
                          [page]: <http://cbfalconer.home .att.net>
                          Try the download section.

                          ** Posted from http://www.teranews.com **

                          Comment

                          • Eric Sosman

                            #14
                            Re: malloc() and implicit cast

                            CBFalconer wrote:
                            arnuld wrote:
                            >>Jack Klein wrote:
                            >>>
                            ... snip ...
                            >>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.
                            >
                            No, that is a programming error. That particular void* can be
                            converted back into an int* without loss, but other conversions
                            (implicit or not) are NOT guaranteed.
                            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.

                            --
                            Eric.Sosman@sun .com

                            Comment

                            • Andrey Tarasevich

                              #15
                              Re: malloc() and implicit cast

                              arnuld 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.
                              ??? I don't know where you draw that "conclusion " from. Function pointers
                              _cannot_ be implicitly converted to object pointers. That's what has been said
                              so far. How you ended up concluding the opposite is beyond me.
                              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
                              Compilers _cannot_ implicitly convert 'int* ' into 'char*' directly, meaning
                              that an attempt to do so would require a diagnostic.

                              What can be done, is that you can perform that conversion without using an
                              explicit cast, as a two-step process involving 'void*' as an intermediate type
                              'int*' -'void*' -'char*'.

                              int* pi;
                              ...
                              void* pv = pi; /* implicit conversion 1 */
                              char* pc = pv; /* implicit conversion 2 */

                              Needless to say, due to the two-step structure of the process, in general case
                              the compiler cannot catch the problem here, if there's one. So, of course, it is
                              not surprising there's no requirement for any diagnostic.

                              --
                              Best regards,
                              Andrey Tarasevich

                              Comment

                              Working...