Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Object's address and std::map

Reply
Thread Tools

Object's address and std::map

 
 
gusfonseca@yahoo.com
Guest
Posts: n/a
 
      03-15-2011
Is it safe to assume an object's address remains the same for the
duration of a program, once it is constructed?

For exemple, may i create a std::map<&my_class, char> and expect to
get the same 'char' upon passing an object's address ( my_char =
my_map[ my_pointer_to_object ] )?

If it is safe, is it also advisable to do so, or is it "evil" in some
way?
 
Reply With Quote
 
 
 
 
Peter Remmers
Guest
Posts: n/a
 
      03-15-2011
Am 16.03.2011 00:18, schrieb http://www.velocityreviews.com/forums/(E-Mail Removed):
> Is it safe to assume an object's address remains the same for the
> duration of a program, once it is constructed?

An object's address remains the same for the duration of its lifetime.
Which may or may not be the same as the duration of the program. It
depends on the code you have written.


> For exemple, may i create a std::map<&my_class, char> and expect to

I guess that should be std::map<my_class*, char>

> get the same 'char' upon passing an object's address ( my_char =
> my_map[ my_pointer_to_object ] )?


Yes. The map maps pointer values (addresses) to chars. The map will
always return the same char for the same pointer value (as long as you
don't modify that map entry). This is independent of the lifetime of the
object that happens to live (or have lived) at that address.

>
> If it is safe, is it also advisable to do so, or is it "evil" in some
> way?


The danger lies in the re-use of the address. If you destroy the object
and create a new one, the new one possibly happens to live at the same
address where some previous instance lived. Also, if you iterate over
the map and try to access the objects pointed to by the keys, these
objects better be still alive. The map should somehow be synchronized
with the lifetime of the objects. I.e. when an object gets deleted, make
sure you erase its address from the map.

Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Identify IP address from a text file and replace it with new address. Chandu80 Ruby 4 11-09-2010 07:16 AM
PIX Firewall MAC address VPN IP address Julian Dragut Cisco 1 02-07-2006 07:57 AM
Pass a Web Address and return a File System Address =?Utf-8?B?QW5nZWw=?= ASP .Net 2 09-20-2005 11:35 PM
obtaining the IP ADDRESS of an IP POHNE by its MAC ADDRESS ProgDario Cisco 17 05-06-2005 02:32 PM
Routing to public IP of NAT address from internal NAT address Andrew Albert Cisco 1 02-08-2005 07:05 PM



Advertisments