Re: why does this happen?

From: Barry Schwarz (schwarzb_at_deloz.net)
Date: 03/30/05

  • Next message: Barry Schwarz: "Re: How do I do something every x increment in time?"
    Date: Tue, 29 Mar 2005 17:07:23 -0800
    
    

    On Tue, 29 Mar 2005 20:00:31 GMT, Thomas Matthews
    <Thomas_MatthewsSpamBotsSuck@sbcglobal.net> wrote:

    >Billy Patton wrote:
    >
    >> First I'm trying to convert K lines of c code to c++, in baby steps.
    >>
    >> I've written a small example of something I'm trying to do.
    >> #include <string>
    >For using malloc(), also:
    > #include <stdlib.h>
    >That way, you won't have to cast the output.
    >{You can also include <cstdlib> }

    In C++, the cast is mandatory.

    Regardless, failing to have a valid prototype in scope for malloc
    leads to undefined behavior.

    <<Remove the del for email>>


  • Next message: Barry Schwarz: "Re: How do I do something every x increment in time?"

    Relevant Pages

    • Re: unusual behaviour of program under linux....
      ... >> You don't need to do this cast. ... You need to stop casting the return value from malloc(). ... The ISO/ANSI C Standard says the some ways of arranging C code ... Undefined behavior is another one you will want to learn ...
      (comp.unix.programmer)
    • Re: problem with memcpy and pointers/arrays confusion - again
      ... this second method is known as an explicit conversion, or cast. ... The cast, in effect, tells the compiler: ... the malloc function. ... function taking a size_t as a parameter and returning a void pointer (i.e. ...
      (comp.lang.c)
    • Re: what will happen after i use free()???
      ... now there is another thing i wanna know.should i cast malloc when i ... since otherwise you won't be alerted by the compiler if you fail to ... Never ever cast the return value of a function returning void* - ... except you likes to land in the world of undefined behavior. ...
      (comp.lang.c)
    • Re: why is casting malloc a bad thing?
      ... In the compilers we used in the 1980s (including VAX ... compilers for IBM PCs), malloc() had type: ... Without the cast, you got ... and adds HUGE amounts of risk. ...
      (comp.lang.c)
    • Re: Casting the return value of malloc()...
      ... The major reason for not using superfluous casts, of which malloc() ... programmer's ability to treat a cast as a warning sign. ... then the compiler can catch type errors. ... The sizeof operator should never be followed immediately by '(' ...
      (comp.lang.c)