C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

how safe is this mapping

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Tom
Guest





PostPosted: Tue Sep 12, 2006 9:10 am    Post subject: how safe is this mapping Reply with 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?

TIA,
Tom.
Back to top
Markus Moll
Guest





PostPosted: Tue Sep 12, 2006 9:10 am    Post subject: Re: how safe is this mapping Reply with quote



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





PostPosted: Tue Sep 12, 2006 9:10 am    Post subject: Re: how safe is this mapping Reply with quote



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





PostPosted: Tue Sep 12, 2006 9:10 am    Post subject: Re: how safe is this mapping Reply with quote

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.

Quote:

TIA,
Tom.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.