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
  • 89
  • 9

2 Answers2

6

sizeof is calculated at compile time.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
doron
  • 27,972
  • 12
  • 65
  • 103
2

The sizeof operator is computed at compile time.

Shahar Bental
  • 991
  • 6
  • 16