PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Copying void *?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Copying void *?

Réponse
 
LinkBack Outils de la discussion
Vieux 21/02/2008, 22h08   #1
Travis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Copying void *?

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.
  Réponse avec citation
Vieux 21/02/2008, 22h15   #2
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Copying void *?

Travis wrote:
> 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.


What exactly are you doing in the copy constructor of 'myClass'?
IOW, post more code.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  Réponse avec citation
Vieux 21/02/2008, 22h19   #3
Christopher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Copying void *?

On Feb 21, 4:08 pm, Travis <travis.bow...@gmail.com> wrote:
> 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.
  Réponse avec citation
Vieux 21/02/2008, 22h43   #4
Travis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Copying void *?

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 *.
  Réponse avec citation
Vieux 21/02/2008, 23h31   #5
Christopher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Copying void *?

On Feb 21, 4:43 pm, Travis <travis.bow...@gmail.com> wrote:
> 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.



  Réponse avec citation
Vieux 21/02/2008, 23h50   #6
Travis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Copying void *?

On Feb 21, 3:31 pm, Christopher <cp...@austin.rr.com> wrote:
> On Feb 21, 4:43 pm, Travis <travis.bow...@gmail.com> wrote:
>
> > 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.


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*
  Réponse avec citation
Vieux 22/02/2008, 00h58   #7
Daniel T.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Copying void *?

Travis <travis.bowers@gmail.com> wrote:

> 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.
  Réponse avec citation
Vieux 22/02/2008, 01h59   #8
thomas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Copying void *?


> 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.
  Réponse avec citation
Vieux 22/02/2008, 08h09   #9
terminator
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Copying void *?

On Feb 22, 2:50am, Travis <travis.bow...@gmail.com> wrote:
> On Feb 21, 3:31 pm, Christopher <cp...@austin.rr.com> wrote:
>
>
>
>
>
> > On Feb 21, 4:43 pm, Travis <travis.bow...@gmail.com> wrote:

>
> > > 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.

>
> 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.
  Réponse avec citation
Vieux 22/02/2008, 14h56   #10
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Copying void *?

Daniel T. wrote:
> Travis <travis.bowers@gmail.com> wrote:
>
>> 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.


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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  Réponse avec citation
Vieux 24/02/2008, 09h02   #11
terminator
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Copying void *?

On Feb 22, 5:56pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> Daniel T. wrote:
> > Travis <travis.bow...@gmail.com> wrote:

>
> >> 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.

>
> 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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h18.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,21402 seconds with 19 queries