So I have a C++ struct, which has a static array as a member, and I want to ask for it's size in the constructor. As I understood from this article http://msdn.microsoft.com/en-us/library/4s7x1k91(VS.71).aspx, sizeof can be applied to a static array to find the size (in bytes) of the entire array, not just it's type. However, when I do sizeof()
on the member, it gives me 4 (the size of the pointer) instead of the size of the array. Here's the context (trivialized):
struct A
{
char descirption[128];
int value;
A(const char desc[], int val)
{
size_t charsToCopy = std::min(sizeof(description), sizeof(desc));
memcpy(description, desc, charsToCopy);
value = val;
}
}
int main()
{
A instance("A description string", 1);
//now instance has a description string that says "A des" followed by garbage characters
}
So how do I get the size of a member char array?
Edit
When I put a breakpoint in the compiler and inspect the two values sizeof(description)
and sizeof(desc)
I see sizeof(description) == 4
, and sizeof(desc) == 21
. Hence my confusion. Because I'm passing a string literal into the constructor, the compiler seems perfectly happy to tell me the real size of the string passed in. Maybe that wouldn't be the case if I assigned it to a variable somewhere, but right now I'm trying to track down the fundamental problem: sizeof( some-member-static-array ) gives me something (relatively) meaningless.
Is it possible that sizeof is doing some sort of string length measurement because it's an array of chars?