Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. auto_ptr array

auto_ptr array

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structuresperformancehelptutorial
37 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C CPallini

    George_George wrote:

    I am confused. We are talking about destructor of auto_ptr? Why do you say "allocates"?? Could you provide more description please?

    I report below _AnShUmAn_ code for reference.

    auto_ptr<int> p(new int(10));

    In the above expression, the new operator allocates one int and initialise it with the number 10.

    George_George wrote:

    No, I have compile errors in MSVC 2008.

    Well, I have VS2005, and no errors here. However I've to admit I overlooked the following (serious) warning:

    warning C4156: deletion of an array expression without using the array form of 'delete'; array form substituted
    [...]

    :)

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

    G Offline
    G Offline
    George_George
    wrote on last edited by
    #7

    Thanks CPallini, 1. -------------------- deletion of an array expression without using the array form of 'delete' -------------------- Means auto_ptr will use delete other than delete[] is destructor of auto_ptr? 2. -------------------- array form substituted -------------------- What means "array form substituted"? regards, George

    C 1 Reply Last reply
    0
    • G George_George

      Thanks CPallini, 1. -------------------- deletion of an array expression without using the array form of 'delete' -------------------- Means auto_ptr will use delete other than delete[] is destructor of auto_ptr? 2. -------------------- array form substituted -------------------- What means "array form substituted"? regards, George

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #8

      George_George wrote:

      Means auto_ptr will use delete other than delete[] is destructor of auto_ptr?

      I think so.

      George_George wrote:

      What means "array form substituted"?

      as the compiler output window shows

      _Ty=int [10]

      i.e. internal type _Ty (blindly) substitutes an array form. Your example, IMHO shows: (1) The std::auto_ptr though helpful is not a panacea. (2) VC++ compiler is smart. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

      G 1 Reply Last reply
      0
      • C CPallini

        George_George wrote:

        Means auto_ptr will use delete other than delete[] is destructor of auto_ptr?

        I think so.

        George_George wrote:

        What means "array form substituted"?

        as the compiler output window shows

        _Ty=int [10]

        i.e. internal type _Ty (blindly) substitutes an array form. Your example, IMHO shows: (1) The std::auto_ptr though helpful is not a panacea. (2) VC++ compiler is smart. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

        G Offline
        G Offline
        George_George
        wrote on last edited by
        #9

        Thanks CPallini, 1. The compiler deduce _Ty,

        _Ty=int [10]

        is because of the following code?

        auto_ptr<int[10]>

        2. In your sample, compiler will make A. one auto_ptr object wrapps an int array on heap; or B. ten auto_ptr objects and each object wrapps an int on heap? regards, George

        C 1 Reply Last reply
        0
        • G George_George

          Thanks CPallini, 1. The compiler deduce _Ty,

          _Ty=int [10]

          is because of the following code?

          auto_ptr<int[10]>

          2. In your sample, compiler will make A. one auto_ptr object wrapps an int array on heap; or B. ten auto_ptr objects and each object wrapps an int on heap? regards, George

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #10

          1. Yes. 2. A. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

          G 1 Reply Last reply
          0
          • C CPallini

            1. Yes. 2. A. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

            G Offline
            G Offline
            George_George
            wrote on last edited by
            #11

            Thanks CPallini, I read the compile error message you posted, -------------------- warning C4156: deletion of an array expression without using the array form of 'delete' -------------------- I think your code will have memory leak potentially? Because delete other than delete[] will work on the array? regards, George

            C 1 Reply Last reply
            0
            • G George_George

              Thanks CPallini, I read the compile error message you posted, -------------------- warning C4156: deletion of an array expression without using the array form of 'delete' -------------------- I think your code will have memory leak potentially? Because delete other than delete[] will work on the array? regards, George

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #12

              George_George wrote:

              I think your code will have memory leak potentially? Because delete other than delete[] will work on the array?

              The delete syntax will be used (insted of the delete [] one). About potential memory leaks, from MSDN [^] The following two cases produce undefined results: using the array form of delete (delete [ ]) on an object and using the nonarray form of delete on an array. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

              G 1 Reply Last reply
              0
              • C CPallini

                George_George wrote:

                I think your code will have memory leak potentially? Because delete other than delete[] will work on the array?

                The delete syntax will be used (insted of the delete [] one). About potential memory leaks, from MSDN [^] The following two cases produce undefined results: using the array form of delete (delete [ ]) on an object and using the nonarray form of delete on an array. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                G Offline
                G Offline
                George_George
                wrote on last edited by
                #13

                You mean your code will cause undefined behavior? CPallini? regards, George

                L C 2 Replies Last reply
                0
                • G George_George

                  You mean your code will cause undefined behavior? CPallini? regards, George

                  L Offline
                  L Offline
                  led mike
                  wrote on last edited by
                  #14

                  Why do you ask people to repeat themselves all the time? Why?

                  led mike

                  G C 2 Replies Last reply
                  0
                  • L led mike

                    Why do you ask people to repeat themselves all the time? Why?

                    led mike

                    G Offline
                    G Offline
                    George_George
                    wrote on last edited by
                    #15

                    Sorry led mike, What is your reply to my original question? How to make an auto_ptr array and initialize it? regards, George

                    L 1 Reply Last reply
                    0
                    • L led mike

                      Why do you ask people to repeat themselves all the time? Why?

                      led mike

                      C Offline
                      C Offline
                      Cedric Moonen
                      wrote on last edited by
                      #16

                      So this way he can read the answer once again, just to be sure he read it correctly ;P

                      Cédric Moonen Software developer
                      Charting control [v1.3]

                      G 1 Reply Last reply
                      0
                      • G George_George

                        You mean your code will cause undefined behavior? CPallini? regards, George

                        C Offline
                        C Offline
                        CPallini
                        wrote on last edited by
                        #17

                        George_George wrote:

                        You mean your code will cause undefined behavior? CPallini?

                        Well, MSDN says it and of course I cannot object to Microsoft. See here [^]. :)

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                        modified on Tuesday, April 1, 2008 1:55 PM

                        G 1 Reply Last reply
                        0
                        • G George_George

                          Sorry led mike, What is your reply to my original question? How to make an auto_ptr array and initialize it? regards, George

                          L Offline
                          L Offline
                          led mike
                          wrote on last edited by
                          #18

                          George_George wrote:

                          What is your reply to my original question?

                          I have no answer for it. I don't understand the premise. auto_ptr should be used to implement exception safe locality and I don't understand the need to have an array of int pointers for local use. I would just put the ints on the stack and I would not use an array I would use a vector.

                          led mike

                          G 1 Reply Last reply
                          0
                          • G George_George

                            Hello everyone, I have tried to initialize an auto_ptr array, but failed. My C++ Programming Language book does not contain a sample about how to initialize an auto_ptr array. (not an auto_ptr pointing to an array, which is not legal) Any solutions?

                            #include <memory>

                            using namespace std;

                            int main()
                            {
                            auto_ptr<int[]> pi (new int[10]); // compile error

                            auto\_ptr<int> pi (new int\[10\]); // compile error
                            
                            return 0;
                            

                            }

                            thanks in advance, George

                            A Offline
                            A Offline
                            ankita patel 0
                            wrote on last edited by
                            #19

                            As you have already discovered auto_ptr is not designed to work as an array of pointers. There are multiple solutions to your problem and they are described in the below links. http://www.codeproject.com/KB/cpp/COAP.aspx[^] http://www.gotw.ca/gotw/042.htm[^] If you are just interested finding out how auto_ptr can be used with an array then above links are suffice. but in real use, you might want to take a look at the boost smart pointer library. you can also use the shared_ptr as it is designed to work with STL containers. Ankita

                            G 1 Reply Last reply
                            0
                            • C CPallini

                              George_George wrote:

                              You mean your code will cause undefined behavior? CPallini?

                              Well, MSDN says it and of course I cannot object to Microsoft. See here [^]. :)

                              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                              modified on Tuesday, April 1, 2008 1:55 PM

                              G Offline
                              G Offline
                              George_George
                              wrote on last edited by
                              #20

                              Sure, CPallini! About my original question, your option is we can not define an auto_ptr array and do initialization at the same time? regards, George

                              C 1 Reply Last reply
                              0
                              • L led mike

                                George_George wrote:

                                What is your reply to my original question?

                                I have no answer for it. I don't understand the premise. auto_ptr should be used to implement exception safe locality and I don't understand the need to have an array of int pointers for local use. I would just put the ints on the stack and I would not use an array I would use a vector.

                                led mike

                                G Offline
                                G Offline
                                George_George
                                wrote on last edited by
                                #21

                                Hi led mike, int is just used for demo purpose. You can use user defined data types, like class Foo. How to define an array of auto_ptr and initialization at the same time? regards, George

                                L 1 Reply Last reply
                                0
                                • C Cedric Moonen

                                  So this way he can read the answer once again, just to be sure he read it correctly ;P

                                  Cédric Moonen Software developer
                                  Charting control [v1.3]

                                  G Offline
                                  G Offline
                                  George_George
                                  wrote on last edited by
                                  #22

                                  I agree, Cedric! Confirmation is good practice for the flat world. Since you are not sit next to me, or climb through the network cable. :-) regards, George

                                  1 Reply Last reply
                                  0
                                  • A ankita patel 0

                                    As you have already discovered auto_ptr is not designed to work as an array of pointers. There are multiple solutions to your problem and they are described in the below links. http://www.codeproject.com/KB/cpp/COAP.aspx[^] http://www.gotw.ca/gotw/042.htm[^] If you are just interested finding out how auto_ptr can be used with an array then above links are suffice. but in real use, you might want to take a look at the boost smart pointer library. you can also use the shared_ptr as it is designed to work with STL containers. Ankita

                                    G Offline
                                    G Offline
                                    George_George
                                    wrote on last edited by
                                    #23

                                    Hi Ankita, I have not made myself understood. My question is (say in another way), how to define an array of auto_ptr and initialization at the same time of definition? Any ideas? regards, George

                                    1 Reply Last reply
                                    0
                                    • G George_George

                                      Sure, CPallini! About my original question, your option is we can not define an auto_ptr array and do initialization at the same time? regards, George

                                      C Offline
                                      C Offline
                                      CPallini
                                      wrote on last edited by
                                      #24

                                      Do you need an array of auto_ptr?

                                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                                      G 1 Reply Last reply
                                      0
                                      • C CPallini

                                        Do you need an array of auto_ptr?

                                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                                        G Offline
                                        G Offline
                                        George_George
                                        wrote on last edited by
                                        #25

                                        Sure, CPallini. My requirement is, I need to have an array of pointers to class Goo, wrapped in class Foo as member variables. I want to make them auto_ptr array to make it exception safe. Do you think in this situation using auto_ptr array is a good idea? If you have better ideas, please feel free to let me know. :-) regards, George

                                        C 1 Reply Last reply
                                        0
                                        • G George_George

                                          Sure, CPallini. My requirement is, I need to have an array of pointers to class Goo, wrapped in class Foo as member variables. I want to make them auto_ptr array to make it exception safe. Do you think in this situation using auto_ptr array is a good idea? If you have better ideas, please feel free to let me know. :-) regards, George

                                          C Offline
                                          C Offline
                                          CPallini
                                          wrote on last edited by
                                          #26

                                          You probably need something like the following: (Sample for a 3-items array)

                                          auto_ptr<Foo> pi[3]={auto_ptr<Foo>(new Foo), auto_ptr<Foo>(new Foo), auto_ptr<Foo>(new Foo)};

                                          :)

                                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                                          G 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups