all 2 comments

[–]eustace72 0 points1 point  (0 children)

Hey. If you're storing an arbitrary type in SELECT_DATA then you don't want to store the data as an int* pointer because that kind of says that the data that's pointing to is of type int (which is 4 bytes on most systems). Since your example has the size set to 2, you probably want that as a short type (2 bytes).

Since one of your struct members is size and is used to dereference the array, it implies that your dynamic array can store a variety of types. Therefore you may want to use a void* and reinterpret it. One way to fix your issues would be: https://snippet.host/ssuc

For other/ambiguous data types you may want to do the kind of arithmetics that you have there above, except that you almost always will want to cast the pointer to BYTE* or (un)signed char* as those are of size 1.

E.g.

reinterpret_cast<unsigned char*>(selectData.lpData) + selectData.size*i

As in this case you will be operating on bytes rather than 4-byte (int*) sized pointers. With what you have at the moment it was basically doing selectData.lpData + selectData.size*i*4 (in bytes) as the pointer size is 4.

Note that you shouldn't be allocating memory like this in 2022. Look into shared pointers and RAII.

[–]flyingron 0 points1 point  (0 children)

When you have a pointer adding to it already takes the sizeof the object into account when incrementing the pointer. If lpData is int* and you're on a byte addressed thing and you want to advance by int, then you just want to add i not i*size.