C99 Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vijay Kumar R Zanvar

    C99 Question

    Hi,

    Which section of C99 says that return value
    of malloc(3) should not be casted?

    Thanks.

    --
    Vijay Kumar R Zanvar
    My Home Page - http://www.geocities.com/vijoeyz/


  • Martin Dickopp

    #2
    Re: C99 Question

    "Vijay Kumar R Zanvar" <vijoeyz@hotpop .com> writes:
    [color=blue]
    > Which section of C99 says that return value
    > of malloc(3) should not be casted?[/color]

    None. The standard only defines the language, it does not advise on
    good programming style.

    Martin

    Comment

    • Richard Heathfield

      #3
      Re: C99 Question

      Vijay Kumar R Zanvar wrote:
      [color=blue]
      > Hi,
      >
      > Which section of C99 says that return value
      > of malloc(3) should not be casted?[/color]

      The same section that says you should not microwave your cat.

      Casting malloc is deprecated by the regular subscribers in this newsgroup
      because:

      (a) it does nothing good;
      (b) it doesn't stop anything bad happening;
      (c) it can (in certain circumstances in C90) actually /cause/ something bad
      to happen.

      Even if you're not persuaded by (a) and (b) together (which you probably
      should be), (c) should be a showstopper.

      The malloc function - Just Don't Cast.

      --
      Richard Heathfield : binary@eton.pow ernet.co.uk
      "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
      C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
      K&R answers, C books, etc: http://users.powernet.co.uk/eton

      Comment

      • lallous

        #4
        Re: C99 Question

        Hello,
        "Richard Heathfield" <invalid@addres s.co.uk.invalid > wrote in message
        news:3ff15532@n ews2.power.net. uk...[color=blue]
        > Vijay Kumar R Zanvar wrote:
        >
        >
        > The malloc function - Just Don't Cast.[/color]
        This might be silly from me, but you mean don't cast as:
        int *i = (int *) malloc(sizeof(i nt));
        ?
        What other solutions are there then? just assign w/o casting?
        [color=blue]
        >
        > --
        > Richard Heathfield : binary@eton.pow ernet.co.uk
        > "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
        > C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
        > K&R answers, C books, etc: http://users.powernet.co.uk/eton[/color]


        Comment

        • Richard Bos

          #5
          Re: C99 Question

          "Vijay Kumar R Zanvar" <vijoeyz@hotpop .com> wrote:
          [color=blue]
          > Which section of C99 says that return value
          > of malloc(3) should not be casted?[/color]

          There is no section of C99 that says that; if there were, you wouldn't
          be able to do it. It's a very bad idea all the same.
          In fact, it's similar to there not being any law forbidding you to shoot
          yourself in the foot. You _are_ allowed to do that; but expect it to
          hurt, and do not expect your insurance to pay for the medical costs.

          Richard

          Comment

          • Richard Heathfield

            #6
            Re: C99 Question

            lallous wrote:
            [color=blue]
            > Hello,
            > "Richard Heathfield" <invalid@addres s.co.uk.invalid > wrote in message
            > news:3ff15532@n ews2.power.net. uk...[color=green]
            >> Vijay Kumar R Zanvar wrote:
            >>
            >>
            >> The malloc function - Just Don't Cast.[/color]
            > This might be silly from me, but you mean don't cast as:
            > int *i = (int *) malloc(sizeof(i nt));
            > ?[/color]

            Yes, I mean that this cast is pointless and can be harmful.
            [color=blue]
            > What other solutions are there then? just assign w/o casting?[/color]

            T *p = malloc(n * sizeof *p);

            works for any object type T. You can omit n if it's 1, of course.

            Your example would be better written as: int *i = malloc(sizeof *i);

            --
            Richard Heathfield : binary@eton.pow ernet.co.uk
            "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
            C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
            K&R answers, C books, etc: http://users.powernet.co.uk/eton

            Comment

            • pete

              #7
              Re: C99 Question

              Vijay Kumar R Zanvar wrote:[color=blue]
              >
              > Hi,
              >
              > Which section of C99 says that return value
              > of malloc(3) should not be casted?[/color]

              How about an errata from K&R2 instead ?

              142(§6.5, toward the end):
              The remark about casting the return value of malloc
              ("the proper method is to declare ... then explicitly coerce")
              needs to be rewritten. The example is correct and works,
              but the advice is debatable in the context of the
              1988-1989 ANSI/ISO standards. It's not necessary
              (given that coercion of void * to ALMOSTANYTYPE * is automatic),
              and possibly harmful if malloc,
              or a proxy for it, fails to be declared as returning void *.
              The explicit cast can cover up an unintended error.
              On the other hand, pre-ANSI, the cast was necessary,
              and it is in C++ also.

              🔑Surf like a Boss! Unleash the indomitable power of our lightning-fast, rock-solid VPN shield. Maximum cybersecurity and privacy protection. Built on WireGuard


              --
              pete

              Comment

              • pete

                #8
                Re: C99 Question

                Richard Heathfield wrote:[color=blue]
                >
                > lallous wrote:
                >[color=green]
                > > Hello,
                > > "Richard Heathfield" <invalid@addres s.co.uk.invalid >
                > > wrote in message
                > > news:3ff15532@n ews2.power.net. uk...[color=darkred]
                > >> Vijay Kumar R Zanvar wrote:
                > >>
                > >>
                > >> The malloc function - Just Don't Cast.[/color]
                > > This might be silly from me, but you mean don't cast as:
                > > int *i = (int *) malloc(sizeof(i nt));
                > > ?[/color]
                >
                > Yes, I mean that this cast is pointless and can be harmful.
                >[color=green]
                > > What other solutions are there then? just assign w/o casting?[/color]
                >
                > T *p = malloc(n * sizeof *p);
                >
                > works for any object type T. You can omit n if it's 1, of course.
                >
                > Your example would be better written as: int *i = malloc(sizeof *i);[/color]

                Neither of your posts in this thread, mention stdlib.h.
                In order to write the lines that you wrote, there has to be

                #include <stdlib.h>

                in the code, somewhere before those lines,
                otherwise the compiler will generate a warning which,
                (though the warning signifies an absence of stdlib.h to us,)
                may suggest a cast, and confuse the programmer into thinking
                that the return value of malloc should be cast.
                And who's the programmer going to believe, clc or his compiler ?

                --
                pete

                Comment

                • Richard Heathfield

                  #9
                  Re: C99 Question

                  pete wrote:
                  [color=blue]
                  > Richard Heathfield wrote:[color=green]
                  >>[/color][/color]

                  <snip>
                  [color=blue][color=green]
                  >> Your example would be better written as: int *i = malloc(sizeof *i);[/color]
                  >
                  > Neither of your posts in this thread, mention stdlib.h.[/color]

                  Thanks for mentioning <stdlib.h> which, of course, provides the essential
                  function prototype that is so critical to correct malloc usage.
                  [color=blue]
                  > And who's the programmer going to believe, clc or his compiler ?[/color]

                  If he is wise, he will believe clc (and then he will find out precisely
                  *why* his compiler is right).

                  --
                  Richard Heathfield : binary@eton.pow ernet.co.uk
                  "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
                  C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
                  K&R answers, C books, etc: http://users.powernet.co.uk/eton

                  Comment

                  • lallous

                    #10
                    Re: C99 Question

                    Hello,

                    "Richard Heathfield" <invalid@addres s.co.uk.invalid > wrote in message
                    news:3ff16c4c@n ews2.power.net. uk...[color=blue]
                    > pete wrote:
                    >[color=green]
                    > > Richard Heathfield wrote:[color=darkred]
                    > >>[/color][/color]
                    >
                    > <snip>
                    >[color=green][color=darkred]
                    > >> Your example would be better written as: int *i = malloc(sizeof *i);[/color]
                    > >
                    > > Neither of your posts in this thread, mention stdlib.h.[/color]
                    >
                    > Thanks for mentioning <stdlib.h> which, of course, provides the essential
                    > function prototype that is so critical to correct malloc usage.
                    >[color=green]
                    > > And who's the programmer going to believe, clc or his compiler ?[/color]
                    >
                    > If he is wise, he will believe clc (and then he will find out precisely
                    > *why* his compiler is right).[/color]


                    I use the cast to avoid warning or when using malloc() in a C++ program.

                    The stdlib.h would remove the warning? How, compiler switch (like vc's
                    #pragma warn(disable:n) )?

                    --
                    Elias[color=blue]
                    > --
                    > Richard Heathfield : binary@eton.pow ernet.co.uk
                    > "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
                    > C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
                    > K&R answers, C books, etc: http://users.powernet.co.uk/eton[/color]


                    Comment

                    • Richard Bos

                      #11
                      Re: C99 Question

                      "lallous" <lallous@lgwm.o rg> wrote:
                      [color=blue]
                      > "Richard Heathfield" <invalid@addres s.co.uk.invalid > wrote in message
                      > news:3ff16c4c@n ews2.power.net. uk...[color=green]
                      > > Thanks for mentioning <stdlib.h> which, of course, provides the essential
                      > > function prototype that is so critical to correct malloc usage.[/color][/color]
                      [color=blue]
                      > I use the cast to avoid warning or when using malloc() in a C++ program.[/color]

                      - don't use malloc() in a C++ program, use new;
                      - don't confound C with C++, and least of all good C with good C++;
                      - therefore, don't apply a C++ hack-around to proper C code.
                      [color=blue]
                      > The stdlib.h would remove the warning? How, compiler switch (like vc's
                      > #pragma warn(disable:n) )?[/color]

                      Of course not - by the simple expedient of providing a proper
                      declaration for malloc() and friends. As all good C code should do for
                      all used functions.

                      Richard

                      Comment

                      • Servé Lau

                        #12
                        Re: C99 Question

                        "lallous" <lallous@lgwm.o rg> wrote in message
                        news:bsrr2u$o7i i$1@ID-161723.news.uni-berlin.de...[color=blue]
                        > I use the cast to avoid warning or when using malloc() in a C++ program.[/color]

                        First, you should use new in a C++ program, and you don't have to cast new.

                        Second, the whole point is that if you forget to include stdlib.h and you
                        try to use malloc, the compiler lets malloc return int instead of void *. If
                        you don't cast the return value, the compiler should give an error that
                        there is no automatic conversion from int to pointer type. If you do cast
                        the return value, the compiler thinks you are saying "shut up, I know what
                        I'm doing" and will happily generate some code that converts an int to a
                        pointer. This causes big problemos on platforms where sizeof(int) != sizeof
                        (void *) and 64 bit platforms are coming pretty soon.
                        [color=blue]
                        > The stdlib.h would remove the warning? How, compiler switch (like vc's
                        > #pragma warn(disable:n) )?[/color]

                        Do not remove the warning, write correct code.


                        Comment

                        • pete

                          #13
                          Re: C99 Question

                          lallous wrote:[color=blue]
                          >
                          > Hello,
                          >
                          > "Richard Heathfield" <invalid@addres s.co.uk.invalid > wrote in message
                          > news:3ff16c4c@n ews2.power.net. uk...[color=green]
                          > > pete wrote:
                          > >[color=darkred]
                          > > > Richard Heathfield wrote:
                          > > >>[/color]
                          > >
                          > > <snip>
                          > >[color=darkred]
                          > > >> Your example would be better written as: int *i = malloc(sizeof *i);
                          > > >
                          > > > Neither of your posts in this thread, mention stdlib.h.[/color]
                          > >
                          > > Thanks for mentioning <stdlib.h> which, of course, provides the essential
                          > > function prototype that is so critical to correct malloc usage.
                          > >[color=darkred]
                          > > > And who's the programmer going to believe, clc or his compiler ?[/color]
                          > >
                          > > If he is wise, he will believe clc
                          > > (and then he will find out precisely *why* his compiler is right).[/color]
                          >
                          > I use the cast to avoid warning or when using
                          > malloc() in a C++ program.[/color]

                          The rumors are, that well written C++ programs
                          which look like C++ programs, don't use malloc.
                          [color=blue]
                          > The stdlib.h would remove the warning? How, compiler switch (like vc's
                          > #pragma warn(disable:n) )?[/color]

                          You need to understand the reason for the warning.

                          malloc returns type void*. The prototype for malloc is in stdlib.h.
                          If a function, like malloc, doesn't have a prototype in scope,
                          then the compiler will assume that the function returns type int.
                          If you assign an int to a pointer, you get a warning or an error.

                          If stdlib.h is #included, then the compiler will know that
                          malloc returns type void*. When you assign a void* value
                          to any type of pointer to an object, there is no problem, no warning.

                          --
                          pete

                          Comment

                          • lallous

                            #14
                            Re: C99 Question


                            "pete" <pfiland@mindsp ring.com> wrote in message
                            news:3FF17BE3.5 44@mindspring.c om...[color=blue]
                            > lallous wrote:[color=green]
                            > >
                            > > Hello,
                            > >
                            > > "Richard Heathfield" <invalid@addres s.co.uk.invalid > wrote in message
                            > > news:3ff16c4c@n ews2.power.net. uk...[color=darkred]
                            > > > pete wrote:
                            > > >
                            > > > > Richard Heathfield wrote:
                            > > > >>
                            > > >
                            > > > <snip>
                            > > >
                            > > > >> Your example would be better written as: int *i = malloc(sizeof[/color][/color][/color]
                            *i);[color=blue][color=green][color=darkred]
                            > > > >
                            > > > > Neither of your posts in this thread, mention stdlib.h.
                            > > >
                            > > > Thanks for mentioning <stdlib.h> which, of course, provides the[/color][/color][/color]
                            essential[color=blue][color=green][color=darkred]
                            > > > function prototype that is so critical to correct malloc usage.
                            > > >
                            > > > > And who's the programmer going to believe, clc or his compiler ?
                            > > >
                            > > > If he is wise, he will believe clc
                            > > > (and then he will find out precisely *why* his compiler is right).[/color]
                            > >
                            > > I use the cast to avoid warning or when using
                            > > malloc() in a C++ program.[/color]
                            >
                            > The rumors are, that well written C++ programs
                            > which look like C++ programs, don't use malloc.
                            >[color=green]
                            > > The stdlib.h would remove the warning? How, compiler switch (like vc's
                            > > #pragma warn(disable:n) )?[/color]
                            >
                            > You need to understand the reason for the warning.
                            >
                            > malloc returns type void*. The prototype for malloc is in stdlib.h.
                            > If a function, like malloc, doesn't have a prototype in scope,
                            > then the compiler will assume that the function returns type int.[/color]
                            Yes, I know that.
                            [color=blue]
                            > If you assign an int to a pointer, you get a warning or an error.
                            >
                            > If stdlib.h is #included, then the compiler will know that
                            > malloc returns type void*. When you assign a void* value
                            > to any type of pointer to an object, there is no problem, no warning.[/color]
                            I know that C++ uses new, however C++ enforces type casting and that is why
                            I cast return value of malloc() to the desired type. C would access to
                            convert void* to any other pointer.

                            Anyway, thank you all for the info.
                            Elias[color=blue]
                            >
                            > --
                            > pete[/color]


                            Comment

                            • pete

                              #15
                              Re: C99 Question

                              lallous wrote:[color=blue]
                              >
                              > "pete" <pfiland@mindsp ring.com> wrote in message
                              > news:3FF17BE3.5 44@mindspring.c om...[color=green]
                              > > lallous wrote:[color=darkred]
                              > > >
                              > > > Hello,
                              > > >
                              > > > "Richard Heathfield" <invalid@addres s.co.uk.invalid > wrote in message
                              > > > news:3ff16c4c@n ews2.power.net. uk...
                              > > > > pete wrote:
                              > > > >
                              > > > > > Richard Heathfield wrote:
                              > > > > >>
                              > > > >
                              > > > > <snip>
                              > > > >
                              > > > > >> Your example would be better written as: int *i = malloc(sizeof[/color][/color]
                              > *i);[color=green][color=darkred]
                              > > > > >
                              > > > > > Neither of your posts in this thread, mention stdlib.h.
                              > > > >
                              > > > > Thanks for mentioning <stdlib.h> which, of course, provides the[/color][/color]
                              > essential[color=green][color=darkred]
                              > > > > function prototype that is so critical to correct malloc usage.
                              > > > >
                              > > > > > And who's the programmer going to believe, clc or his compiler ?
                              > > > >
                              > > > > If he is wise, he will believe clc
                              > > > > (and then he will find out precisely *why* his compiler is right).
                              > > >
                              > > > I use the cast to avoid warning or when using
                              > > > malloc() in a C++ program.[/color]
                              > >
                              > > The rumors are, that well written C++ programs
                              > > which look like C++ programs, don't use malloc.
                              > >[color=darkred]
                              > > > The stdlib.h would remove the warning? How, compiler switch (like vc's
                              > > > #pragma warn(disable:n) )?[/color]
                              > >
                              > > You need to understand the reason for the warning.
                              > >
                              > > malloc returns type void*. The prototype for malloc is in stdlib.h.
                              > > If a function, like malloc, doesn't have a prototype in scope,
                              > > then the compiler will assume that the function returns type int.[/color]
                              > Yes, I know that.
                              >[color=green]
                              > > If you assign an int to a pointer, you get a warning or an error.
                              > >
                              > > If stdlib.h is #included, then the compiler will know that
                              > > malloc returns type void*. When you assign a void* value
                              > > to any type of pointer to an object, there is no problem,
                              > > no warning.[/color]
                              > I know that C++ uses new, however C++ enforces type
                              > casting and that is why
                              > I cast return value of malloc() to the desired type. C would access to
                              > convert void* to any other pointer.
                              >
                              > Anyway, thank you all for the info.[/color]

                              And so, to sum up, the big advantage of not casting,
                              is that the warning will alert the programmer if the code
                              has changed in some way such that the prototype for malloc
                              is no longer in scope,
                              which is something that really does happen in real programs.

                              Not having the prototype in scope, gives your code undefined behavior,
                              which means that it might work right in your tests
                              and still have problems in the field.

                              --
                              pete

                              Comment

                              Working...