Reputation: 127
I created a vector of pointers to an array
vector<PxVec3*> gConvexMeshVertexesArrays;
I added a link to the array with elements
static PxVec3 convexVerts[] = {
PxVec3(-0.000000, 1.295039, -5.117647),
PxVec3(-9.881939, 10.235294, 5.235294),
PxVec3(-10.117647, 10.235294, 5.235294)
}
Then I try to get the size of this array
cout << "in = " << sizeof(convexVerts) / sizeof(PxVec3) << endl; //ok
gConvexMeshVertexesArrays.push_back(convexVerts);
cout << "in2 = " << sizeof(*(gConvexMeshVertexesArrays.back())) / sizeof(PxVec3) << endl; //wrong!
How can I make the second option work correctly? After all, back() returns a reference to the pointer and the size of the array I can't get from the reference.
Thanks.
Upvotes: 0
Views: 312
Reputation: 238351
I created a vector of pointers to an array
vector<PxVec3*> gConvexMeshVertexesArrays;
You've created a vector of pointers to PxVec3
, which I assume is not an array. Those pointed PxVec3
objects may be elements of an array though.
cout << "in2 = " << sizeof(*(gConvexMeshVertexesArrays.back())) / sizeof(PxVec3) << endl; //wrong!
How can I make the second option work correctly?
This would work, if you actually did have a vector of pointers to an array:
std::vector<PxVec3(*)[3]> gConvexMeshVertexesArrays;
This way, indirecting through the pointer to array, you get an lvalue reference to the array itself, and you can get the size of that array the same way as you'd get it from the array directly as you are attempting here.
If you instead have a pointer to an element of an array, as you do in the example, then there is no general way to get the size of the array that contains the element from that pointer.
In some particular cases, there may be a way to calculate the size however. For example, you could decide that certain value represents the end of the array, in which case you can iterate the successive sibling of the pointed object until you reach the terminator. This is conventionally done with character strings where the null terminator character represents end of a string.
In case you want to store a pointer to an element of an array, and want to know the size and cannot choose a value as terminator, then you would typically store the size along with the pointer.
gConvexMeshVertexesArrays.push_back(convexVerts);
I successfully add a pointer to an array to the vector and then try to get the size of that array.
No, you don't. You push a pointer to the first element of an array into the vector. Then you get the size of the array element (whether you intended that or not), which you divide by the size of the element, which always results in 1.
Upvotes: 1
Reputation: 26066
There is no way to know the size of some memory a pointer may be pointing to because pointers have no information about what data they are pointing to (except the declared type, that is).
If you want a (pointer, size) pair, take a look at std::span
in C++20 and similar types provided in some libraries.
The well-known sizeof(array) / sizeof(element)
expression works for arrays (and it does so at compile-time) because arrays statically know their entire size (plus they are contiguous, etc.).
Upvotes: 2