Copying void *?

T

Travis

So I had a Tree (custom written) that had, as a node type, as pretty
simple class. The class just stored some std::string's plus a void*.

So my tree instantiation use to be Tree <myClass *> myTree so adding a
new looked something like

myTree.Insert( new myClass( "string 1", "string 2", (void*) 2) ); //
seemed to work fine

Then I decided it was silly to store a tree of pionters, and changed
the tree to Tree <myClass> myTree so adding looked more like...

myTree.Insert( myClass( "string 1", "string 2", (void*) 2) ); //
crashes?

This insertion style compiles but the later one it crashes and I'm not
sure why. It would *appear* that the void* at some point goes NULL or
otherwise erroneous.

Just looking for some educated thoughts/opinions. Thanks.
 
C

Christopher

So I had a Tree (custom written) that had, as a node type, as pretty
simple class. The class just stored some std::string's plus a void*.

So my tree instantiation use to be Tree <myClass *> myTree so adding a
new looked something like

myTree.Insert( new myClass( "string 1", "string 2", (void*) 2) ); //
seemed to work fine

Then I decided it was silly to store a tree of pionters, and changed
the tree to Tree <myClass> myTree so adding looked more like...

myTree.Insert( myClass( "string 1", "string 2", (void*) 2) ); //
crashes?

This insertion style compiles but the later one it crashes and I'm not
sure why. It would *appear* that the void* at some point goes NULL or
otherwise erroneous.

Just looking for some educated thoughts/opinions. Thanks.

Would have to see the declaration and definition of the constructor
for the object type of myClass to explain what is happening. I
_really_ doubt you want to be passing a void pointer to a constant
integer that will soon be out of scope.

What exactly do you want to use the parameters of the constructor for?
What types will they be?
Where are they stored and how are they used?

While I've found that sometimes using a void pointer is the only
option, it is pretty rare. What made you decide to use a void pointer?
Are all nodes going to be containing different types of objects that
were allocated outside the node?
Or would all nodes contain the same type of objects.

Maybe you want to templatize your node class?

Would need more details as your goal is unknown.
 
T

Travis

So the constuctor is basically

myClass::myClass (const myClass &rhs)
{
string1 = rhs.string1;
string2 = rhs.string2;
voidstar = rhs.voidstar;
}

dangerous?

what I want is to have people be able to insert into the tree and
throw whatever type they want into the tree and i'll store it as a
void *.
 
C

Christopher

So the constuctor is basically

myClass::myClass (const myClass &rhs)
{
string1 = rhs.string1;
string2 = rhs.string2;
voidstar = rhs.voidstar;

}

dangerous?

what I want is to have people be able to insert into the tree and
throw whatever type they want into the tree and i'll store it as a
void *.

Very dangerous in my opinion:
Who allocates what void * is pointing to?
Who releases it?
If every node may contain a different type, how is the user of the
node going to know what type it contains, perform the proper cast and
use it?

I am still unclear if you want to make a tree that may contain nodes
that contain any _one_ type
i.e
I can make a tree<int> that contains node<int> objects, which contain
ints
I can create another instance of the tree, tree<myclass>, that
contains node<myclass> objects, which that contain myclass
....
or
....
If you are wanting to create a tree that may contain nodes that may
contain any type
i.e.
I can make a tree, that contains one node, that contains an int
The same instance of the tree may contain another node, which contains
a myclass object

The first can be easily done with templates and I can envision it
easily.
The second is harder, and I _think_ it can also be done with
templates, however, I cannot easily envision its design.

I think there is also a boost container type that supports elements of
varying types, such that one element can contain an int while another
element can contain a myclass, with both elements belonging to the
same container.

I think your use of the void * is a way for you to get around a design
problem. But it if isn't a problem in your design, we still need to
see the constructor that takes 3 arguments, allocation of the memory
void * points to, where it is release, and where it is casted and
used.
 
T

Travis

Very dangerous in my opinion:
Who allocates what void * is pointing to?
Who releases it?
If every node may contain a different type, how is the user of the
node going to know what type it contains, perform the proper cast and
use it?

