Casting the return value of malloc() ?

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

    #16
    Re: Casting the return value of malloc() ?

    Keith Thompson <kst-u@mib.orgwrote:
    Old Wolf <oldwolf@inspir e.net.nzwrites:
    On Oct 3, 10:27 pm, Nick Keighley <nick_keighley_ nos...@hotmail. com>
    Yes C and C++ are different. But you can write a programs
    using a LARGE subset of C that will compile with C and C++.
    True, but in the general case they'll be both bad C and worse C++.
    _Correct_, yes, but horrible style.
    The question is, why would you want to do this?

    You can write programs that compile in both C
    and Fortran. But again, why would you?

    Here's the reasons I can think of:
    1) For curiosity's sake
    2) You are an idiot

    What's your reason?
    >
    As we've mentioned several times in this thread, P.J. Plauger seems to
    have sound business reasons for doing this.
    He does, but so far, he's the only one who has managed to convince me
    that _in his specific case_, the advantages outweigh the downsides. In
    every other case I've seen, the claim that "but I might want to compile
    my C code as C++" is very simply put aside with the argument that you
    just should not do that, as there are mechanisms in C++ to make such
    half-hearted practices superfluous.

    Richard

    Comment

    • Keith Thompson

      #17
      Re: Casting the return value of malloc() ?

      rlb@hoekstra-uitgeverij.nl (Richard Bos) writes:
      Keith Thompson <kst-u@mib.orgwrote:
      >Old Wolf <oldwolf@inspir e.net.nzwrites:
      On Oct 3, 10:27 pm, Nick Keighley <nick_keighley_ nos...@hotmail. com>
      >Yes C and C++ are different. But you can write a programs
      >using a LARGE subset of C that will compile with C and C++.
      >
      True, but in the general case they'll be both bad C and worse C++.
      _Correct_, yes, but horrible style.
      As far as I know, the only thing you need to do that I'd consider poor
      C style is casting the result of malloc -- and more generally, not
      taking advantage of C's implicit conversions for void*.

      I'm sure a lot of programmers trying to write code that's compatible
      with both C and C++ will write stylistically bad code -- but then a
      lot of programmers manage to do that trying to write plain C.

      [...]
      >As we've mentioned several times in this thread, P.J. Plauger seems to
      >have sound business reasons for doing this.
      >
      He does, but so far, he's the only one who has managed to convince me
      that _in his specific case_, the advantages outweigh the downsides. In
      every other case I've seen, the claim that "but I might want to compile
      my C code as C++" is very simply put aside with the argument that you
      just should not do that, as there are mechanisms in C++ to make such
      half-hearted practices superfluous.
      Agreed. *Most* programmers don't need to write code that compiles as
      both C and C++ (and far too many think they do). But to suggest that
      *everyone* who does so is stupid, or has stupid customers, is absurd.

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      Nokia
      "We must do something. This is something. Therefore, we must do this."
      -- Antony Jay and Jonathan Lynn, "Yes Minister"

      Comment

      • RocTheEngy

        #18
        Re: Casting the return value of malloc() ?

        Not that I can think of. Testing the return value, on the other hand, is
        crucial.
        >
        I suggest you find someone who advocates the cast, and ask them why. Most
        likely, they won't know. In the event that you find someone who does know
        why they're casting, why not present this group with the reason he or she
        gives you, and ask us what we think of it?
        >
        --
        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

        The professor at my college where I learned C (he was newly hired at
        the time; almost 20 years ago) was the first person to teach C at
        the university. Previous programming classes were purely pascal.

        He was therefore, by defacto standard, the "guru" and not to be
        questioned... (at least not by us undergrads).

        He was _adamant_ that the malloc() family *required* casts. To this
        day,
        the only answer I recall him giving is "...to give the compiler a
        warm,
        fuzzy feeling". To inquire further left one with the feeling that
        your next test grade may suffer if you pressed him. Looking back, I
        suspect the intimidation was to avoid embarrasment.

        The system, as best I recall, was some type of VAX. It ran a flavor
        of Unix, and we compiled our code with "cc" (not gcc). I used that
        programming convention for years on many platforms until I read the
        CLC FAQ.

        To this day, many of us would like to know WHY we were
        required to cast for malloc's. I wish I could remember more details.
        For all I now, he may have had a reason. Beats me.

        Comment

        • Richard Heathfield

          #19
          Re: Casting the return value of malloc() ?

          RocTheEngy said:
          >
          >Not that I can think of. Testing the return value, on the other hand, is
          >crucial.
          >>
          >I suggest you find someone who advocates the cast, and ask them why.
          >Most likely, they won't know. In the event that you find someone who
          >does know why they're casting, why not present this group with the
          >reason he or she gives you, and ask us what we think of it?
          >>
          The professor at my college where I learned C (he was newly hired at
          the time; almost 20 years ago) was the first person to teach C at
          the university. Previous programming classes were purely pascal.
          >
          He was therefore, by defacto standard, the "guru" and not to be
          questioned... (at least not by us undergrads).
          >
          He was _adamant_ that the malloc() family *required* casts.
          20 years ago, he was *right*.

          The language changed... 19 years ago.

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

          • RocTheEngy

            #20
            Re: Casting the return value of malloc() ?

            On Oct 6, 4:35 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
            RocTheEngy said:
            >
            >
            >
            >
            >
            >
            >
            Not that I can think of. Testing the return value, on the other hand, is
            crucial.
            >
            I suggest you find someone who advocates the cast, and ask them why.
            Most likely, they won't know. In the event that you find someone who
            does know why they're casting, why not present this group with the
            reason he or she gives you, and ask us what we think of it?
            >
            The professor at my college where I learned C (he was newly hired at
            the time; almost 20 years ago) was the first person to teach C at
            the university.  Previous programming classes were purely pascal.
            >
            He was therefore, by defacto standard, the "guru" and not to be
            questioned... (at least not by us undergrads).
            >
            He was _adamant_ that the malloc() family *required* casts.
            >
            20 years ago, he was *right*.
            >
            The language changed... 19 years ago.
            >
            --
            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- Hide quoted text -
            >
            - Show quoted text -
            I said _almost_ 20 years ago. :) It was 1991...

            But to understand your statement; there was a time when this
            convention was correct?
            (i.e. required)

            Comment

            • Ian Collins

              #21
              Re: Casting the return value of malloc() ?

              RocTheEngy wrote:
              >
              I said _almost_ 20 years ago. :) It was 1991...
              >
              But to understand your statement; there was a time when this
              convention was correct?
              (i.e. required)
              >
              Before C was standardised, void wasn't part of the language and malloc
              returned char*

              --
              Ian Collins.

              Comment

              • Peter Nilsson

                #22
                Re: Casting the return value of malloc() ?

                Flash Gordon <s...@spam.caus eway.comwrote:
                Nick Keighley wrote, On 03/10/08 09:27:
                An example of someone who writes code that compiles
                with both C and C++ is P.J.Plauger.
                Let's not forget Ritchie and Kernighan.
                Is he an imbecile or an idiot?
                >
                I *think* he was calling the customers dumbos, not the
                authors of the library.
                Plauger shares the view of his customers. So any criticism
                of his customers must be applied to Plauger himself, if
                consistency is to hold.

                Here's what he had to say in 2004...

                "What I find interesting about this debate is the two
                positions being espoused:

                1) Omitting casts on malloc calls is acceptable, but
                not necessarily virtuous.

                2) Putting casts on malloc calls is stupid.

                Those of us in the first camp are going to keep using
                casts, and we're going to keep respecting those who
                don't. It would be nice if we were granted a bit of
                respect in turn, but what the hell. A closed mind
                avoids the risk of having to change."

                --
                Peter

                Comment

                • Flash Gordon

                  #23
                  Re: Casting the return value of malloc() ?

                  Peter Nilsson wrote, On 06/10/08 23:03:
                  Flash Gordon <s...@spam.caus eway.comwrote:
                  >Nick Keighley wrote, On 03/10/08 09:27:
                  >>An example of someone who writes code that compiles
                  >>with both C and C++ is P.J.Plauger.
                  >
                  Let's not forget Ritchie and Kernighan.
                  That was because there were no compilers conforming to ANSI C at the
                  time so they had to use a C++ compiler to get access to certain features.
                  >>Is he an imbecile or an idiot?
                  >I *think* he was calling the customers dumbos, not the
                  >authors of the library.
                  >
                  Plauger shares the view of his customers. So any criticism
                  of his customers must be applied to Plauger himself, if
                  consistency is to hold.
                  I thought that his view was that it was the right thing for him to do it
                  because that was his customers' requirement.
                  Here's what he had to say in 2004...
                  >
                  "What I find interesting about this debate is the two
                  positions being espoused:
                  >
                  1) Omitting casts on malloc calls is acceptable, but
                  not necessarily virtuous.
                  >
                  2) Putting casts on malloc calls is stupid.
                  He missed a view some espouse...

                  3) Putting casts on malloc is virtuous
                  Those of us in the first camp are going to keep using
                  casts, and we're going to keep respecting those who
                  don't. It would be nice if we were granted a bit of
                  respect in turn, but what the hell. A closed mind
                  avoids the risk of having to change."
                  Well, at least some of the people who in general strongly disagree with
                  putting the casts in have accepted that sometimes there is a good
                  reason. So not everyone who is against casting has a completely closed
                  mind :-)
                  --
                  Flash Gordon
                  If spamming me sent it to smap@spam.cause way.com
                  If emailing me use my reply-to address
                  See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/

                  Comment

                  • Ian Collins

                    #24
                    Re: Casting the return value of malloc() ?

                    Flash Gordon wrote:
                    Peter Nilsson wrote, On 06/10/08 23:03:
                    >Flash Gordon <s...@spam.caus eway.comwrote:
                    >>Nick Keighley wrote, On 03/10/08 09:27:
                    >>>An example of someone who writes code that compiles
                    >>>with both C and C++ is P.J.Plauger.
                    >Let's not forget Ritchie and Kernighan.
                    >
                    That was because there were no compilers conforming to ANSI C at the
                    time so they had to use a C++ compiler to get access to certain features.
                    >
                    Like void!

                    I don't think the original K&R even mentions malloc. They do briefly
                    describe calloc, which is shown as returning char* so a cast is required.

                    --
                    Ian Collins.

                    Comment

                    • Peter Nilsson

                      #25
                      Re: Casting the return value of malloc() ?

                      Flash Gordon <s...@spam.caus eway.comwrote:
                      Peter Nilsson wrote, On 06/10/08 23:03:
                      Flash Gordon <s...@spam.caus eway.comwrote:
                      Nick Keighley wrote, On 03/10/08 09:27:
                      >An example of someone who writes code that compiles
                      >with both C and C++ is P.J.Plauger.
                      Let's not forget Ritchie and Kernighan.
                      >
                      That was because there were no compilers conforming to
                      ANSI C at the time so they had to use a C++ compiler to
                      get access to certain features.
                      <http://groups.google.com/group/comp....ea4a63a610cac?
                      dmode=source>
                      >Is he an imbecile or an idiot?
                      I *think* he was calling the customers dumbos, not the
                      authors of the library.
                      Plauger shares the view of his customers. So any criticism
                      of his customers must be applied to Plauger himself, if
                      consistency is to hold.
                      >
                      I thought that his view was that it was the right thing for
                      him to do it because that was his customers' requirement.
                      <http://groups.google.com/group/comp....11d85b17ce318?
                      dmode=source>

                      <snip>

                      --
                      Peter

                      Comment

                      • Nick Keighley

                        #26
                        Re: Casting the return value of malloc() ?

                        On 8 Oct, 02:14, Ian Collins <ian-n...@hotmail.co mwrote:
                        I don't think the original K&R even mentions malloc.  
                        pp 143 and 167

                        --
                        Nick Keighley

                        Comment

                        • Ian Collins

                          #27
                          Re: Casting the return value of malloc() ?

                          Nick Keighley wrote:
                          On 8 Oct, 02:14, Ian Collins <ian-n...@hotmail.co mwrote:
                          >
                          >I don't think the original K&R even mentions malloc.
                          >
                          pp 143 and 167
                          >
                          Not in my 1979 printing. You are thinking of K&R2.

                          --
                          Ian Collins.

                          Comment

                          • Nick Keighley

                            #28
                            Re: Casting the return value of malloc() ?

                            On 4 Oct, 18:40, Old Wolf <oldw...@inspir e.net.nzwrote:
                            On Oct 3, 10:27 pm,Nick Keighley<nick_k eighley_nos...@ hotmail.com>
                            wrote:
                            >
                            Yes C and C++ are different. But you can write a programs
                            using a LARGE subset of C that will compile with C and C++.
                            I know it is unfashionable to say this in clc.
                            But it is none the less true.
                            >
                            The question is, why would you want to do this?
                            >
                            You can write programs that compile in both C
                            and Fortran.
                            that's just dumb. Try thinking instead of just repeating dogma.
                            Fortran doesn't look anything like C. Well written C compiles
                            with C++. That is a fact.


                            But again, why would you?
                            >
                            Here's the reasons I can think of:
                              1) For curiosity's sake
                              2) You are an idiot
                            whatever.

                            --
                            Nick Keighley

                            Comment

                            • Ian Collins

                              #29
                              Re: Casting the return value of malloc() ?

                              Nick Keighley wrote:
                              On 4 Oct, 18:40, Old Wolf <oldw...@inspir e.net.nzwrote:
                              >On Oct 3, 10:27 pm,Nick Keighley<nick_k eighley_nos...@ hotmail.com>
                              >wrote:
                              >>
                              >>Yes C and C++ are different. But you can write a programs
                              >>using a LARGE subset of C that will compile with C and C++.
                              >>I know it is unfashionable to say this in clc.
                              >>But it is none the less true.
                              >The question is, why would you want to do this?
                              >>
                              >You can write programs that compile in both C
                              >and Fortran.
                              >
                              that's just dumb. Try thinking instead of just repeating dogma.
                              Fortran doesn't look anything like C. Well written C compiles
                              with C++. That is a fact.
                              >
                              No it is not. The common subset of C and C++ compiles as C and C++.
                              There are a number of C constructs that are not valid C++.

                              --
                              Ian Collins.

                              Comment

                              • Nick Keighley

                                #30
                                Re: Casting the return value of malloc() ?

                                On 8 Oct, 09:09, Ian Collins <ian-n...@hotmail.co mwrote:
                                Nick Keighley wrote:
                                On 8 Oct, 02:14, Ian Collins <ian-n...@hotmail.co mwrote:
                                >
                                I don't think the original K&R even mentions malloc.  
                                >
                                pp 143 and 167
                                >
                                Not in my 1979 printing.  You are thinking of K&R2.
                                ah! missed the word "original"! And I don't have
                                K&R1 at my elbow

                                --
                                nick keighley

                                Comment

                                Working...