Alternate way of computing size of a type using pointer arithmetic

前端 未结 9 552
广开言路
广开言路 2021-01-12 04:53

Is the following code 100% portable?

int a=10;
size_t size_of_int = (char *)(&a+1)-(char*)(&a); // No problem here?

std::cout<

        
9条回答
  •  死守一世寂寞
    2021-01-12 05:18

    There was a debate on a similar question.

    See the comments on my answer to that question for some pointers at why this is not only non-portable, but also is undefined behaviour by the standard.

提交回复
热议问题