what is the size of empty class in C++,java?

前端 未结 8 2103
北恋
北恋 2020-12-03 17:35

What is the size of an empty class in C++ and Java? Why is it not zero? sizeof(); returns 1 in the case of C++.

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-03 18:03

    Because an object has to have an address in memory, and to have an address in memory, it has to occupy "some" memory. So, it is usually, in C++, the smallest possible amount, i.e. 1 char (but that might depend on the compiler). In Java, I wouldn't be so sure.. it might have some default data (more than just a placeholder like in C++), but it would be surprising if it was much more than in C++.

提交回复
热议问题