sizeof(value) vs sizeof(type)?

前端 未结 3 546
一生所求
一生所求 2021-01-02 23:37

Considering :

double data; 
double array[10]; 
std::vector vec(4, 100); 
MyClass myclass;   

Is there a difference between :

3条回答
  •  迷失自我
    2021-01-03 00:06

    Whenever you use sizeof(), it is always evaluated at compile time. It is the datatype of the parameter that sizeof is concerned with.

    However, you can pass values, and even expressions as parameter. But the sizeof function will only consider the dataype of the passed parameter.

    You can also pass expressions as sizeof(x = x+1);. Say x was int. In this case this sizeof will return 4(as on my machine) and the value of x will remain unchanged. This is because sizeof is always evaluated at compile time.

    The two set of syntax you provide, will return the same result, but you cannot say they are equivalent. But yes, latter is defined in terms of former.

提交回复
热议问题