|
-
Oct 23rd, 2001, 08:38 AM
#1
Thread Starter
Junior Member
Array of pointers, dynamically
How do i create an array of pointers to a certain class (eg LLNode), but the size of the array needs to be dynamic????
Thanks in advance.
-
Oct 23rd, 2001, 09:00 AM
#2
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 23rd, 2001, 09:06 AM
#3
Thread Starter
Junior Member
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 ...
-
Oct 23rd, 2001, 09:12 AM
#4
I have no idea what you wanna do...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 23rd, 2001, 09:15 AM
#5
Thread Starter
Junior Member
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
-
Oct 23rd, 2001, 09:20 AM
#6
why do you use this complicated syntax?
It would work with STL why not use it?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 23rd, 2001, 09:22 AM
#7
Thread Starter
Junior Member
because i dont know how .. and to me this seams easy
-
Oct 23rd, 2001, 09:39 AM
#8
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:
Code:
#include <vector>
using namespace std;
vector<LLNode*> nodes;
see this link for example:
reference
It's a litle technical, but it's complete with iterators and algorithms.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 23rd, 2001, 09:58 AM
#9
transcendental analytic
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.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 23rd, 2001, 10:01 AM
#10
Thread Starter
Junior Member
thanks ... yeah i want to .. but i want to stay away from templates for now
-
Oct 23rd, 2001, 10:07 AM
#11
transcendental analytic
Why? Is there something you don't think you can understand without much insight?
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 23rd, 2001, 10:14 AM
#12
Thread Starter
Junior Member
not really .. its just something i cant be bothered to learn right now .. and there has got to be another way?
-
Oct 23rd, 2001, 10:50 AM
#13
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?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 23rd, 2001, 11:27 AM
#14
Thread Starter
Junior Member
thanks for that, i shall do
-
Oct 23rd, 2001, 11:39 AM
#15
transcendental analytic
Damn Cornedbee, we failed!
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 24th, 2001, 07:52 AM
#16
Next time I will tell them that there is no other way
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|