I am writing a program in C++ and I need to initialize an array of a struct object I have created. It looks something like this:
typedef struct {
float x;
float y;
} vec2;
And then I initialize an array like this:
vec2 hotSpot[1000];
I thought when I initialized such an array, it would be completely empty, but when I print the value of sizeof(hotSpot), it says 8000!
Am I going wrong somewhere, or have I misunderstood some concept? How do I make this array empty?
Your
hotSpotcan not be empty as you initialized it as an array of 1000. There for, there are 1000 elements.When
vec2 hotSpot[1000];happens, it places all 1000 values as an uninitialized variable.The elements in
hotSpotare not valid as they have not been initialized. If you are looking to set them all to zero, you could use memset to initialize them all to zero.For more information on arrays, please take a look at this reference.