Garbage collection in C++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pushpakulkar@gmail.com

    Garbage collection in C++

    Hi all,

    Is garbage collection possible in C++. It doesn't come as part of
    language support. Is there any specific reason for the same due to the
    way the language is designed. Or it is discouraged due to
    some specific reason. If someone can give inputs on the same, it will
    be of great help.

    Regards,
    Pushpa
  • Chris M. Thomasson

    #2
    Re: Garbage collection in C++

    <pushpakulkar@g mail.comwrote in message
    news:22b430aa-4990-4dcf-9e2f-9e828427986c@a2 9g2000pra.googl egroups.com...
    Hi all,
    >
    Is garbage collection possible in C++.
    Sure:





    It doesn't come as part of
    language support. Is there any specific reason for the same due to the
    way the language is designed. Or it is discouraged due to
    some specific reason. If someone can give inputs on the same, it will
    be of great help.
    C++ is a low-level systems language, IMVHO, that's no place for a GC to
    be...

    Comment

    • Bharath

      #3
      Re: Garbage collection in C++

      On Nov 15, 10:32 am, "Chris M. Thomasson" <n...@spam.inva lidwrote:
      <pushpakul...@g mail.comwrote in message
      >
      news:22b430aa-4990-4dcf-9e2f-9e828427986c@a2 9g2000pra.googl egroups.com...
      >
      Hi all,
      >
      Is garbage collection possible in C++.
      >
      Sure:
      >

      >
      It doesn't come as part of
      language support. Is there any specific reason for the same due to the
      way the language is designed. Or it is discouraged due to
      some specific reason. If someone can give inputs on the same, it will
      be of great help.
      >
      C++ is a low-level systems language, IMVHO, that's no place for a GC to
      be...
      Check out: http://www.research.att.com/~bs/bs_f...age-collection

      - bharath

      Comment

      • Juha Nieminen

        #4
        Re: Garbage collection in C++

        pushpakulkar@gm ail.com wrote:
        Is garbage collection possible in C++. It doesn't come as part of
        language support. Is there any specific reason for the same due to the
        way the language is designed. Or it is discouraged due to
        some specific reason. If someone can give inputs on the same, it will
        be of great help.
        I have absolutely no experience in third-party garbage collectors for
        C++. What I have always wondered, though, is how those can handle a
        situation like:

        int* foo()
        {
        int* table = new int[100];
        return table+40;
        }

        The pointer to the beginning of the allocated array dies when foo()
        terminates, but a pointer to one of its elements lives beyond the scope
        of foo(). This pointer may be used in the calling code. A garbage
        collector would have to:

        1) Know that there's still a live pointer pointing to (an element
        inside) the array, and thus it cannot destroy it yet.

        2) Know to delete the array properly (ie. from the original pointer
        pointing to the beginning of the array, rather than the one which lived
        longer than that) when that returned pointer dies and the GC runs.

        I suppose they have figured out these problems. I'm just wondering how.

        Comment

        • Pete Becker

          #5
          Re: Garbage collection in C++

          On 2008-11-15 12:21:06 -0500, Juha Nieminen <nospam@thanks. invalidsaid:
          >
          I have absolutely no experience in third-party garbage collectors for
          C++. What I have always wondered, though, is how those can handle a
          situation like:
          >
          int* foo()
          {
          int* table = new int[100];
          return table+40;
          }
          >
          The pointer to the beginning of the allocated array dies when foo()
          terminates, but a pointer to one of its elements lives beyond the scope
          of foo(). This pointer may be used in the calling code. A garbage
          collector would have to:
          >
          1) Know that there's still a live pointer pointing to (an element
          inside) the array, and thus it cannot destroy it yet.
          >
          2) Know to delete the array properly (ie. from the original pointer
          pointing to the beginning of the array, rather than the one which lived
          longer than that) when that returned pointer dies and the GC runs.
          >
          I suppose they have figured out these problems. I'm just wondering how.
          It's just a bit of bookkeeping. When the array is allocated the
          collector makes notes about the size of the allocated block and where
          it begins. The returned pointer points into the allocated block, so the
          block is still live.

          --
          Pete
          Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
          Standard C++ Library Extensions: a Tutorial and Reference
          (www.petebecker.com/tr1book)

          Comment

          • Joe Smith

            #6
            Re: Garbage collection in C++


            <pushpakulkar@g mail.comwrote in message
            news:22b430aa-4990-4dcf-9e2f-9e828427986c@a2 9g2000pra.googl egroups.com...
            Hi all,
            >
            Is garbage collection possible in C++. It doesn't come as part of
            language support. Is there any specific reason for the same due to the
            way the language is designed. Or it is discouraged due to
            some specific reason. If someone can give inputs on the same, it will
            be of great help.
            The C++ standardization commitee is working with some garbage collection
            related proposals,
            although no official garbage collection mechanism will be included in C++0x.
            It is not yet clear
            if the auxillary proposal to make it easier to support garbage collection
            via third party libraries
            will be approved for C++0x or not.

            Comment

            • Pete Becker

              #7
              Re: Garbage collection in C++

              On 2008-11-15 13:44:20 -0500, "Joe Smith" <unknown_kev_ca t@hotmail.comsa id:
              >
              <pushpakulkar@g mail.comwrote in message
              news:22b430aa-4990-4dcf-9e2f-9e828427986c@a2 9g2000pra.googl egroups.com...
              Hi
              >
              >all,
              >>
              >Is garbage collection possible in C++. It doesn't come as part of
              >language support. Is there any specific reason for the same due to the
              >way the language is designed. Or it is discouraged due to
              >some specific reason. If someone can give inputs on the same, it will
              >be of great help.
              >
              The C++ standardization commitee is working with some garbage
              collection related proposals,
              although no official garbage collection mechanism will be included in
              C++0x. It is not yet clear
              if the auxillary proposal to make it easier to support garbage
              collection via third party libraries
              will be approved for C++0x or not.
              It's quite clear, since it was approved at the September meeting. <g>
              Search for "safely derived pointer" in the current working draft.

              --
              Pete
              Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
              Standard C++ Library Extensions: a Tutorial and Reference
              (www.petebecker.com/tr1book)

              Comment

              • Joe Smith

                #8
                Re: Garbage collection in C++


                "Pete Becker" <pete@versatile coding.comwrote in message
                news:2008111514 561516807-pete@versatilec odingcom...
                On 2008-11-15 13:44:20 -0500, "Joe Smith" <unknown_kev_ca t@hotmail.com>
                said:
                >The C++ standardization commitee is working with some garbage collection
                >related proposals,
                >although no official garbage collection mechanism will be included in
                >C++0x. It is not yet clear
                >if the auxillary proposal to make it easier to support garbage collection
                >via third party libraries
                >will be approved for C++0x or not.
                >
                It's quite clear, since it was approved at the September meeting. <g>
                Search for "safely derived pointer" in the current working draft.
                >
                Roger that, I should have been more clear that it was not clear to me
                if the auxillary proposal was included. I'm trying to follow things, but
                I am not reading every posted paper, so I may well miss things.

                Comment

                • Chris M. Thomasson

                  #9
                  Re: Garbage collection in C++

                  "Chris M. Thomasson" <no@spam.invali dwrote in message
                  news:7OCTk.31$m 74.24@newsfe24. iad...
                  <pushpakulkar@g mail.comwrote in message
                  news:22b430aa-4990-4dcf-9e2f-9e828427986c@a2 9g2000pra.googl egroups.com...
                  >Hi all,
                  >>
                  >Is garbage collection possible in C++.
                  >
                  Sure:
                  >

                  >
                  >
                  >
                  >
                  >It doesn't come as part of
                  >language support. Is there any specific reason for the same due to the
                  >way the language is designed. Or it is discouraged due to
                  >some specific reason. If someone can give inputs on the same, it will
                  >be of great help.
                  >
                  C++ is a low-level systems language,
                  C++ is nice because it allows the user to apply just enough of its features
                  to get the job done. You can use C++ for kernel programming; just not all of
                  it...

                  ;^D


                  IMVHO, that's no place for a GC to be...

                  Comment

                  • James Kanze

                    #10
                    Re: Garbage collection in C++

                    On Nov 15, 4:57 pm, pushpakul...@gm ail.com wrote:
                    Is garbage collection possible in C++.
                    Yes and no. There are, in fact, a few constructs which could
                    break it, and there are potential compiler optimizations which
                    could prevent it from being used.. In practice, the language
                    constructs are recognized as poor programming practice, and
                    something to be avoided, regardless, and compilers don't do
                    optimizations which would break it, and in fact, it is more or
                    less widely used; see
                    http://www.hpl.hp.com/personal/Hans_Boehm/gc/.
                    It doesn't come as part of language support. Is there any
                    specific reason for the same due to the way the language is
                    designed. Or it is discouraged due to some specific reason. If
                    someone can give inputs on the same, it will be of great help.
                    More history than any other reasons. It's readily available,
                    and works, and is being used by a number of people.

                    --
                    James Kanze (GABI Software) email:james.kan ze@gmail.com
                    Conseils en informatique orientée objet/
                    Beratung in objektorientier ter Datenverarbeitu ng
                    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                    Comment

                    • James Kanze

                      #11
                      Re: Garbage collection in C++

                      On Nov 15, 5:32 pm, "Chris M. Thomasson" <n...@spam.inva lidwrote:
                      <pushpakul...@g mail.comwrote in message
                      news:22b430aa-4990-4dcf-9e2f-9e828427986c@a2 9g2000pra.googl egroups.com...
                      Is garbage collection possible in C++.
                      Sure:
                      It doesn't come as part of language support. Is there any
                      specific reason for the same due to the way the language is
                      designed. Or it is discouraged due to some specific reason.
                      If someone can give inputs on the same, it will be of great
                      help.
                      C++ is a low-level systems language, IMVHO, that's no place
                      for a GC to be...
                      C++ is a multi-paradigm language, usable in many contexts. If
                      you're writing kernel code, a garbage collector certainly has no
                      place; nor do exceptions, for that matter. And if you're
                      implementing a garbage collector, obviously, you can't use it.
                      But for most application programs, it's stupid not to.

                      --
                      James Kanze (GABI Software) email:james.kan ze@gmail.com
                      Conseils en informatique orientée objet/
                      Beratung in objektorientier ter Datenverarbeitu ng
                      9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                      Comment

                      • George Kettleborough

                        #12
                        Re: Garbage collection in C++

                        On 15/11/08 15:57, pushpakulkar@gm ail.com wrote:
                        Hi all,
                        >
                        Is garbage collection possible in C++. It doesn't come as part of
                        language support. Is there any specific reason for the same due to the
                        way the language is designed. Or it is discouraged due to
                        some specific reason. If someone can give inputs on the same, it will
                        be of great help.
                        >
                        Regards,
                        Pushpa
                        A related question if I may: Is garbage collection necessary if the
                        programmer follows a strict RAII design ie. only allocating and
                        deallocating resources in constructors and destructors? Is garbage
                        collection just to enable you to program more like Java ie. creating
                        objects on the heap by using new everywhere? What is the advantage of this?

                        Java also has the finally block, and I understand this won't be
                        implemented in C++(0x) any time soon because it's not necessary with
                        RAII. Can garbage collection in C++ work to the same extent as it does
                        in Java without the finally block?

                        --
                        George Kettleborough

                        Comment

                        • Ian Collins

                          #13
                          Re: Garbage collection in C++

                          George Kettleborough wrote:
                          On 15/11/08 15:57, pushpakulkar@gm ail.com wrote:
                          >Hi all,
                          >>
                          >Is garbage collection possible in C++. It doesn't come as part of
                          >language support. Is there any specific reason for the same due to the
                          >way the language is designed. Or it is discouraged due to
                          >some specific reason. If someone can give inputs on the same, it will
                          >be of great help.
                          >>
                          >Regards,
                          >Pushpa
                          >
                          A related question if I may: Is garbage collection necessary if the
                          programmer follows a strict RAII design ie. only allocating and
                          deallocating resources in constructors and destructors? Is garbage
                          collection just to enable you to program more like Java ie. creating
                          objects on the heap by using new everywhere? What is the advantage of this?
                          >
                          GC and RAII are orthogonal concepts.
                          Java also has the finally block, and I understand this won't be
                          implemented in C++(0x) any time soon because it's not necessary with
                          RAII. Can garbage collection in C++ work to the same extent as it does
                          in Java without the finally block?
                          >
                          Yes.

                          --
                          Ian Collins

                          Comment

                          • James Kanze

                            #14
                            Re: Garbage collection in C++

                            On Nov 16, 8:38 am, George Kettleborough
                            <g.kettleboro.. .@member.fsf.or gwrote:
                            On 15/11/08 15:57, pushpakul...@gm ail.com wrote:
                            Is garbage collection possible in C++. It doesn't come as
                            part of language support. Is there any specific reason for
                            the same due to the way the language is designed. Or it is
                            discouraged due to some specific reason. If someone can give
                            inputs on the same, it will be of great help.
                            A related question if I may: Is garbage collection necessary
                            if the programmer follows a strict RAII design ie. only
                            allocating and deallocating resources in constructors and
                            destructors?
                            It rarely makes sense to allocate memory in constructors and
                            deallocate in destructors. If that's what you're doing, it's
                            generally preferable to use an object directly, with no dynamic
                            allocation. There are exceptions, of course, but they're a lot
                            less frequent than people make out. The main motivation for
                            using dynamic allocation is precisely because the lifetime of
                            the object must be arbitrary, and doesn't fit any pre-defined
                            pattern.
                            Is garbage collection just to enable you to program more like
                            Java ie. creating objects on the heap by using new everywhere?
                            What is the advantage of this?
                            At the lowest level, garbage collection has two major effects:
                            it allows memory which can no longer be accessed to be reused
                            (without programmer intervention), and it forbids memory which
                            can still be accessed from being reused. The first results in
                            less work for the programmer in a number of specific instances;
                            it isn't the panacea that some Java proponents would have us
                            believe, but every little bit helps. The second results in more
                            robust programs; the memory for one object can't be reused for
                            another object due to a premature delete. (Premature delete is
                            a bug that can be exploited to break security, so you definitly
                            want garbage collection for this reason if your program connects
                            directly to the Internet.)
                            Java also has the finally block, and I understand this won't
                            be implemented in C++(0x) any time soon because it's not
                            necessary with RAII. Can garbage collection in C++ work to the
                            same extent as it does in Java without the finally block?
                            What does garbage collection have to do with the finally block?
                            For that matter, at the level garbage collection works, what is
                            the difference between a finally block and the destructor of a
                            local object?

                            --
                            James Kanze (GABI Software) email:james.kan ze@gmail.com
                            Conseils en informatique orientée objet/
                            Beratung in objektorientier ter Datenverarbeitu ng
                            9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                            Comment

                            • George Kettleborough

                              #15
                              Re: Garbage collection in C++

                              On 16/11/08 09:43, James Kanze wrote:
                              On Nov 16, 8:38 am, George Kettleborough
                              <g.kettleboro.. .@member.fsf.or gwrote:
                              >On 15/11/08 15:57, pushpakul...@gm ail.com wrote:
                              >
                              >>Is garbage collection possible in C++. It doesn't come as
                              >>part of language support. Is there any specific reason for
                              >>the same due to the way the language is designed. Or it is
                              >>discouraged due to some specific reason. If someone can give
                              >>inputs on the same, it will be of great help.
                              >
                              >A related question if I may: Is garbage collection necessary
                              >if the programmer follows a strict RAII design ie. only
                              >allocating and deallocating resources in constructors and
                              >destructors?
                              >
                              It rarely makes sense to allocate memory in constructors and
                              deallocate in destructors. If that's what you're doing, it's
                              generally preferable to use an object directly, with no dynamic
                              allocation. There are exceptions, of course, but they're a lot
                              less frequent than people make out. The main motivation for
                              using dynamic allocation is precisely because the lifetime of
                              the object must be arbitrary, and doesn't fit any pre-defined
                              pattern.
                              How would you implement something like a vector without dynamically
                              allocated memory? I thought dynamically allocated memory was used
                              because the amount of memory needed can only be decided at run time.
                              >Is garbage collection just to enable you to program more like
                              >Java ie. creating objects on the heap by using new everywhere?
                              >What is the advantage of this?
                              >
                              At the lowest level, garbage collection has two major effects:
                              it allows memory which can no longer be accessed to be reused
                              (without programmer intervention), and it forbids memory which
                              can still be accessed from being reused. The first results in
                              less work for the programmer in a number of specific instances;
                              it isn't the panacea that some Java proponents would have us
                              believe, but every little bit helps. The second results in more
                              robust programs; the memory for one object can't be reused for
                              another object due to a premature delete. (Premature delete is
                              a bug that can be exploited to break security, so you definitly
                              want garbage collection for this reason if your program connects
                              directly to the Internet.)
                              I suppose what I don't get is why you would ever want to create objects
                              on the stack. It's something you can't do in Java, and it's much quicker
                              to create them on the stack plus they get destructed automatically when
                              the go out of scope.
                              >Java also has the finally block, and I understand this won't
                              >be implemented in C++(0x) any time soon because it's not
                              >necessary with RAII. Can garbage collection in C++ work to the
                              >same extent as it does in Java without the finally block?
                              >
                              What does garbage collection have to do with the finally block?
                              For that matter, at the level garbage collection works, what is
                              the difference between a finally block and the destructor of a
                              local object?
                              Well, if you are using new in a try block, and an exception is thrown,
                              C++ will guarantee the destruction of the pointer and anything else on
                              the stack but it won't call delete for what you allocated there. I
                              thought the finally block was to "clean up" any resources you allocated
                              in the try block, something that isn't necessary if you don't
                              dynamically allocate resources in there (but instead in the constructors
                              of objects create there, which are guaranteed to be destroyed).

                              Maybe I have got this all wrong and I am confusing myself, forgive me
                              because I'm only just beginning to really learn C++!

                              --
                              George Kettleborough

                              Comment

                              Working...