How do i create an array of pointers to a certain class (eg LLNode), but the size of the array needs to be dynamic????
:confused:
Thanks in advance.
Printable View
How do i create an array of pointers to a certain class (eg LLNode), but the size of the array needs to be dynamic????
:confused:
Thanks in advance.
you could use vector<LLNode*> from STL, but there are some things to remember when using pointers in the STL containers.
I just forgot where I found it. basically, watch out if you remove any pointers from the array, if they pointed to heap memory you have a leak, since STL doesn't give you a chance to deallocate them.
well basically i dont want to use templates
and the purpose of the array is to delete heap memory of a Linked Link
but a forward linked link only (no backwards pointers) .. so i was going to read all the pointers into an array and delete them one my one ...
I have no idea what you wanna do...
psuedo code
while (still more members)
copy pointer into array
move on
end while
while (not at end of array)
delete pointer
end while
but the array needs to be dynamic in size at run time
why do you use this complicated syntax?
It would work with STL why not use it?
because i dont know how .. and to me this seams easy
If you don't use STL you'd have to write your own dynamic array...
Using vector is very simple. You include <vector> and use std namespace. Then you can create vector objects:
see this link for example:Code:#include <vector>
using namespace std;
vector<LLNode*> nodes;
reference
It's a litle technical, but it's complete with iterators and algorithms.
a vector has it's own drawbacks (an empty takes 16 bytes), but it's generally usefull for many purposes. Creating a simpler container is good if you want to be strict about resources, for instance if you're developing a large scale project. But most of the case you will do fine with vectors, and developing your own containers just takes unnesseraily much time and you still might end up with not too usefull containers with possible hidden bugs that hardly don't exist in STL.
If you still wan't to create your own containers, I could of course help.
thanks ... yeah i want to .. but i want to stay away from templates for now
Why? Is there something you don't think you can understand without much insight?
not really .. its just something i cant be bothered to learn right now .. and there has got to be another way?
xeah there is: do what the compiler does for you with templates: hard code it for every data type you need.
If you want to write your own dynamic container, it must fulfill this criterias:
At adding new elements, it must check it's own size and reallocate it's memory if necessary.
At removing elements, it should free unused memory.
It should provide the same interface as an array, meaning you should (but don't have to if only you use it) should override the [] operator and such.
It must not leave any leak anywhere. Use the destructor to make sure of that.
Oh, yeah, it should not be too slow...
Anything else?
thanks for that, i shall do
Damn Cornedbee, we failed! :D
:( Next time I will tell them that there is no other way :p