473,703 Members | 4,233 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

create a dynamic array of pointers with initial values of NULL

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.

Nov 28 '06 #1
23 7407
Oops. My subject is incorrect, I want to create a dynamic array of my
class Directory, within my class Directory.

Nov 28 '06 #2
sa***@murdocks. on.ca 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<Filefi les_;
std::set<Direct orysubdirs_;
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

Nov 28 '06 #3
sa***@murdocks. on.ca 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<Dir ectory*Director ies;

then you do

... Directories(Max SbDirs, (Directory*)NUL L) ...

(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
Nov 28 '06 #4
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*)t his)->Directory::Dir ectories' cannot be used as a
function

Obviously I don't understand what was meant.

Victor Bazarov wrote:
sa***@murdocks. on.ca 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<Dir ectory*Director ies;

then you do

... Directories(Max SbDirs, (Directory*)NUL L) ...

(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
Nov 29 '06 #5
* sa***@murdocks. on.ca:
[top-posting, overquoting]
Please don't -- read the FAQ -- corrected.
* sa***@murdocks. on.ca:
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*)t his)->Directory::Dir ectories' 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?
Nov 29 '06 #6

sa***@murdocks. on.ca 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(ne w Directory[MaxSubDirs]());

I tried it as is and got the error:
`((Directory*)t his)->Directory::Dir ectories' 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
Nov 29 '06 #7
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<Director y*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.res ize[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.

Nov 29 '06 #8
* sa***@murdocks. on.ca:
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<Director y*Directories; // pointer to an array of Directories
Make that

vector<Director ydirectories;

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

But if you need pointers, use e.g.

typedef boost::shared_p tr<DirectoryDir ectoryPtr;
typedef std::vector<Dir ectoryPtrDirect oryPtrs;

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.res ize[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?
Nov 29 '06 #9

sa***@murdocks. on.ca 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<Directo ry*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.re size[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::ostream &sout){
sout<<" size="<<vec.siz e()<<" cap="<<vec.capa city()<<std::en dl;
return;
} // PrintVec(vector <intconst&,ostr eam&)
// --------------------------
void VecIntSize( std::ostream &cout ){
cout<<"\n--- VecInt() size test ---"<<std::end l;
std::vector<int VecInt(10);
PrintVec( VecInt, cout);
VecInt.push_bac k( 1 );
PrintVec( VecInt, cout);
for(size_t i(0); i < 11; ++i){ VecInt.push_bac k( i );}
PrintVec( VecInt, cout);
VecInt.resize( 50 );
PrintVec( VecInt, cout);
VecInt.push_bac k( 1 );
PrintVec( VecInt, cout);
VecInt.resize( 40 );
PrintVec( VecInt, cout);
cout<<" std::vector<int >( ).swap( VecInt );"<<std::end l;
std::vector<int >( ).swap( VecInt );
PrintVec( VecInt, cout);
cout<<"--- VecInt() size test ---END"<<std::endl ;
return;
}
// --------------------------

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

std::vector<dou blevecD( 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()<<st d::endl;
std::cout<<" vecD.capacity() ="<<vecD.capaci ty()<<std::endl ;

{ // initialise with string array
std::string B[]={"001","2","we ","the world"};
std::vector<std ::stringvBstr( 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
Nov 29 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
7687
by: Scott Lyons | last post by:
Hey all, Can someone help me figure out how to pass a dynamic array into a function? Its been giving me some trouble, and my textbook of course doesnt cover the issue. Its probably something simple, but its just not popping into my mind at the moment. My little snippet of code is below. Basically, the studentID array is dynamic so it will fit any length of a Student's Name. What I'm trying to do is place this chunk of code into a...
5
3407
by: meyousikmann | last post by:
I am having a little trouble with dynamic memory allocation. I am trying to read a text file and put the contents into a dynamic array. I know I can use vectors to make this easier, but it has to be done using dynamic arrays. I don't know the size of the text file ahead of time, though, so I created a class that includes a method to resize the array. Here is that class: class Data { public: Data(int initialsize);
14
8479
by: dam_fool_2003 | last post by:
Friends, cannot we malloc a array? So, I tried the following code: int main(void) { unsigned int y={1,3,6},i,j; for(i=0;i<3;i++) printf("before =%d\n",y); *y = 7; /* 1*/
8
3679
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was static; if the input file contained more than entries, tough. This time I want to do it right - use a dynamic array that increases in size with each word read from the file. A few test programs that make use of **List and realloc( List, blah...
15
4890
by: Charles Sullivan | last post by:
Assume I have a static array of structures the elements of which could be any conceivable mixture of C types, pointers, arrays. And this array is uninitialized at program startup. If later in the program I wish to return this array to its startup state, can this be accomplished by writing binary zeroes to the entire memory block with memset(). E.g., static struct mystruct_st { int x1;
2
7981
by: Wendell Wilkie | last post by:
I am working with a 3rd party unmanaged dll, and I need to pass an array of char* as an argument. I can used fixed to get a single char* as follows: char buf = new char; fixed (char* p = buf) { ... } But I do not know how many of these to create until runtime. I cannot think of a way to use fixed in a loop to fix multiple pointers. Is there a way?
19
6070
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 4- Arrays & Pointers, exercise 4.28 * STATEMENT * write a programme to read the standard input and build a vector of integers from values that are read. allocate an array of the same size as the vector and copy elements from the vector into the array */
5
1843
by: subramanian100in | last post by:
Consider the following classes without ctors. class Sample { private: double d_val; double* d_ptr; }; class Test
17
2319
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the same as its address. The second one simply depends on a term that is not well-defined. Most people consider the type to be an important part of the notion of
0
8672
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9123
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6594
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3125
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2462
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2070
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.