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

    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
      • C CPallini

        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 Offline
        G Offline
        George_George
        wrote on last edited by
        #27

        Thanks CPallini, Do we have any ways to saving typing? regards, George

        C 1 Reply Last reply
        0
        • G George_George

          Thanks CPallini, Do we have any ways to saving typing? regards, George

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

          Yes: employ someone and let he/she do the job for you. :-D

          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

            Yes: employ someone and let he/she do the job for you. :-D

            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
            #29

            Thanks CPallini, I believe it is a limitation for initialization approach for auto_ptr array. :-) regards, George

            C 1 Reply Last reply
            0
            • G George_George

              Thanks CPallini, I believe it is a limitation for initialization approach for auto_ptr array. :-) regards, George

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

              That applies to all arrays initialization. Actually I dont know if there is a smarter way to do it. :)

              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

                That applies to all arrays initialization. Actually I dont know if there is a smarter way to do it. :)

                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
                #31

                No CPallini, For a normal array, we can declare/initialize in the simple way, saving type work. Right?

                int array[20] = {100}; // initialize at the same time, assign all elements to 100

                regards, George

                C 1 Reply Last reply
                0
                • G George_George

                  No CPallini, For a normal array, we can declare/initialize in the simple way, saving type work. Right?

                  int array[20] = {100}; // initialize at the same time, assign all elements to 100

                  regards, George

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

                  No George. You are wrong. Your code

                  int array[20] = {100};

                  initializes only the first element of the array (please make a test). You can indeed apply the same syntax to an auto_ptr 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

                    No George. You are wrong. Your code

                    int array[20] = {100};

                    initializes only the first element of the array (please make a test). You can indeed apply the same syntax to an auto_ptr 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
                    #33

                    Cool, CPallini! I have tried, you are correct! regards, George

                    1 Reply Last reply
                    0
                    • G George_George

                      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 Offline
                      L Offline
                      led mike
                      wrote on last edited by
                      #34

                      ankita patel has given you again the answer I gave you because it is the correct answer. Who cares how to do something that shouldn't be done. I recommend you stop wasting your time with all this deep diving into subtle mechanics of something just to find the answer. Start studying Design Patterns and learn about designing maintainable flexible software. When you run into a specific situation within a real context not an imagined one, then you take the time to go deep. Until then it's a giant waste of time trying to figure out something that you will never use. Don't you think?

                      led mike

                      G 1 Reply Last reply
                      0
                      • G George_George

                        Hi _AnShUmAn_, It is not correct code. Because the destructor of auto_ptr will use delete other than delete[]. And it will lead to memory leak. Any ideas or comments? regards, George

                        S Offline
                        S Offline
                        Stephen Hewitt
                        wrote on last edited by
                        #35

                        Use Boost's scoped_array[^].

                        Steve

                        G 1 Reply Last reply
                        0
                        • L led mike

                          ankita patel has given you again the answer I gave you because it is the correct answer. Who cares how to do something that shouldn't be done. I recommend you stop wasting your time with all this deep diving into subtle mechanics of something just to find the answer. Start studying Design Patterns and learn about designing maintainable flexible software. When you run into a specific situation within a real context not an imagined one, then you take the time to go deep. Until then it's a giant waste of time trying to figure out something that you will never use. Don't you think?

                          led mike

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

                          Thanks all the same led mike! regards, George

                          1 Reply Last reply
                          0
                          • S Stephen Hewitt

                            Use Boost's scoped_array[^].

                            Steve

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

                            Good resource, Steve! Unfortunately, in current project, I have to use MSVC, and it does not support shared_ptr. :-) regards, George

                            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