Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > create a dynamic array of pointers with initial values of NULL

Reply
Thread Tools

create a dynamic array of pointers with initial values of NULL

 
 
sandy@murdocks.on.ca
Guest
Posts: n/a
 
      11-28-2006
I need (okay, I want) to make a dynamic array of my class 'Directory',
within my class Directory (Can you already smell disaster?)

Each Directory can have subdirectories so I thought to put these in an
array. The application compiles but aborts without giving me any useful
information.

What I suspect is happening is infinite recursion. Each Directory
object creates an array of Subdirectories each of which has an array of
sub-directorires...

I think it basically is recursively killing itself.

Is there some way to declare this array so that the values are NULL
initially? Thus (I think) doing away with the recursion problem. Then
as I add ACTUAL sub-directories it should work. As I delete
Sub-directories I take the array item and set it to NULL... or so my
theory goes.

currently in my header I declare my array like this:

Directory *Directories; // pointer to an array of Directories

and I had been instantiating it like this in my .cpp file:

Directories = new Directory[MaxSubDirs];

is there some way to automatically set the values to NULL? I can't loop
because it has already died by then. I need something in the order of
(of course this does not work)

Directories = new Directory[MaxSubDirs] = NULL;

I realize that I should be able to do the same thing with a linked
list, but I think that using the array will be faster (if it is
possible) due to the date the assignment is due.

Did I mention I am a student? I am a student.

Thanks.

 
Reply With Quote
 
 
 
 
sandy@murdocks.on.ca
Guest
Posts: n/a
 
      11-28-2006
Oops. My subject is incorrect, I want to create a dynamic array of my
class Directory, within my class Directory.

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      11-28-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I need (okay, I want) to make a dynamic array of my class 'Directory',
> within my class Directory (Can you already smell disaster?)
>
> Each Directory can have subdirectories so I thought to put these in an
> array. The application compiles but aborts without giving me any useful
> information.
>
> What I suspect is happening is infinite recursion. Each Directory
> object creates an array of Subdirectories each of which has an array of
> sub-directorires...
>
> I think it basically is recursively killing itself.
>
> Is there some way to declare this array so that the values are NULL
> initially? Thus (I think) doing away with the recursion problem. Then
> as I add ACTUAL sub-directories it should work. As I delete
> Sub-directories I take the array item and set it to NULL... or so my
> theory goes.
>
> currently in my header I declare my array like this:
>
> Directory *Directories; // pointer to an array of Directories
>
> and I had been instantiating it like this in my .cpp file:
>
> Directories = new Directory[MaxSubDirs];
>
> is there some way to automatically set the values to NULL? I can't loop
> because it has already died by then. I need something in the order of
> (of course this does not work)
>
> Directories = new Directory[MaxSubDirs] = NULL;
>
> I realize that I should be able to do the same thing with a linked
> list, but I think that using the array will be faster (if it is
> possible) due to the date the assignment is due.
>
> Did I mention I am a student? I am a student.


Use std::vector, std::set or std::list:

class File;

class Directory
{
Directory& parent_;
std::string name_;
std::set<File> files_;
std::set<Directory> subdirs_;
public:
Directory( const Directory& parent, const std::string& name )
: parent_( parent )
, name_( name )
, files_()
, subdirs_()
{}

void AddDir( const std::string& name )
{
subdirs_.insert( Directory( *this, name ) );
}

// ...
};

Cheers! --M

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      11-28-2006
(E-Mail Removed) wrote:
> I need (okay, I want) to make a dynamic array of my class 'Directory',
> within my class Directory (Can you already smell disaster?)
>
> Each Directory can have subdirectories so I thought to put these in an
> array. The application compiles but aborts without giving me any
> useful information.
>
> What I suspect is happening is infinite recursion. Each Directory
> object creates an array of Subdirectories each of which has an array
> of sub-directorires...
>
> I think it basically is recursively killing itself.
>
> Is there some way to declare this array so that the values are NULL
> initially? Thus (I think) doing away with the recursion problem. Then
> as I add ACTUAL sub-directories it should work. As I delete
> Sub-directories I take the array item and set it to NULL... or so my
> theory goes.
>
> currently in my header I declare my array like this:
>
> Directory *Directories; // pointer to an array of Directories
>
> and I had been instantiating it like this in my .cpp file:
>
> Directories = new Directory[MaxSubDirs];
>
> is there some way to automatically set the values to NULL? [..]


In the initialiser list:

... Directories(new Directory[MaxSubDirs]()) ...

And consider using std::vector instead, which can also be
initialised. Imagine that 'Directories' is declared as

std::vector<Directory*> Directories;

