0

In a function, I have access to the pointer and size of another array. Passing these along with the pointer, I want to add that data to a map. How can I do that without copying the underlying data twice?

void addToMap(double* ptr, size_t size, unsigned key) {

 // Here I want to add the array corresponding to the pointer location and size to the map.
 // Since adding to the map needs to copy the data once anyway, I want to prevent any other copies.
 double array[size] = ?;

 // Can I initialize this array from the data found at (ptr) to (ptr + size) without a copy?
 array& = ptr; 

 _map[key] = array;

}

The data being pointed to is deleted after this function is called. The map is just a map from an integer to an array of doubles. That simply means, I want to somehow store the data associated with this key while it is still available.

Or do you have a better idea of doing this? Would using a std::vector<double> slower here?

10
  • double array[size] isn't standard C++ anyways. Size of the array must be compile time constant. Commented Apr 22, 2021 at 13:33
  • Is using array necessary? Isn't simply using pointer like double* array = ptr; not accepted? Commented Apr 22, 2021 at 13:33
  • 1
    What is _map that you would try to assign to _map[key] by passing an array? You can't do what you are asking, but there may be other solutions depending on what _map is. Commented Apr 22, 2021 at 13:33
  • array in _map[key] = array; will automatically converted to a pointer anyway. Commented Apr 22, 2021 at 13:34
  • 1
    std::vector<double> array(ptr, ptr + size);. That also means you map needs to have std::vector<double> as the value type Commented Apr 22, 2021 at 13:34

1 Answer 1

1

You cannot define an array variable like that because the function argument can vary at runtime, and is therefore not compile time constant which is a requirement for the size of an array variable.

You'll need to allocate the array dynamically. std::vector is the simplest way to do that.

An array can also not be directly initialised from a pointer to another array, whether you're initialising an array variable (which isn't an option here) or a dynamic array. Here too, std::vector helps with the constructors that it offers.

How would I use std::vector<double> here then?

Example:

std::unordered_map<unsigned, std::vector<double>> _map;

// addToMap
_map.emplace(key, std::vector<double>{ptr, ptr + size});

I recommend using std::span<double> as the argument instead of the pointer and size.

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

5 Comments

How would I use std::vector<double> here then?
Thanks! Seems to be working. And this only copies the data once?
@EigenGrau Yes.
I recommend using std::span<double> as the argument instead of the pointer and size. Why?
@EigenGrau It's simpler to use. That way you could for example call: addToMap(some_array, key) instead of addToMap(some_array, std::size(some_array), key). Another alternative would be to accept a templated range by value which would allow avoiding a copy entirely if the caller passes an rvalue std::vector<double>.

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.