0

So basically I've recently started coding in C++ instead of C so maybe that's not the C++ way of doing this, but I've got a program where the user passes an array as a function parameter (void foo(void* pass_array_here)) and I want to copy it in a private member of a class, also declared as void* array_private. I'm copying it like this:

void foo(void* pass_array_here) {
    array_private = pass_array_here;
}

I must note that I'm working with OpenGL so I don't know if I can rely on the new keyword or the heap in general. So how do I do that?

(Note, on a sizeof call, a 9-element float array appeared as 8 bytes in the stack, so it definitely didn't work).

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
AggelosT
  • 108
  • 1
  • 9
  • 5
    In general there's absolutely nothing you can do with a `void*`. Either you can pass it along to something else like `memcpy` or you'll need to cast it to some other kind of pointer. – Mark Ransom May 01 '22 at 03:58
  • Unless the function KNOWS what is ACTUALLY being passed to it, it can't safely do anything that touches what the void pointer points at. If it can ASSUME an array of (say) ten integers is passed, then it can access all or any of those ten integers to (say) copy them somewhere else. But *nothing* prevents the caller passing something different, such as the address of a single variable of type `int`. The function has no way to verify that it has been passed something consistent with what it assumes and, if it is passed something different, behaviour is undefined. – Peter May 01 '22 at 04:54
  • This seems to be an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You probably just want to pass the array to an OpenGL API instruction like [`glTexImage2D`](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glTexImage2D.xhtml) or [`glBufferData`](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glBufferData.xhtml). Your array has a type, so use that type in the interface and just get the void pointer to the data when you need it. By the way, the C++ way is `std::vector` or `std::array`. – Rabbid76 May 01 '22 at 07:05

1 Answers1

1
  1. As already commented above, using void* is not very useful.
  2. If you need an array object, it's recommended in C++ to use either std::array for a fixed size array, or std::vector for dynamic size array.
  3. This line: array_private = pass_array_here is simply assigning the pointer, not copying the array content.
  4. If you use std::array or std::vector as suggested in point 2 above, you can utilize the copy constructor of these classes to actually copy the array's content easily:
    void foo(std::vector<SomeType> const & pass_array_here) {
        array_private = pass_array_here;
    }
  1. If the caller of the function foo does not need to use the array after calling the function, you can save the copy of the data, and utilize C++ move semantics (available since C++ 11):
    void foo(std::vector<SomeType> && pass_array_here) {
        array_private = std::move(pass_array_here);
    }
  1. If the caller does not have a std::vector but an old C style array, it is possible to initialize the std::vector member from it (which will copy the data). See: std::vector::assign.

For 4, 5 and 6 above, your private data member will have to be defined as:

std::vector<SomeType> array_private; 
  1. If you need to access the data buffer managed by an std::vector, you can use std::vector::data: std::vector::data. You can cast this data pointer into a void* if you need to pass it to a C style function, e.g. in opengl (no need to copy the data).

  2. More about move semantics and R value references here: Understanding rvalue references

wohlstad
  • 12,661
  • 10
  • 26
  • 39
  • Thanks for the answer. Is std::vector compatible with OpenGL functions though? (such as glBufferData, which needs a const void* to pass the array), or do I need to first convert it or whatever? That is the actual reason I need to copy the array and hence it's important. – AggelosT May 01 '22 at 04:22
  • Yes, it is possible to use it with opengl. I updated my answer. – wohlstad May 01 '22 at 04:25
  • See point #7 in my answer. – wohlstad May 01 '22 at 04:42
  • @AggelosT please consider to accept my answer (the "v" below the score) if it was helpful. – wohlstad May 01 '22 at 05:20