then you do

... Directories(MaxSbDirs, (Directory*)NULL) ...

(although you don't really need that, vector can grow as
needed).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
sandy@murdocks.on.ca
Guest
Posts: n/a
 
      11-29-2006
Thanks for the reply. I may have to try the vectors... we have only
discussed them in class, not used them.

Can you explain this a bit:

Directories(new Directory[MaxSubDirs]());

I tried it as is and got the error:
`((Directory*)this)->Directory:irectories' cannot be used as a
function

Obviously I don't understand what was meant.

Victor Bazarov wrote:
> (E-Mail Removed) wrote:
> > I need (okay, I want) to make a dynamic array of my class 'Directory',
> > within my class Directory (Can you already smell disaster?)
> >
> > Each Directory can have subdirectories so I thought to put these in an
> > array. The application compiles but aborts without giving me any
> > useful information.
> >
> > What I suspect is happening is infinite recursion. Each Directory
> > object creates an array of Subdirectories each of which has an array
> > of sub-directorires...
> >
> > I think it basically is recursively killing itself.
> >
> > Is there some way to declare this array so that the values are NULL
> > initially? Thus (I think) doing away with the recursion problem. Then
> > as I add ACTUAL sub-directories it should work. As I delete
> > Sub-directories I take the array item and set it to NULL... or so my
> > theory goes.
> >
> > currently in my header I declare my array like this:
> >
> > Directory *Directories; // pointer to an array of Directories
> >
> > and I had been instantiating it like this in my .cpp file:
> >
> > Directories = new Directory[MaxSubDirs];
> >
> > is there some way to automatically set the values to NULL? [..]

>
> In the initialiser list:
>
> ... Directories(new Directory[MaxSubDirs]()) ...
>
> And consider using std::vector instead, which can also be
> initialised. Imagine that 'Directories' is declared as
>
> std::vector<Directory*> Directories;
>
> then you do
>
> ... Directories(MaxSbDirs, (Directory*)NULL) ...
>
> (although you don't really need that, vector can grow as
> needed).
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-29-2006
* (E-Mail Removed):
> [top-posting, overquoting]


Please don't -- read the FAQ -- corrected.


* (E-Mail Removed):
> Thanks for the reply. I may have to try the vectors... we have only
> discussed them in class, not used them.


Use them.


> Can you explain this a bit:
>
> Directories(new Directory[MaxSubDirs]());
>
> I tried it as is and got the error:
> `((Directory*)this)->Directory:irectories' cannot be used as a
> function
>
> Obviously I don't understand what was meant.


You have extranous parentheses, but don't try to correct this: use
std::vector.


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      11-29-2006

(E-Mail Removed) wrote in message ...
>Thanks for the reply. I may have to try the vectors... we have only
>discussed them in class, not used them.
>
>Can you explain this a bit:
>
>Directories(new Directory[MaxSubDirs]());
>
>I tried it as is and got the error:
> `((Directory*)this)->Directory:irectories' cannot be used as a
>function
>
>Obviously I don't understand what was meant.


I think Victor meant for you to use that *in* an initialiser list:

class Directory{
static const size_t MaxSubDirs = 5;
Directory *Directories; // pointer to an array of Directories
public:
// - like this -
Directory() : Directories( new Directory[ MaxSubDirs ]() ) {}

~Directory(){ delete[] Directories; }
};

Is that the way you tried it?

Go with the std::vector.

--
Bob R
POVrookie


 
Reply With Quote
 
sandy@murdocks.on.ca
Guest
Posts: n/a
 
      11-29-2006
Thanks for all your help.

I am still trying to figure this out... and maybe I will have to go
with a linked list. Hard to say.
Anyway:

In my header I have this in the private section:

vector<Directory*> Directories; // pointer to an array of Directories

and in the cpp file I am trying this (and maybe I shouldn't... more to
follow):
Directories.resize[MaxSubDirs];

which will not compile:
invalid types `<unknown type>[unsigned int]' for array subscript

MaxSubDirs is an unsigned int in this case, but I have tried int as
well.

With regular arrays one of the issues is what to do when it gets full.
In order to save time, instead of adding one more slot at a time, we
use doubling. With Vector I have been reading and I am not clear.

When I push_back an item, it adds it to the vector and takes space from
the capacity. When capacity is reached does vector double itself? Add
1? Do I need to add functions to double the size of my vector the way I
do my arrays, not because it's the only way to make more space but
because it is more efficient (time wise) to add many slots at once?

Notice I have two questions there.

