What is the best alternative for realloc in C++

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

    What is the best alternative for realloc in C++

    Hi,
    Can somebody advise me on what is the best alternative for realloc in C++?
    Is it safe to "realloc"at e memory allocated using new operator ?
  • Rolf Magnus

    #2
    Re: What is the best alternative for realloc in C++

    Kumar wrote:
    [color=blue]
    > Hi,
    > Can somebody advise me on what is the best alternative for realloc in
    > C++?[/color]

    What would you need it for?
    [color=blue]
    > Is it safe to "realloc"at e memory allocated using new operator ?[/color]

    No. You can only use realloc on memory that you got from malloc or
    realloc. Also, realloc won't work properly with non-pod objects, since
    it doesn't care about constructors and destructors.

    Comment

    • Rolf Magnus

      #3
      Re: What is the best alternative for realloc in C++

      Kumar wrote:
      [color=blue]
      > Hi,
      > Can somebody advise me on what is the best alternative for realloc in
      > C++?[/color]

      What would you need it for?
      [color=blue]
      > Is it safe to "realloc"at e memory allocated using new operator ?[/color]

      No. You can only use realloc on memory that you got from malloc or
      realloc. Also, realloc won't work properly with non-pod objects, since
      it doesn't care about constructors and destructors.

      Comment

      • David Harmon

        #4
        Re: What is the best alternative for realloc in C++

        On 6 Apr 2004 02:12:33 -0700 in comp.lang.c++, rsmkumar@acmet. com
        (Kumar) wrote,[color=blue]
        >Hi,
        >Can somebody advise me on what is the best alternative for realloc in C++?[/color]

        std::vector<>:: resize()
        [color=blue]
        >Is it safe to "realloc"at e memory allocated using new operator ?[/color]

        Don't use 'new'.

        Comment

        • David Harmon

          #5
          Re: What is the best alternative for realloc in C++

          On 6 Apr 2004 02:12:33 -0700 in comp.lang.c++, rsmkumar@acmet. com
          (Kumar) wrote,[color=blue]
          >Hi,
          >Can somebody advise me on what is the best alternative for realloc in C++?[/color]

          std::vector<>:: resize()
          [color=blue]
          >Is it safe to "realloc"at e memory allocated using new operator ?[/color]

          Don't use 'new'.

          Comment

          • Kevin Goodsell

            #6
            Re: What is the best alternative for realloc in C++

            Rolf Magnus wrote:
            [color=blue]
            > Kumar wrote:
            >[color=green]
            >>Is it safe to "realloc"at e memory allocated using new operator ?[/color]
            >
            >
            > No. You can only use realloc on memory that you got from malloc or
            > realloc.[/color]

            ....or calloc().
            [color=blue]
            > Also, realloc won't work properly with non-pod objects, since
            > it doesn't care about constructors and destructors.
            >[/color]

            Likewise with malloc(), calloc(), and free(), which is why these should
            only be used when absolutely necessary, and only by people who really
            know what they are doing.

            -Kevin
            --
            My email address is valid, but changes periodically.
            To contact me please use the address from a recent posting.

            Comment

            • Kevin Goodsell

              #7
              Re: What is the best alternative for realloc in C++

              Rolf Magnus wrote:
              [color=blue]
              > Kumar wrote:
              >[color=green]
              >>Is it safe to "realloc"at e memory allocated using new operator ?[/color]
              >
              >
              > No. You can only use realloc on memory that you got from malloc or
              > realloc.[/color]

              ....or calloc().
              [color=blue]
              > Also, realloc won't work properly with non-pod objects, since
              > it doesn't care about constructors and destructors.
              >[/color]

              Likewise with malloc(), calloc(), and free(), which is why these should
              only be used when absolutely necessary, and only by people who really
              know what they are doing.

              -Kevin
              --
              My email address is valid, but changes periodically.
              To contact me please use the address from a recent posting.

              Comment

              • Kevin Goodsell

                #8
                Re: What is the best alternative for realloc in C++

                David Harmon wrote:
                [color=blue]
                >
                > Don't use 'new'.
                >[/color]

                Reasonable advice, but just to be perfectly clear:

                1) Don't use malloc() (and friends) when 'new' can do the job.

                2) Don't use malloc() or 'new' when containers or automatic variables
                can do the job.

                3) When explicit memory management is necessary, try to use smart
                pointers to make it easier and safer.

                4) If you do have to use any form of explicit memory management, use
                caution.

                -Kevin
                --
                My email address is valid, but changes periodically.
                To contact me please use the address from a recent posting.

                Comment

                • Kevin Goodsell

                  #9
                  Re: What is the best alternative for realloc in C++

                  David Harmon wrote:
                  [color=blue]
                  >
                  > Don't use 'new'.
                  >[/color]

                  Reasonable advice, but just to be perfectly clear:

                  1) Don't use malloc() (and friends) when 'new' can do the job.

                  2) Don't use malloc() or 'new' when containers or automatic variables
                  can do the job.

                  3) When explicit memory management is necessary, try to use smart
                  pointers to make it easier and safer.

                  4) If you do have to use any form of explicit memory management, use
                  caution.

                  -Kevin
                  --
                  My email address is valid, but changes periodically.
                  To contact me please use the address from a recent posting.

                  Comment

                  • Evan Carew

                    #10
                    Re: What is the best alternative for realloc in C++

                    -----BEGIN PGP SIGNED MESSAGE-----
                    Hash: SHA1

                    Kumar wrote:[color=blue]
                    > Hi,
                    > Can somebody advise me on what is the best alternative for realloc in C++?
                    > Is it safe to "realloc"at e memory allocated using new operator ?[/color]
                    Kumar,

                    The simple answer is that realloc is nothing more than a fancy version
                    of malloc, ie new. So if you are working with STL compliant objects &
                    containers, then simply setting one equal to another will suffice, then
                    either let the old object go out of scope (if automatic), or delete it.

                    Evan
                    -----BEGIN PGP SIGNATURE-----
                    Version: GnuPG v1.0.6 (GNU/Linux)
                    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

                    iD8DBQFAc0OVoo/Prlj9GScRApXyAJ 0UKj7WWWX5v98Ad CdCyrm1fwO4PQCd FJ61
                    M1vu6exq008FY2X rmEXkrkk=
                    =4O+j
                    -----END PGP SIGNATURE-----

                    Comment

                    • Evan Carew

                      #11
                      Re: What is the best alternative for realloc in C++

                      -----BEGIN PGP SIGNED MESSAGE-----
                      Hash: SHA1

                      Kumar wrote:[color=blue]
                      > Hi,
                      > Can somebody advise me on what is the best alternative for realloc in C++?
                      > Is it safe to "realloc"at e memory allocated using new operator ?[/color]
                      Kumar,

                      The simple answer is that realloc is nothing more than a fancy version
                      of malloc, ie new. So if you are working with STL compliant objects &
                      containers, then simply setting one equal to another will suffice, then
                      either let the old object go out of scope (if automatic), or delete it.

                      Evan
                      -----BEGIN PGP SIGNATURE-----
                      Version: GnuPG v1.0.6 (GNU/Linux)
                      Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

                      iD8DBQFAc0OVoo/Prlj9GScRApXyAJ 0UKj7WWWX5v98Ad CdCyrm1fwO4PQCd FJ61
                      M1vu6exq008FY2X rmEXkrkk=
                      =4O+j
                      -----END PGP SIGNATURE-----

                      Comment

                      • Kevin Goodsell

                        #12
                        Re: What is the best alternative for realloc in C++

                        Evan Carew wrote:[color=blue]
                        > malloc, ie new.[/color]

                        The implication that malloc() and 'new' are equivalent is very wrong.
                        There are significant differences, for example: malloc() returns NULL on
                        failure while 'new' throws an exception, malloc() allocates raw memory
                        while 'new' constructs objects in the allocated space, malloc() is a
                        single function while 'new' comes in several variants and can be
                        overloaded or overridden by the programmer.

                        -Kevin
                        --
                        My email address is valid, but changes periodically.
                        To contact me please use the address from a recent posting.

                        Comment

                        • Kevin Goodsell

                          #13
                          Re: What is the best alternative for realloc in C++

                          Evan Carew wrote:[color=blue]
                          > malloc, ie new.[/color]

                          The implication that malloc() and 'new' are equivalent is very wrong.
                          There are significant differences, for example: malloc() returns NULL on
                          failure while 'new' throws an exception, malloc() allocates raw memory
                          while 'new' constructs objects in the allocated space, malloc() is a
                          single function while 'new' comes in several variants and can be
                          overloaded or overridden by the programmer.

                          -Kevin
                          --
                          My email address is valid, but changes periodically.
                          To contact me please use the address from a recent posting.

                          Comment

                          • Evan Carew

                            #14
                            Re: What is the best alternative for realloc in C++

                            -----BEGIN PGP SIGNED MESSAGE-----
                            Hash: SHA1

                            Kumar,

                            I'm sure Kevin means well, but I think you would do well to put his
                            posts in your filter list. The problem with his post is that most
                            compiler vendors do indeed use malloc under the hood as the default new
                            operator's machenery. While many vendors do add additional functionality
                            to new beside what malloc provides, ultimately, you are interfacing with
                            the OS's memory machenery, and thus must use its entry points. In the
                            UNIX world, this happens to be malloc (I can't imagine it would be any
                            different with MS products).

                            Evan Carew
                            -----BEGIN PGP SIGNATURE-----
                            Version: GnuPG v1.0.6 (GNU/Linux)
                            Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

                            iD8DBQFAdCPmoo/Prlj9GScRAnl5AJ 49sHLYuak9OAOLv XGdExJKqn2PrACf fZQ2
                            Pv1ZYvqReZFHmBY 1cbdHnQo=
                            =bC5J
                            -----END PGP SIGNATURE-----

                            Comment

                            • Evan Carew

                              #15
                              Re: What is the best alternative for realloc in C++

                              -----BEGIN PGP SIGNED MESSAGE-----
                              Hash: SHA1

                              Kumar,

                              I'm sure Kevin means well, but I think you would do well to put his
                              posts in your filter list. The problem with his post is that most
                              compiler vendors do indeed use malloc under the hood as the default new
                              operator's machenery. While many vendors do add additional functionality
                              to new beside what malloc provides, ultimately, you are interfacing with
                              the OS's memory machenery, and thus must use its entry points. In the
                              UNIX world, this happens to be malloc (I can't imagine it would be any
                              different with MS products).

                              Evan Carew
                              -----BEGIN PGP SIGNATURE-----
                              Version: GnuPG v1.0.6 (GNU/Linux)
                              Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

                              iD8DBQFAdCPmoo/Prlj9GScRAnl5AJ 49sHLYuak9OAOLv XGdExJKqn2PrACf fZQ2
                              Pv1ZYvqReZFHmBY 1cbdHnQo=
                              =bC5J
                              -----END PGP SIGNATURE-----

                              Comment

                              Working...