Getting the size of an array of structs
I am just wondering as I have seen this in some sample code. And when I was looking at it more carefully I am wondering if two would do the same job in getting the correct size of the array structure.
My structure has 2 unsigned int (keeping it simple) 4 bytes each. And the array is only 2 elements.
static const unsigned int NUMBER_OF_PORTS = 2;
unsigned int portID;
unsigned int portType;
So my idea of the actual size of the array is this. (2 * 4) = (unsigned int * bytes) = 8.
The array size is 2. So that gives us 2 * 8 = 16.
The above structure is 16 bytes total. So with the code below sizeof(port) would be correct size.
Now, I have sent this in a sample C++ program
std::cout << "portID: " << sizeof(unsigned int) << std::endl; // 4 bytes
std::cout << "port: " << sizeof(port) << std::endl; //16 bytes
std::cout << "PORT: " << sizeof(PORT) << std::endl; //8 bytes
Now with what I have mentioned above. Would it not be possible to have this instead?
memset(port, 0, NUMBER_OF_PORTS * sizeof(PORT)); //Will give 16 bytes
I think this is more clear and more efficient as there is no calculation to perform.
memset(port, 0, sizeof(port));
Am I correct with my thinking.