Hello,
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.
Code:
static const unsigned int NUMBER_OF_PORTS = 2;
struct PORTS
{
unsigned int portID;
unsigned int portType;
} port[NUMBER_OF_PORTS];
My structure has 2 unsigned int (keeping it simple) 4 bytes each. And the array is only 2 elements.
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.
Code:
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, I have sent this in a sample C++ program
Code:
memset(port, 0, NUMBER_OF_PORTS * sizeof(PORT)); //Will give 16 bytes
Now with what I have mentioned above. Would it not be possible to have this instead?
Code:
memset(port, 0, sizeof(port));
I think this is more clear and more efficient as there is no calculation to perform.
Am I correct with my thinking.
Thanks,