Allocating an array of class pointers. - SOLVED

Recommended Posts

Okay, let's say I have a pointer declared like this, branchobject being a class:
BranchObject** BranchChildren;
Now, I want to allocate the array of pointers to contain n amount of pointers, so I used this:
BranchChildren = new BranchObject*[childrencount];
However, my app crashed whenever it hits this line... anyone know the reason behind it? Any help is greatly appreciated. Thanks in advance ;) [Edited by - Erondial on June 13, 2006 11:47:58 AM]

Share on other sites
It shouldn't. That code is correct. We need to know more about the nature of the crash: "my program crashed" is not very descriptive because there are multiple ways a program can terminate. We also need to know more about your class.

Share on other sites
Well, the syntax is fine. Seems like it's something else in your code that's causing the crash.

Try using vector<BranchObject *>. It's a bit safer than an array of pointers.

Share on other sites
Well, to give a more detailed description of the crash... there's no error report, or anything, the program doesn't complete it's loop, it just ends, at that line. If I debug, it says a segmentation fault has caused the error, probably at that line (though it doesn't tell me for sure for some reason). I checked to see if the pointer was NULL, and indeed it was so there's no problem there...

Oh... I'm calling the function from the end of BranchObject's constructor... could that have somehting to do with it?

As for Vectors... I'll read up on those, thanks ;)

Oh, and here's the class and functions:

class BranchObject {	public:	double vecx;	double vecy;	double vecz;	double posx;	double posy;	double posz;	double height;	double radius;	double* vertexarray;	unsigned short int childrencount;	unsigned short int level;	BranchObject** BranchChildren;	BranchObject* BranchParent;	TreeObject* TreeParent;		BranchObject(TreeObject* NewTreeParent, BranchObject* NewBranchParent, unsigned short int newlevel);	~BranchObject();		void GenerateBranch();	void LoadPolygon();	void GenerateChildren();	void RemoveChildren();	void DrawBranch();};

BranchObject::BranchObject(TreeObject* NewTreeParent, BranchObject* NewBranchParent, unsigned short int newlevel){	height = 0;	radius = 0;	childrencount = 0;	posx = 0;	posy = 0;	posz = 0;	vecx = 0;	vecy = 0;	vecz = 0;	vertexarray = NULL;	level = newlevel;	BranchChildren = NULL;	BranchParent = NewBranchParent;	TreeParent = NewTreeParent;	GenerateBranch();	if (level <= TreeParent->complexity)		GenerateChildren();}

void BranchObject::GenerateChildren(){	childrencount = level + 1 + rand()%2;	BranchChildren = new BranchObject*[childrencount];	for (int count = 0; count < childrencount; count++){		BranchChildren[count] = new BranchObject(TreeParent,this,level+1);	}}

Share on other sites
How large is TreeObject->complexity? Have you considered the possibility that you're running out of memory?

What does GenerateBranch() do? Could it possibly be corrupting a pointer somewhere?

style issues:

- Don't mindlessly add "Object" to your class names. It adds precisely no extra descriptive power to the name.

- Use initializer lists.

- Components of a tree are normally called Nodes, rather than Branches. That's what other people reading your code will expect to see. Sorry :)

Share on other sites
Actually, in this chase I'm talking about a LITERAL tree, being displayed using OpenGL. (though it'll take quite some time to get it right)

I checked the size I'm allocating, it's 3. I tried replacing the variables with a static number as well, with no effect.

As for GenerateBranch(), I checked in GenerateChildren to see if the pointer was still a NULL, and indeed it was.

However, I took your advice and commeneted out GenerateBranch(), and it seems to have worked. Can't think of why though. I'll look into it. Thanks anyway :)

EDIT:

Ah. Seems I was writing off the end of an array. Never mind :) Thanks for all your help guys :)

Create an account

Register a new account

• Partner Spotlight

• Forum Statistics

• Total Topics
627654
• Total Posts
2978447

• 10
• 12
• 22
• 13
• 33