 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Tom Guest
|
Posted: Tue Sep 12, 2006 9:10 am Post subject: how safe is this mapping |
|
|
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. |
|
| Back to top |
|
 |
Markus Moll Guest
|
Posted: Tue Sep 12, 2006 9:10 am Post subject: Re: how safe is this mapping |
|
|
Hi
Tom wrote:
| Quote: | 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 |
|
| Back to top |
|
 |
David Harmon Guest
|
Posted: Tue Sep 12, 2006 9:10 am Post subject: Re: how safe is this mapping |
|
|
On 12 Sep 2006 01:14:45 -0700 in comp.lang.c++, "Tom"
<anbother_freemail (AT) gawab (DOT) com> wrote,
| Quote: | 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. |
|
| Back to top |
|
 |
Carlos Martinez Guest
|
Posted: Tue Sep 12, 2006 9:10 am Post subject: Re: how safe is this mapping |
|
|
Tom wrote:
| Quote: | 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.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|