I am still unclear if you want to make a tree that may contain nodes
that contain any _one_ type
i.e
I can make a tree<int> that contains node<int> objects, which contain
ints
I can create another instance of the tree, tree<myclass>, that
contains node<myclass> objects, which that contain myclass
...
or
...
If you are wanting to create a tree that may contain nodes that may
contain any type
i.e.
I can make a tree, that contains one node, that contains an int
The same instance of the tree may contain another node, which contains
a myclass object

The first can be easily done with templates and I can envision it
easily.
The second is harder, and I _think_ it can also be done with
templates, however, I cannot easily envision its design.

I think there is also a boost container type that supports elements of
varying types, such that one element can contain an int while another
element can contain a myclass, with both elements belonging to the
same container.

I think your use of the void * is a way for you to get around a design
problem. But it if isn't a problem in your design, we still need to
see the constructor that takes 3 arguments, allocation of the memory
void * points to, where it is release, and where it is casted and
used.

Yeah the second one. The tree is always going to have a node_type of
myClass (and the tree is templated). As part of that class, there is
one attribute that I would like to be any type therefore one of the
attributes is a void*
 
D

Daniel T.

Travis said:
So I had a Tree (custom written) that had, as a node type, as pretty
simple class. The class just stored some std::string's plus a void*.

So my tree instantiation use to be Tree <myClass *> myTree so adding a
new looked something like

myTree.Insert( new myClass( "string 1", "string 2", (void*) 2) ); //
seemed to work fine

Then I decided it was silly to store a tree of pionters, and changed
the tree to Tree <myClass> myTree so adding looked more like...

myTree.Insert( myClass( "string 1", "string 2", (void*) 2) ); //
crashes?

This insertion style compiles but the later one it crashes and I'm not
sure why. It would *appear* that the void* at some point goes NULL or
otherwise erroneous.

Just looking for some educated thoughts/opinions. Thanks.

The thread seems to be focusing on the fact that you are casting a
number to a void*, but it seems to me that your question involves why
Insert( new MyObj() ) works but Insert( MyObj() ) doesn't.

I expect it crashes because, in the second case the object that was
inserted ceases to exist as soon as the Insert function returns.
 
T

thomas

Yeah the second one. The tree is always going to have a node_type of
myClass (and the tree is templated). As part of that class, there is
one attribute that I would like to be any type therefore one of the
attributes is a void*- Hide quoted text -

- Show quoted text -

For a container to hold different types, I think you can use a parent
class for your types.
So that creating a tree<parent> will hold any type derived from it.
 
T

terminator

Yeah the second one. The tree is always going to have a node_type of
myClass (and the tree is templated). As part of that class, there is
one attribute that I would like to be any type therefore one of the
attributes is a void*- Hide quoted text -

- Show quoted text -

I guess the problem is that you can introduce just one destructor
where you are using multiple (at least two) constructors in your later
design (in contrary to the former which only uses one constructor).If
destructor and constructors do not conform to each other,your code
will crash.You have to check all constructors , assignment operator(if
introduced),and the destructor.

regards,
FM.
 
V

Victor Bazarov

Daniel said:
The thread seems to be focusing on the fact that you are casting a
number to a void*, but it seems to me that your question involves why
Insert( new MyObj() ) works but Insert( MyObj() ) doesn't.

I expect it crashes because, in the second case the object that was
inserted ceases to exist as soon as the Insert function returns.

So? The tree node allegedly is a value of the store type. That
value has to be copy-constructed. Whether he copy-constructs a mere
pointer or copy-constructs the entire object, shouldn't matter,
should it? If the copy-constructor for 'myClass' looks like shown,
the only other problem can be in the definition of Tree<T> to see
how the instance of 'T' is handled.

V
 
T

terminator

So?  The tree node allegedly is a value of the store type.  That
value has to be copy-constructed.  Whether he copy-constructs a mere
pointer or copy-constructs the entire object, shouldn't matter,
should it?  If the copy-constructor for 'myClass' looks like shown,
the only other problem can be in the definition of Tree<T> to see
how the instance of 'T' is handled.

copying a pointer is well defined and different from coppying an
object with problematic destructor and constructors in which case the
result of copy is source of doubt.

regards,
FM.
 

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

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,504
Latest member
SherryFerr

Latest Threads

Top