how safe is this mapping

T

Tom

I create a object in function foo and then I pass the reference of the
object to function bar like this

void foo()
{
class_foo myFoo();
bar(myFoo);
}

void bar(class_foo& object)
{
}

and bar places the object in to a global map by passing the address of
the object parameter.

Now if I then leave function foo, is the object in the global map still
safe?

TIA,
Tom.
 
M

Markus Moll

Hi
void foo()
{
class_foo myFoo();
bar(myFoo);
}

void bar(class_foo& object)
{
}

and bar places the object in to a global map by passing the address of
the object parameter.

Now if I then leave function foo, is the object in the global map still
safe?

You have a compiler error before that.
"class_foo myFoo();" declares a function myFoo.
If you correct that to "class_foo myFoo;", then you have a dangling pointer
as soon as foo's scope is left. Anything that you might then do with that
pointer results in undefined behavior.

You might want to look at boost::shared_ptr.

Markus
 
D

David Harmon

On 12 Sep 2006 01:14:45 -0700 in comp.lang.c++, "Tom"
I create a object in function foo and then I pass the reference of the
object to function bar like this

void foo()
{
class_foo myFoo();
bar(myFoo);
}

void bar(class_foo& object)
{
}

and bar places the object in to a global map by passing the address of
the object parameter.

Now if I then leave function foo, is the object in the global map still
safe?

No!

The object will cease to exist (and its destructor run, if any) when
function foo exits. To further confuse you, if you call foo again
there's a good chance in many implementations that the new object it
requires will be constructed at exactly the same address.
 
C

Carlos Martinez

Tom said:
I create a object in function foo and then I pass the reference of the
object to function bar like this

void foo()
{
class_foo myFoo();
bar(myFoo);
}

void bar(class_foo& object)
{
}

and bar places the object in to a global map by passing the address of
the object parameter.

Now if I then leave function foo, is the object in the global map still
safe?

myFoo's lifetime ends with end of foo function. As a consequence object
pointed from map doesn't exists.

In general you must not keep references or pointers to local variables.
Instead you can keep a copy of that object or create it in heap.
 
T

Tom

David said:
On 12 Sep 2006 01:14:45 -0700 in comp.lang.c++, "Tom"


No!

The object will cease to exist (and its destructor run, if any) when
function foo exits. To further confuse you, if you call foo again
there's a good chance in many implementations that the new object it
requires will be constructed at exactly the same address.

So it would be better to do this?

void foo()
{
class_foo* myFoo = new class_foo();
bar(myFoo);
}

void bar(class_foo* object)
{
}

So it would be a object from the new operator that will be placed in
the map and therefore safe until I call the objects dtor?
 
D

David Harmon

On 12 Sep 2006 02:28:21 -0700 in comp.lang.c++, "Tom"
So it would be better to do this?

void foo()
{
class_foo* myFoo = new class_foo();
bar(myFoo);
}

void bar(class_foo* object)
{
}

So it would be a object from the new operator that will be placed in
the map and therefore safe until I call the objects dtor?

At that point you are not dead yet.

But note, the map destructor will not delete any of the objects
pointed to by the class_foo* pointers it contains. Perhaps you want
to use shared_ptr of some sort instead.
 
T

Tom

David said:
On 12 Sep 2006 02:28:21 -0700 in comp.lang.c++, "Tom"


At that point you are not dead yet.

But note, the map destructor will not delete any of the objects
pointed to by the class_foo* pointers it contains. Perhaps you want
to use shared_ptr of some sort instead.

Yep. This was a very simplistic example of the solution I am
implementing. The actual map is stored in a global class and that
classes dtor iterates through the map calling the mapped classes dtors
so I should be safe there.

Thanks,
Tom.
 
F

F.J.K.

Tom said:
void foo()
{
class_foo myFoo();
bar(myFoo);
}

void bar(class_foo& object)
{
}

and bar places the object in to a global map by passing the address of
the object parameter.

Now if I then leave function foo, is the object in the global map still
safe?

1. class_foo myFoo(); declares a function. You want to define an object
with class_foo myFoo;

2. It really depends, what exactly your std::map contains. If you
insert by calling

my_map[key] = object;

everything is safe, as map.insert copies it's arguments and it's
destructor will take care of the release of the copy automatically. If
you insert by

my_map[key] = &object;

it copies the pointer, not the actual object. You can possibly safe
some cycles, but you'll need to construct the myFoo object on the heap
with operator new and you'll have to take care of the destruction
yourself. In the face of exceptions, the only way to safely do so is
the use of some smart pointer, which seems to be pretty borderline
given your current grasp of the language.

My advice:
Use the first variant. If the construction of the map turns out to be
the bottleneck of your program, you can still optimize it later. But
there's a 99.99% chance, it's not gonna make the slightest difference
on usability, whether you take the easy or the "hard" way. You'd be a
fool to pick the hard way ;-)
 

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
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top