std::auto_ptr<MyStruct> myStruct(new MyStruct);
// init my struct
m_MyTable[key] = myStruct;
// no need to worry about destruction (will be done by auto ptr)
// and address are on the heap (and not on the stack) --> still valid after init loop exit.
auto_ptr can not be used in containers. Any standard complient
Oh Really ? Why not ?
Is it not possible using typedef like this:
Code:
typedef struct
{
int x;
int y;
} mystruct_t;
typedef std::auto_ptr<mystruct_t> mystruct_autoptr_t;
typedef std::hash_map<int,mystruct_autoptr_t> myStructTable;
Does I miss something ?
but can you please tell me why in option 1, it will point to invalid memory?
Because in option 1, your structs are allocated on the heap, and they will be deallocated when your init method will exit, meaning that the address you copy into your hash map will point to an invalid address.
Because in option 1, your structs are allocated on the heap, and they will be deallocated when your init method will exit, meaning that the address you copy into your hash map will point to an invalid address.
by this:
Because in option 1, your structs are allocated on the STACK, and they will be deallocated when your init method will exit, meaning that the address you copy into your hash map will point to an invalid address.
Originally posted by pdurye
Does I miss something ?
Oh yes, you missed something. copying auto_ptrs transfers
ownership of the object pointed to and sets the copied auto_ptr
to null. Using containers of auto_ptrs can lead to all sorts
of bad, bad behavior. I suggest you go look it up. Now since the
op is "required" to use all sorts of bad programming techniques
maybe he should toss some containers of auto_prts in there
just to make it complete.
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
then isn't mystruct is also invalid after finishing the loop?
pls explain to me, becuase that's what used at the begining, when I thought no copy operation needed, so I didn't use pointers, and it worked fine, and I don't understand why (I mean after reading your explanation about option 1, this is on the stack too!)
then isn't mystruct is also invalid after finishing the loop?
pls explain to me, becuase that's what used at the begining, when I thought no copy operation needed, so I didn't use pointers, and it worked fine, and I don't understand why (I mean after reading your explanation about option 1, this is on the stack too!)
calling this:
Code:
m_MyTable[key] = myStruct
the copy constructor of myStruct is called and the newly created object is stored into the map. Consequenly myStruct will be destroyed when init loop will exit, but its copy will remain inside the map and will persist as long as the class to which it belongs to will persist (stack or heap depends on how you create the embedding object - on the stack or on the heap -).
As the map will call destructor of its elements you do not need to take care of freeing them. It will just be more time consuming (but if your struct is small or init called a few time do not care of this).
Oh yes, you missed something. copying auto_ptrs transfers
ownership of the object pointed to and sets the copied auto_ptr
to null. Using containers of auto_ptrs can lead to all sorts
of bad, bad behavior. I suggest you go look it up. Now since the
op is "required" to use all sorts of bad programming techniques
maybe he should toss some containers of auto_prts in there
just to make it complete.
OK see what you mean. We can get behavior problem due to the fact that copy constructor of auto_ptr is not const for the original. You are right, it was a bad idea. Thanks for explanation.
No, it was a good idea. If pointers must be used then a container
of smart pointers is the right solution. It is just that you can not
use auto_ptr, but a reference counted smart pointer would be
fine and could easily be made thread safe.
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
a reference counted smart pointer would be
fine and could easily be made thread safe.
Is there any implementation of counted smart pointers in STL ? or in Boost may be ? Let me know I will be very interested in it. If you have your own implementation, I will be interested too.
Thanks.
Originally posted by pdurye
Is there any implementation of counted smart pointers in STL ? or in Boost may be ? Let me know I will be very interested in it. If you have your own implementation, I will be interested too.
Thanks.
Sure.:)
I think boost has one and here is a quick example of what one
might look like (not a template). This one is thread safe
Bookmarks