Jump to content
  • Advertisement
Sign in to follow this  
sooner123

Dynamic memory allocation in a constructor

This topic is 2847 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to create an n-ary tree in C++. Here is my struct:


struct tree
{
int data;
tree * next;

tree(int numLeaves)
{
cout << "tree object created\n";
next = new tree[numLeaves];
}

tree()
{
}
};




When a tree object is created with an integer argument, am I correct in assuming this will create an array of pointers to 'tree' objects of a size equal to the argument.

The following code is giving compile-time errors:


int main()
{
tree test(2);
test.data = 1;
test.next[0] = new tree(3);

return 0;
}




It says there's an invalid conversion from tree* to int.

I'm confused since I used similar code to make a statically linked tree by omitting both constructors and initializing the pointer array as follows:

tree *next[2];

So what am I doing wrong in initializing this array of pointers dynamically? Or is my error in how I'm trying to access it?

edit: and my guess is that i'm looking at the constructor as creating an array of pointers to trees when in fact it's actually creating the trees themselves.

Share this post


Link to post
Share on other sites
Advertisement
If next is a tree * then next[0] is a tree, not a tree *. In this case, I'd recommend using a std::vector rather than a dynamic array, in which case if you want to hold pointers you would use std::vector<tree *>. However, I'd also give some serious thought about using smart pointers rather than raw pointers.

Share this post


Link to post
Share on other sites
Try thinking about this:
int* pint = new int [1];
pint[0] = new int; // doesn't compile
pint[0] = 123; // compiles

int** pintarr = new int* [1];
pintarr[0] = new int; // compiles fine
pintarr[0][0] = 123;

Share this post


Link to post
Share on other sites
You're creating an array of tree's, but it looks like you want an array of pointers to tree's, e.g.:

struct tree
{
int data;
tree ** next;

tree(int numLeaves)
{
cout << "tree object created\n";
next = new tree*[numLeaves];
}

tree()
{
}
};

(Of course the usually admonitions regarding dynamic memory management, RAII, use of standard containers, and so on apply here.)

Share this post


Link to post
Share on other sites

#include <iostream>

using namespace std;

struct tree
{
int data;
tree ** next;

tree(int numLeaves)
{
cout << "tree object created with " << numLeaves << " leaves\n";
next = new tree*[numLeaves];
}

tree()
{
}
};

int main()
{
tree test(2);
test.data = 1;
test.next[0] = new tree(3);
test.next[1] = new tree(4);
test.next[0]->data = 2;
test.next[1]->data = 3;
test.next[0]->next[0]->data = 4;
test.next[0]->next[1]->data = 5;
test.next[0]->next[2]->data = 6;
test.next[1]->next[0]->data = 7;
test.next[1]->next[1]->data = 8;
test.next[1]->next[2]->data = 9;
test.next[1]->next[3]->data = 10;

cout << test.data;

return 0;
}




I modified it so that it was an array of pointers instead of trees, but it still seems to have some subtle problem.

The above code, doesn't print out '1' as expected.

Nor does printing out any of the other data bits. (test.next[0]->next[1]->data, etc.)

Share this post


Link to post
Share on other sites
Quote:
The above code, doesn't print out '1' as expected.
What does it print out? (Or more generally, what happens when you run the program?)

It looks to me like you're dereferencing a number of tree pointers that haven't had a value assigned to them. At the very least, you should probably fill your next array with 0s in the constructor, as that will likely cause a run-time error to occur if no other assignment is made.

Also, you may already know this, but all of the memory you're allocating is being leaked. (This doesn't really have any practical significance in the given context, but it's important to be aware of.)

Share this post


Link to post
Share on other sites
Actually it doesn't print anything.

Apparently the line:
test.next[0]->next[0]->data = 4;

Crashes the program at run time and I don't understand why.

Share this post


Link to post
Share on other sites
Because you never initialize test.next[0]->next[0] with anything. It's a random garbage address that you're trying to access without allocating anything for it.

Share this post


Link to post
Share on other sites
Quote:
Original post by sooner123
Apparently the line:
test.next[0]->next[0]->data = 4;

Crashes the program at run time and I don't understand why.
I explained why in my previous post.

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Quote:
Original post by sooner123
Apparently the line:
test.next[0]->next[0]->data = 4;

Crashes the program at run time and I don't understand why.
I explained why in my previous post.


I didn't quite follow you at the time but now I understand. Thanks for your help.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!