dynamic array
-
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.
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
-
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.
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.
-
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]
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
-
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
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]
-
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.
-
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]
-
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]
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] -
I just tried it! I can't believe it. :omg: Regards, Jijo.
_____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
:)
nave [OpenedFileFinder]
-
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.
Sunday8PM wrote:
Would the code work?
No, because you have not told
new
how manyMyClass
objects to create. Since you do not know this information at the time of creation, perhaps a vector ofMyClass
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
-
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.
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;