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. dynamic array

dynamic array

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresperformancehelpquestion
12 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.
  • S Offline
    S Offline
    Sunday8PM
    wrote on last edited by
    #1

    Let's say I have a class called MyClass. I want to store objects of this class in a dynamic array and by dynamic I mean the ability to add any number of object one by one, not a predefined number. I have googled it and "dynamic array" appears to mean that you can assign a dynamic length when you create the array but that's not what I'm looking for. I want an array with an unspecified length that I can add any number of objects to. Would something like this work? MyClass *MyObjects = new MyClass[]; MyObject[0] = MyObject1 MyObject[1] = MyObject2 MyObject[2] = MyObject3 If I have understood arrays, increasing the number will make it jump to the next place in the memory (8 byte jump forward if the object takes 8 bytes). I'm worried that since I haven't specified the length of the array before, it might write over something else since it isn't expecting the array to use it. I haven't tested the code, I just came to think of it. Would the code work? is there another way of doing it? Is it possible at all? Any help appreciated.

    N P J D E 5 Replies Last reply
    0
    • S Sunday8PM

      Let's say I have a class called MyClass. I want to store objects of this class in a dynamic array and by dynamic I mean the ability to add any number of object one by one, not a predefined number. I have googled it and "dynamic array" appears to mean that you can assign a dynamic length when you create the array but that's not what I'm looking for. I want an array with an unspecified length that I can add any number of objects to. Would something like this work? MyClass *MyObjects = new MyClass[]; MyObject[0] = MyObject1 MyObject[1] = MyObject2 MyObject[2] = MyObject3 If I have understood arrays, increasing the number will make it jump to the next place in the memory (8 byte jump forward if the object takes 8 bytes). I'm worried that since I haven't specified the length of the array before, it might write over something else since it isn't expecting the array to use it. I haven't tested the code, I just came to think of it. Would the code work? is there another way of doing it? Is it possible at all? Any help appreciated.

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      You could try the CArray class in MFC or you can use the vector class in STL.

      Sunday8PM wrote:

      Would the code work?

      No it will crash.

      nave [OpenedFileFinder]

      CPalliniC 1 Reply Last reply
      0
      • S Sunday8PM

        Let's say I have a class called MyClass. I want to store objects of this class in a dynamic array and by dynamic I mean the ability to add any number of object one by one, not a predefined number. I have googled it and "dynamic array" appears to mean that you can assign a dynamic length when you create the array but that's not what I'm looking for. I want an array with an unspecified length that I can add any number of objects to. Would something like this work? MyClass *MyObjects = new MyClass[]; MyObject[0] = MyObject1 MyObject[1] = MyObject2 MyObject[2] = MyObject3 If I have understood arrays, increasing the number will make it jump to the next place in the memory (8 byte jump forward if the object takes 8 bytes). I'm worried that since I haven't specified the length of the array before, it might write over something else since it isn't expecting the array to use it. I haven't tested the code, I just came to think of it. Would the code work? is there another way of doing it? Is it possible at all? Any help appreciated.

        P Offline
        P Offline
        Perspx
        wrote on last edited by
        #3

        The code you have suggested means that the array elements can only be added in the source code - any dynamic changes in the code are not possible. If you are wanting to dynamically add array elements at run time, this link may help you solve your problem. Regards, --Perspx

        "The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia Introduction to Object-Oriented JavaScript

        1 Reply Last reply
        0
        • S Sunday8PM

          Let's say I have a class called MyClass. I want to store objects of this class in a dynamic array and by dynamic I mean the ability to add any number of object one by one, not a predefined number. I have googled it and "dynamic array" appears to mean that you can assign a dynamic length when you create the array but that's not what I'm looking for. I want an array with an unspecified length that I can add any number of objects to. Would something like this work? MyClass *MyObjects = new MyClass[]; MyObject[0] = MyObject1 MyObject[1] = MyObject2 MyObject[2] = MyObject3 If I have understood arrays, increasing the number will make it jump to the next place in the memory (8 byte jump forward if the object takes 8 bytes). I'm worried that since I haven't specified the length of the array before, it might write over something else since it isn't expecting the array to use it. I haven't tested the code, I just came to think of it. Would the code work? is there another way of doing it? Is it possible at all? Any help appreciated.

          J Offline
          J Offline
          Jijo Raj
          wrote on last edited by
          #4

          Dynamic arrays means - you are free to allocate any number of members at runtime. But still you should specify the number of items in you dynamic array. The only flexibility is - you could decide the count at runtime where in static arrays, you cannot.

          Sunday8PM wrote:

          MyClass *MyObjects = new MyClass[];

          You've to specify the array size.

          int ArraySize = 10;
          MyClass *MyObjects = new MyClass[ArraySize];

          Sunday8PM wrote:

          I'm worried that since I haven't specified the length of the array before, it might write over something else since it isn't expecting the array to use it.

          You cannot instantiate arrays without specifying the size. Well, you could use stl containers such as std::vector where you'll be free from the array size headache. Regards, Jijo.

          _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

          S 1 Reply Last reply
          0
          • N Naveen

            You could try the CArray class in MFC or you can use the vector class in STL.

            Sunday8PM wrote:

            Would the code work?

            No it will crash.

            nave [OpenedFileFinder]

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            Naveen wrote:

            Sunday8PM wrote: Would the code work? No it will crash.

            [added] I was wrong: [/added] No. As it stands, it will not compile. [added] See Naveen's reply. [/added] :)

            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
            [My articles]

            modified on Monday, August 25, 2008 6:26 AM

            In testa che avete, signor di Ceprano?

            N 1 Reply Last reply
            0
            • CPalliniC CPallini

              Naveen wrote:

              Sunday8PM wrote: Would the code work? No it will crash.

              [added] I was wrong: [/added] No. As it stands, it will not compile. [added] See Naveen's reply. [/added] :)

              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
              [My articles]

              modified on Monday, August 25, 2008 6:26 AM

              N Offline
              N Offline
              Naveen
              wrote on last edited by
              #6

              CPallini wrote:

              No. As it stands, it will not compile.

              :) If you are telling this because of the empty [], you are wrong. A few days back, I came to know that an empty [] will return a pointer pointing to a memory of size 1 byte. Thats why I said, it will crash. Just try..

              nave [OpenedFileFinder]

              J CPalliniC 2 Replies Last reply
              0
              • J Jijo Raj

                Dynamic arrays means - you are free to allocate any number of members at runtime. But still you should specify the number of items in you dynamic array. The only flexibility is - you could decide the count at runtime where in static arrays, you cannot.

                Sunday8PM wrote:

                MyClass *MyObjects = new MyClass[];

                You've to specify the array size.

                int ArraySize = 10;
                MyClass *MyObjects = new MyClass[ArraySize];

                Sunday8PM wrote:

                I'm worried that since I haven't specified the length of the array before, it might write over something else since it isn't expecting the array to use it.

                You cannot instantiate arrays without specifying the size. Well, you could use stl containers such as std::vector where you'll be free from the array size headache. Regards, Jijo.

                _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

                S Offline
                S Offline
                Sunday8PM
                wrote on last edited by
                #7

                I'm going to look into vector's, they sound interesting :) thanks!

                1 Reply Last reply
                0
                • N Naveen

                  CPallini wrote:

                  No. As it stands, it will not compile.

                  :) If you are telling this because of the empty [], you are wrong. A few days back, I came to know that an empty [] will return a pointer pointing to a memory of size 1 byte. Thats why I said, it will crash. Just try..

                  nave [OpenedFileFinder]

                  J Offline
                  J Offline
                  Jijo Raj
                  wrote on last edited by
                  #8

                  I just tried it! I can't believe it. :omg: Regards, Jijo.

                  _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

                  N 1 Reply Last reply
                  0
                  • N Naveen

                    CPallini wrote:

                    No. As it stands, it will not compile.

                    :) If you are telling this because of the empty [], you are wrong. A few days back, I came to know that an empty [] will return a pointer pointing to a memory of size 1 byte. Thats why I said, it will crash. Just try..

                    nave [OpenedFileFinder]

                    CPalliniC Offline
                    CPalliniC Offline
                    CPallini
                    wrote on last edited by
                    #9

                    Naveen wrote:

                    If you are telling this because of the empty [],

                    Yes.

                    Naveen wrote:

                    you are wrong

                    Yes. I just tried it.

                    Naveen wrote:

                    A few days back, I came to know that an empty [] will return a pointer pointing to a memory of size 1 byte.

                    Good to know.

                    Naveen wrote:

                    Thats why I said, it will crash.

                    And you're, of course, right. Thank you for pointing out. :)

                    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
                    [My articles]

                    In testa che avete, signor di Ceprano?

                    1 Reply Last reply
                    0
                    • J Jijo Raj

                      I just tried it! I can't believe it. :omg: Regards, Jijo.

                      _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

                      N Offline
                      N Offline
                      Naveen
                      wrote on last edited by
                      #10

                      :)

                      nave [OpenedFileFinder]

                      1 Reply Last reply
                      0
                      • S Sunday8PM

                        Let's say I have a class called MyClass. I want to store objects of this class in a dynamic array and by dynamic I mean the ability to add any number of object one by one, not a predefined number. I have googled it and "dynamic array" appears to mean that you can assign a dynamic length when you create the array but that's not what I'm looking for. I want an array with an unspecified length that I can add any number of objects to. Would something like this work? MyClass *MyObjects = new MyClass[]; MyObject[0] = MyObject1 MyObject[1] = MyObject2 MyObject[2] = MyObject3 If I have understood arrays, increasing the number will make it jump to the next place in the memory (8 byte jump forward if the object takes 8 bytes). I'm worried that since I haven't specified the length of the array before, it might write over something else since it isn't expecting the array to use it. I haven't tested the code, I just came to think of it. Would the code work? is there another way of doing it? Is it possible at all? Any help appreciated.

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #11

                        Sunday8PM wrote:

                        Would the code work?

                        No, because you have not told new how many MyClass objects to create. Since you do not know this information at the time of creation, perhaps a vector of MyClass objects would work.

                        "Love people and use things, not love things and use people." - Unknown

                        "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                        1 Reply Last reply
                        0
                        • S Sunday8PM

                          Let's say I have a class called MyClass. I want to store objects of this class in a dynamic array and by dynamic I mean the ability to add any number of object one by one, not a predefined number. I have googled it and "dynamic array" appears to mean that you can assign a dynamic length when you create the array but that's not what I'm looking for. I want an array with an unspecified length that I can add any number of objects to. Would something like this work? MyClass *MyObjects = new MyClass[]; MyObject[0] = MyObject1 MyObject[1] = MyObject2 MyObject[2] = MyObject3 If I have understood arrays, increasing the number will make it jump to the next place in the memory (8 byte jump forward if the object takes 8 bytes). I'm worried that since I haven't specified the length of the array before, it might write over something else since it isn't expecting the array to use it. I haven't tested the code, I just came to think of it. Would the code work? is there another way of doing it? Is it possible at all? Any help appreciated.

                          E Offline
                          E Offline
                          erkanina
                          wrote on last edited by
                          #12

                          Following code should be helpfull; //... First - allocate class pointer array MyClass **pArr = new MyClass* [dwClassCount]; //... Second - allocate class objects for(i=0;i<dwClassCount;i++) pArr[i] = new MyClass; //... Do the job //... Release class objects for(i=0;i<dwClassCount;i++) delete pArr[i]; //... Release class pointer array delete pArr;

                          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