2

I'm kinda new to C++ (coming from C#).

I'd like to pass an array of data to a function (as a pointer).

void someFunc(byte *data)
{
    // add this data to a hashmap.

    Hashtable.put(key, data)
}

This data will be added into a hashmap (some key-value based object).

In C#, i could just add the passed reference to a dictionary and be done with it.

Can the same be done in C++ ? or must i create a COPY of the data, and only add that to the data structure for storing it ?

I have seen this pattern in some code examples, but i am not 100% sure why it is needed, or whether it can be avoided at certain times.

lysergic-acid
  • 19,570
  • 21
  • 109
  • 218

2 Answers2

0

Not sure where your key is coming from... but the std::map and the std::unordered_map are probably what you are looking for.

Now the underlying data structure of the std::map is a binary tree, while the std::unordered_map is a hash.

Furthermore, the std::unordered_map is an addition in the C++ 11 standard.

Kirk Backus
  • 4,776
  • 4
  • 32
  • 52
  • The exact data-structure is not relevant for my question. I am interested in knowing whether i can use a pointer to be passed on to some other object, even after this method returns (i have no idea what will happen to that pointer afterwards). – lysergic-acid Jul 16 '13 at 21:33
  • Oh, yeah, it does nothing. There is no garbage collection. The pointer is just stored in the hashmap. You will have to delete the memory in the pointer when you are done. – Kirk Backus Jul 16 '13 at 21:36
0

It all depends how you pass the data and how it is created. If the data is created on the heap(by using new) you can just put the pointer or reference you have to it in your table. On the other hand if the function takes the argument by value you will need to make a copy at some point, because if you store the address of a temp bad things will happen :).

As for what data structure to use, and how they work, I've found one of the best references is cppreference

Heap allocation should be reserved for special cases. stack allocation is faster and easier to manage, you should read up on RAII(very important). As for other reading try finding out stuff on dynamic vs. automatic memory allocation.

Just found this read specifically saying C# to C++ figured it'd be perfect for you, good luck c++ can be one of the more difficult languages to learn so don't assume anything will work the same as it does in C#. MSDN has a nice C# vs. C++ thing yet

aaronman
  • 18,343
  • 7
  • 63
  • 78
  • Thanks. Can you point me to some reading material on the different allocation options in C++ ? (heap vs. pass by value) – lysergic-acid Jul 16 '13 at 21:34
  • @lysergic-acid some of the trick in c++ is that you can have a function signature that takes a reference or a pointer but that doesn't mean it was allocated dynamically. Most people say if you know c++ well you should barely have to use new and the cases where you do can be managed by smart pointers – aaronman Jul 16 '13 at 21:41
  • @lysergic-acid why the downvote is there something wrong with my answer – aaronman Jul 17 '13 at 22:26
  • @lysergic-acid ok my bad I think it was a revenge downvote, now my answer feels useless – aaronman Jul 19 '13 at 04:51