Click to See Complete Forum and Search --> : Array of pointers, dynamically
TvJames
Oct 23rd, 2001, 08:38 AM
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.
CornedBee
Oct 23rd, 2001, 09:00 AM
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.
TvJames
Oct 23rd, 2001, 09:06 AM
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 ...
CornedBee
Oct 23rd, 2001, 09:12 AM
I have no idea what you wanna do...
TvJames
Oct 23rd, 2001, 09:15 AM
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
CornedBee
Oct 23rd, 2001, 09:20 AM
why do you use this complicated syntax?
It would work with STL why not use it?
TvJames
Oct 23rd, 2001, 09:22 AM
because i dont know how .. and to me this seams easy
CornedBee
Oct 23rd, 2001, 09:39 AM
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:
#include <vector>
using namespace std;
vector<LLNode*> nodes;
see this link for example:
reference (http://www.sgi.com/tech/stl/)
It's a litle technical, but it's complete with iterators and algorithms.
kedaman
Oct 23rd, 2001, 09:58 AM
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.
TvJames
Oct 23rd, 2001, 10:01 AM
thanks ... yeah i want to .. but i want to stay away from templates for now
kedaman
Oct 23rd, 2001, 10:07 AM
Why? Is there something you don't think you can understand without much insight?
TvJames
Oct 23rd, 2001, 10:14 AM
not really .. its just something i cant be bothered to learn right now .. and there has got to be another way?
CornedBee
Oct 23rd, 2001, 10:50 AM
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?
TvJames
Oct 23rd, 2001, 11:27 AM
thanks for that, i shall do
kedaman
Oct 23rd, 2001, 11:39 AM
Damn Cornedbee, we failed! :D
CornedBee
Oct 24th, 2001, 07:52 AM
:( Next time I will tell them that there is no other way :p
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.