compilation of malloc using c++

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

    compilation of malloc using c++

    Hi All,

    I need to compile c code using c++ compiler .Inside the c code I am
    using malloc and free.
    1) My doubt is it ok to compile malloc using c++ compiler?
    2)Is behavior will be undefined ?

    Regards,
    Somenath

  • Jens Thoms Toerring

    #2
    Re: compilation of malloc using c++

    somenath <somenathpal@gm ail.comwrote:
    I need to compile c code using c++ compiler .Inside the c code I am
    using malloc and free.
    1) My doubt is it ok to compile malloc using c++ compiler?
    malloc() and free() are functions, so you don't compiler them,
    your program just calls them. But since C++ isn't C you will
    have to cast the return value of malloc() to the type of the
    pointer you assign the return value to - in contrast to C,
    where this usually isn't a good idea.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \______________ ____________ http://toerring.de

    Comment

    • Kenneth Brody

      #3
      Re: compilation of malloc using c++

      somenath wrote:
      >
      Hi All,
      >
      I need to compile c code using c++ compiler .Inside the c code I am
      using malloc and free.
      1) My doubt is it ok to compile malloc using c++ compiler?
      2)Is behavior will be undefined ?
      You would need to ask in comp.lang.c++, down the hall and to the left.

      --
      +-------------------------+--------------------+-----------------------+
      | Kenneth J. Brody | www.hvcomputer.com | #include |
      | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
      +-------------------------+--------------------+-----------------------+
      Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>

      Comment

      • Dave Vandervies

        #4
        Re: compilation of malloc using c++

        In article <1184341969.300 288.4530@d30g20 00prg.googlegro ups.com>,
        somenath <somenathpal@gm ail.comwrote:
        >Hi All,
        >
        >I need to compile c code using c++ compiler .
        What are you Really Trying To Do? Compiling code in one language with
        a compiler for another language is Always A Bad Idea.
        Inside the c code I am
        >using malloc and free.
        >1) My doubt is it ok to compile malloc using c++ compiler?
        Why are you asking comp.lang.c about C++?

        The right answer is to Don't Do That. If you want to use C++, calling
        malloc is almost always the wrong way to allocate memory. Use new or
        new[] instead; that's what it's there for.

        >2)Is behavior will be undefined ?
        I believe malloc's behavior is perfectly well-defined in C++.


        dave

        --
        Dave Vandervies dj3vande@csclub .uwaterloo.ca
        When other kids are scared of monsters, your kid might turn out to grab a
        baseball bat and be disappointed if there WASN'T a monster under the bed...
        --Juergen Nieveler in the scary devil monastery

        Comment

        • somenath

          #5
          Re: compilation of malloc using c++

          On Jul 13, 9:23 pm, dj3va...@csclub .uwaterloo.ca (Dave Vandervies)
          wrote:
          In article <1184341969.300 288.4...@d30g20 00prg.googlegro ups.com>,
          >
          somenath <somenath...@gm ail.comwrote:
          Hi All,
          >
          I need to compile c code using c++ compiler .
          >
          What are you Really Trying To Do? Compiling code in one language with
          a compiler for another language is Always A Bad Idea.
          >
          Inside the c code I am
          using malloc and free.
          1) My doubt is it ok to compile malloc using c++ compiler?
          >
          Why are you asking comp.lang.c about C++?
          >
          The right answer is to Don't Do That. If you want to use C++, calling
          malloc is almost always the wrong way to allocate memory. Use new or
          new[] instead; that's what it's there for.
          >
          2)Is behavior will be undefined ?
          >
          I believe malloc's behavior is perfectly well-defined in C++.
          >
          dave
          >
          --
          Dave Vandervies dj3va...@csclub .uwaterloo.ca
          When other kids are scared of monsters, your kid might turn out to grab a
          baseball bat and be disappointed if there WASN'T a monster under the bed...
          --Juergen Nieveler in the scary devil monastery
          Thanks for the reply.Actually i need to call some API written using c+
          + .So i need to compile my code using c++ else it does not compile.But
          in side my code it is required to use malloc.So i was having doubt
          will it cause any problem ?


          Comment

          • Flash Gordon

            #6
            Re: compilation of malloc using c++

            somenath wrote, On 13/07/07 16:52:
            Hi All,
            >
            I need to compile c code using c++ compiler .Inside the c code I am
            using malloc and free.
            1) My doubt is it ok to compile malloc using c++ compiler?
            2)Is behavior will be undefined ?
            What you can do with a C++ compiler is topical in a C++ group NOT a C
            group, so you should ask there. However, unless there is a financial
            reason I very much doubt that compiling C code as C++ is the correct
            thing to do, since all C compilers I am aware of come with C compilers
            as well and there are well defined methods for interfacing C and C++
            code. Again, how you interface C and C++ is a C++ issue for a C++ group,
            since it is defined by the C++ side. One good place to discuss what you
            can do with a C++ compiler is the surprisingly named comp.lang.c++ group

            <OTmalloc/frree are available in C++ but there are important
            differences between C and C++ that can trip you up.</OT>
            --
            Flash Gordon

            Comment

            • Flash Gordon

              #7
              Re: compilation of malloc using c++

              somenath wrote, On 13/07/07 17:35:
              On Jul 13, 9:23 pm, dj3va...@csclub .uwaterloo.ca (Dave Vandervies)
              wrote:
              <snip>
              >--
              >Dave Vandervies dj3va...@csclub .uwaterloo.ca
              >When other kids are scared of monsters, your kid might turn out to grab a
              >baseball bat and be disappointed if there WASN'T a monster under the bed...
              > --Juergen Nieveler in the scary devil monastery
              Please don't quote peoples signatures, the bit traditionally after a "--
              ", in fact, you should only quote what you are responding to.
              Thanks for the reply.Actually i need to call some API written using c+
              + .So i need to compile my code using c++ else it does not compile.
              No, you need to ask in comp.lang.c++ about what you can do so you don't
              need to do that, and they may well tell you about the methods that C++
              provides for interfacing with C.
              >But
              in side my code it is required to use malloc.So i was having doubt
              will it cause any problem ?
              There could be any of a number of things in your code that will cause a
              problem. Using malloc as it is often used in C is not legal C++, but you
              have already been told the difference, as to the other things, they are
              other things that will give yo grief and reasons not to go this route
              but to interface C and C++ properly.
              --
              Flash Gordon

              Comment

              • Richard Heathfield

                #8
                Re: compilation of malloc using c++

                somenath said:
                Hi All,
                >
                I need to compile c code using c++ compiler .Inside the c code I am
                using malloc and free.
                1) My doubt is it ok to compile malloc using c++ compiler?
                2)Is behavior will be undefined ?
                Compile C code using a C compiler, and C++ code using a C++ compiler.
                Compiling C code using a C++ compiler is Just Asking For Trouble.

                Link the object code (resulting from the C compilation) to your C++ code
                using a linker.

                Ask comp.lang.c++ for more advice on joining C and C++ code together.

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

                Comment

                • Default User

                  #9
                  Re: compilation of malloc using c++

                  somenath wrote:

                  Thanks for the reply.Actually i need to call some API written using c+
                  + .So i need to compile my code using c++ else it does not compile.But
                  in side my code it is required to use malloc.So i was having doubt
                  will it cause any problem ?
                  Ask C++ questions in the C++ newsgroup, comp.lang.c++.




                  Brian

                  Comment

                  • somenath

                    #10
                    Re: compilation of malloc using c++

                    On Jul 13, 11:32 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
                    somenath said:
                    >
                    Hi All,
                    >
                    I need to compile c code using c++ compiler .Inside the c code I am
                    using malloc and free.
                    1) My doubt is it ok to compile malloc using c++ compiler?
                    2)Is behavior will be undefined ?
                    >
                    Compile C code using a C compiler, and C++ code using a C++ compiler.
                    Compiling C code using a C++ compiler is Just Asking For Trouble.
                    >
                    Link the object code (resulting from the C compilation) to your C++ code
                    using a linker.
                    >
                    Ask comp.lang.c++ for more advice on joining C and C++ code together.
                    >
                    --
                    Richard Heathfield <http://www.cpax.org.uk >
                    Email: -www. +rjh@
                    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                    "Usenet is a strange place" - dmr 29 July 1999
                    Actually I can not compile c code and c++ code separately . I am using
                    third party c code and from that code I need to create object of c++
                    classes. So I need to compile third part c code using c++ compiler .

                    Comment

                    • Malcolm McLean

                      #11
                      Re: compilation of malloc using c++


                      "somenath" <somenathpal@gm ail.comwrote in message
                      news:1184341969 .300288.4530@d3 0g2000prg.googl egroups.com...
                      Hi All,
                      >
                      I need to compile c code using c++ compiler .Inside the c code I am
                      using malloc and free.
                      1) My doubt is it ok to compile malloc using c++ compiler?
                      2)Is behavior will be undefined ?
                      >
                      You can use a C++ compiler asa C compiler with a few glitches. Noticeably
                      the return from malloc() must be cast.

                      This is generally a bad idea, because you will find all sorts of problem if
                      you try to mix the C++ class features with malloc(). The C++ people will
                      tell you all the pitfalls. However it is quite common to see C-like C++ in
                      production code. That probably tells you why most large software projects go
                      over budget.

                      --
                      Free games and programming goodies.


                      Comment

                      • Default User

                        #12
                        Re: compilation of malloc using c++

                        somenath wrote:


                        Actually I can not compile c code and c++ code separately . I am using
                        third party c code and from that code I need to create object of c++
                        classes. So I need to compile third part c code using c++ compiler .

                        Go to comp.lang.c++.



                        Brian

                        Comment

                        • Richard Heathfield

                          #13
                          Re: compilation of malloc using c++

                          somenath said:

                          <snip>
                          Actually I can not compile c code and c++ code separately .
                          Then learn. It isn't difficult.
                          I am using
                          third party c code and from that code I need to create object of c++
                          classes. So I need to compile third part c code using c++ compiler .
                          Um, you can't. If you use a C++ compiler, you're using C++ rules, not C
                          rules, so the code will be treated as if it were written in C++, not C.
                          Same as if you used a Fortran compiler, the compiler would assume the
                          source to be written in Fortran.

                          This can cause no end of confusion. Don't Do It.

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

                          Comment

                          • somenath

                            #14
                            Re: compilation of malloc using c++

                            On Jul 14, 11:16 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
                            somenath said:
                            >
                            <snip>
                            >
                            Actually I can not compile c code and c++ code separately .
                            >
                            Then learn. It isn't difficult.
                            >
                            I am using
                            third party c code and from that code I need to create object of c++
                            classes. So I need to compile third part c code using c++ compiler .
                            >
                            Um, you can't. If you use a C++ compiler, you're using C++ rules, not C
                            rules, so the code will be treated as if it were written in C++, not C.
                            Same as if you used a Fortran compiler, the compiler would assume the
                            source to be written in Fortran.
                            >
                            This can cause no end of confusion. Don't Do It.
                            >
                            --
                            Richard Heathfield <http://www.cpax.org.uk >
                            Email: -www. +rjh@
                            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                            "Usenet is a strange place" - dmr 29 July 1999
                            Thanks for the answer.I would like to know is there any way of doing
                            it ? I mean creating c++ object in c code and calling member
                            functions ?

                            Comment

                            • Richard Heathfield

                              #15
                              Re: compilation of malloc using c++

                              somenath said:

                              <snip>
                              Thanks for the answer.I would like to know is there any way of doing
                              it ? I mean creating c++ object in c code and calling member
                              functions ?
                              C doesn't supply a way to do this. I don't /think/ C++ does either, but
                              you may want to check in comp.lang.c++ for a definitive answer on that
                              subject.

                              The normal way of using C and C++ together is to drive the program from
                              C++, and use C libraries (compiled using a C compiler, naturally) to
                              perform the well-defined tasks for which they were designed. C++ does
                              provide a simple and much-used way to do this.

                              The principal problem with calling C++ methods from C is C++'s penchant
                              for name-mangling, which makes life very awkward indeed for your C
                              code. That's why people tend to do it the other way around.

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

                              Comment

                              Working...