1

Lets say I have a static array of an object which size cannot change.

struct vector2 { //8 Bytes
    float x, y;
};

Does the computer calculate the size every time sizeof is called or is this stored somewhere? If not, does it have the same performance as it was stored somewhere?

std::cout <<  sizeof(vector2) << std::endl;
std::cout <<  sizeof(vector2) << std::endl;

Is this as fast as

Byte sizeOfVector2 = sizeof(vector2);
std::cout <<  sizeOfVector2 << std::endl;
std::cout <<  sizeOfVector2 << std::endl;
Kiryu144
  • 59
  • 8

2 Answers2

6

sizeof is calculated at compile time.

Baum mit Augen
  • 47,658
  • 24
  • 139
  • 177
doron
  • 26,460
  • 11
  • 62
  • 99
2

The sizeof operator is computed at compile time.

Shahar Bental
  • 961
  • 6
  • 15