Thanks for your time.

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-29-2006
* (E-Mail Removed):
> Thanks for all your help.
>
> I am still trying to figure this out... and maybe I will have to go
> with a linked list. Hard to say.
> Anyway:
>
> In my header I have this in the private section:
>
> vector<Directory*> Directories; // pointer to an array of Directories


Make that

vector<Directory> directories;

unless you're going to share those objects or use polymorphism.

But if you need pointers, use e.g.

typedef boost::shared_ptr<Directory> DirectoryPtr;
typedef std::vector<DirectoryPtr> DirectoryPtrs;

DirectoryPtrs directories;

so that object destruction is taken care of automatically.


> and in the cpp file I am trying this (and maybe I shouldn't... more to
> follow):
> Directories.resize[MaxSubDirs];
>
> which will not compile:
> invalid types `<unknown type>[unsigned int]' for array subscript


Wrong kind of parenthesis.


> MaxSubDirs is an unsigned int in this case, but I have tried int as
> well.
>
> With regular arrays one of the issues is what to do when it gets full.
> In order to save time, instead of adding one more slot at a time, we
> use doubling. With Vector I have been reading and I am not clear.


Guaranteed average O(1) for push_back, which essentially means (that
vector uses internal) doubling or some other factor to increase capacity
when exceeded.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      11-29-2006

(E-Mail Removed) wrote in message ...
>Thanks for all your help.
>
>I am still trying to figure this out... and maybe I will have to go
>with a linked list. Hard to say.
>Anyway:
>In my header I have this in the private section:
>
>vector<Directory*> Directories; // pointer to an array of Directories
>
>and in the cpp file I am trying this (and maybe I shouldn't... more to
>follow):
>Directories.resize[MaxSubDirs];


What Alf said. Try this example, maybe it will clear some cobwebs.
( if it don't kill ya first.<G>).
[ BTW, please do not top-post. (ref: other posts of yours)]

// --------------------------
#include <iostream>
#include <ostream>
#include <vector>
// --------------------------
void PrintVec( std::vector<int> const &vec, std:stream &sout){
sout<<" size="<<vec.size()<<" cap="<<vec.capacity()<<std::endl;
return;
} // PrintVec(vector<int> const&,ostream&)
// --------------------------
void VecIntSize( std:stream &cout ){
cout<<"\n--- VecInt() size test ---"<<std::endl;
std::vector<int> VecInt(10);
PrintVec( VecInt, cout);
VecInt.push_back( 1 );
PrintVec( VecInt, cout);
for(size_t i(0); i < 11; ++i){ VecInt.push_back( i );}
PrintVec( VecInt, cout);
VecInt.resize( 50 );
PrintVec( VecInt, cout);
VecInt.push_back( 1 );
PrintVec( VecInt, cout);
VecInt.resize( 40 );
PrintVec( VecInt, cout);
cout<<" std::vector<int>( ).swap( VecInt );"<<std::endl;
std::vector<int>( ).swap( VecInt );
PrintVec( VecInt, cout);
cout<<"--- VecInt() size test ---END"<<std::endl;
return;
}
// --------------------------

int main(){
VecIntSize( std::cout );

std::vector<double> vecD( 10, 3.14 ); // init 10 elements to 3.14.
for(size_t i(0); i < vecD.size(); ++i){
std::cout<<" vecD.at("<<i<<")= "
<<vecD.at( i )<<std::endl;
} // for(i)
vecD.clear();
std::cout<<" vecD.size()="<<vecD.size()<<std::endl;
std::cout<<" vecD.capacity()="<<vecD.capacity()<<std::endl;

{ // initialise with string array
std::string B[]={"001","2","we","the world"};
std::vector<std::string> vBstr( B, B+(sizeof B/sizeof *B) );
for(size_t i(0); i < vBstr.size(); ++i){
std::cout << vBstr.at( i ) <<std::endl;
}
} // initialise with string array - end

return 0;
} // main()

/* --- VecInt() size test --- (some output snipped)
size=10 cap=10
size=11 cap=20 // note cap doubled
size=22 cap=40 // note cap doubled
size=50 cap=50 // re-sized
size=51 cap=100 // note cap doubled.
size=40 cap=100 // note cap didn't shrink
size=0 cap=0 // a reset
--- VecInt() size test ---END
// run to see the other output.
*/
--
Bob R
POVrookie


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
access object in array of pointers to dynamic array of class complex jccorreu@gmail.com C++ 2 05-20-2006 05:08 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
Can a static array contain a dynamic array of pointers? Peter B. Steiger C Programming 8 04-26-2004 03:07 AM
Unexpected initial null in return from split() Craig M. Votava Perl Misc 3 01-30-2004 08:14 PM



Advertisments