1

Regarding an unordered_map with the following declaration:

typedef struct
{
    unsigned int length;
    unsigned type;
    std::string data;
} packet;

boost::unordered::unordered_map<int, packet> map;

When I add an object to this map, the unordered_map will clone this object and append it to its container. So my question is it better to have a pointer to the struct instead of adding it as an object?

And if I have a raw pointer as the value in the unordered_map, is there any method I should follow to keep the object alive in the memory and its place not used by other variable even if I get out of the function scope?

5
  • What's the problem you're trying to solve? You forgot to say :) Commented Apr 7, 2014 at 18:53
  • I am looking for efficiency and speed in the excution Commented Apr 7, 2014 at 18:54
  • So, you are looking to avoid the copy? Commented Apr 7, 2014 at 18:55
  • Yes, because I think constructing the same object again is not efficient. So it's better to have pointer, correct or not? Commented Apr 7, 2014 at 18:57
  • Possibly. But life is not quite so simple. You may think you are speeding up your program but in fact suffering from the fix more than from the original problem. See my answer. Commented Apr 7, 2014 at 19:03

3 Answers 3

2

You should use std::unordered_map and its emplace member function, if you wish to avoid the copy.

Don't store pointers, in general: you may slightly speed up some of your code, but if that was even a bottleneck, the trade-off is more complicated code and the increased risk of bugs. Plus lots of dereferences you don't need.

But feel free to profile all three options if you reckon you need to.

Sign up to request clarification or add additional context in comments.

6 Comments

Let's say I want to store a pointer to an object created newly. After leaving the scope of the object will this object still be in its place in the memory if I didn't explicitly delete it?
@HA-AS: Yes; that's a fairly basic property of dynamically allocated objects. Which book are you using?
Currently I am confused between multiple type of pointers, the new pointers from C++11, will automatically delete the pointed object. And when using new , u need to call delete on the object, otherwise it will stay in the memory. So this confused me, will the object be untouched by other if I didn't delete it?
@HA-AS: The entire point of C++11's "smart pointers" is that they handle the delete for you, when the time is right. I don't know what "untouched by other" means.
I mean by others, during the run-time of a process, there are new declaration for new variables. So when these new variables are declared, will they take the place of this object which was not deleted explicitly and kept alive in the memory with a pointer to it outside the scope of its declaration?
|
1

If you choose to store pointers, you have to allocate your objects on the heap to keep the objects alive when you leave scope. Then you should probably make your container to hold smart pointers (std::unique_ptr ?) instead of raw pointer.

As already mentioned use emplace to avoid copy if your container contains objets.

1 Comment

Let's say I use emplace for adding the object in the container, how can I add get the address of the object in the container?
1

The problem with storing pointers in any container is that you need to manually manage the life time of the objects stored in the container. That is, if you do the following, the destructor of the container will not automatically free up the dynamically allocated memory.

boost::unordered::unordered_map<int, packet *> map;
map.insert(std::make_pair(1, new packet()));

To resolve this problem you could do the following.

boost::unordered::unordered_map<int, std::shared_ptr<packet>> map;
map.insert(std::make_pair(1, std::make_shared<packet>()));

Note: The code will need additional work to compile.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.