Pointer problem

T

Thomas Baier

Hi there,

I've got a class with an array of pointers to objects of the same class. I
thought of doing it like that:
class test {
test **array_var;
...
};
void method::test(test object)
{
children = (Baum**) realloc(children, sizeof(Baum**));
children[elements] = (Baum*) malloc(sizeof(Baum*));
children[elements] = &obj;
}

test a;
test b;
a.method(b);

I tried to access b like:
a.children[0].var_of_b;
but it did not work. So how can I manage it?


Thanks for help

Thomas
 
J

Jesper Madsen

Well if you really want to...
But look into the STL containers instead...

#include <iostream>
class X{
public:
X():number(10){};
int number;
};
typedef X* XPtr;

int main(int argc, char* argv[])
{

XPtr* Arr = new XPtr[10];

Arr[0] = new X;

std::cout << "this should say 10 : " << Arr[0]->number << std::endl;
return 0;
}
 
K

Kevin Goodsell

Thomas said:
Hi there,

I've got a class with an array of pointers to objects of the same class.

This does not seem to be what you have at all, so either your
description is off or your code is. I'm not entirely sure which.
I
thought of doing it like that:
class test {
test **array_var;

This is, of course, not an array at all, but a pointer. Now, if I assume
the description you gave is accurate, then I don't think you want
'array_var' to be of type 'test **', but rather of type 'test *'.
...
};
void method::test(test object)

You haven't given any information about this 'method' class (or namespace?).
{
children = (Baum**) realloc(children, sizeof(Baum**));

A few problems here:

1) This is an unsafe way of using realloc. If the reallocation fails,
you lose the original memory. The only safe way to use realloc is to
store the resulting value in a new temporary pointer, then assign it to
the original pointer if and only if the allocation succeeds.

2) Please don't use realloc in C++. It's dangerous, error-prone, won't
work correctly for non-POD types, and there's a better alternative:
std::vector.

Also, having no idea what 'children' or 'Baum' are, I can't properly
diagnose problems with this code.
children[elements] = (Baum*) malloc(sizeof(Baum*));
children[elements] = &obj;

One of the problems here should be obvious. On one line you store a
pointer to some new memory. On the next you overwrite that pointer. You
have leaked the memory you allocated.

But again, I can't properly diagnose problems due to lack of information.
}

test a;
test b;
a.method(b);

More stuff I don't know about: the 'method' member of test.
I tried to access b like:
a.children[0].var_of_b;

And again: the 'children' member of test.
but it did not work. So how can I manage it?

I really don't know what you are doing, so I can't say. Honestly, I
suspect the code you posted is far from the code you are actually
attempting to use. It kind of looks like you've changed some variable
names and transposed some identifiers.

Post real code, a minimum complete program, and we can probably help.
With what you've given us... there's basically nothing we can do.

-Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top