Re: why is this result ?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 18 January 2013 13:46, Andrew Haley wrote:
> On 01/18/2013 03:50 AM, horseriver wrote:
>> hi:
>>   I am doing a test for c++;
>>
>>   here is my code:
>>
>> #include <stdio.h>
>> class A
>> {};
>>
>> class B
>> {
>> public:
>>   B(){};
>>   ~B(){};
>> };
>>
>> int main()
>> {
>>
>>   printf("size of A is %d \n",sizeof(A));
>>   //printf("size of B is %d \n",sizeof(B));

Careful, you are using %d which expects an int but sizeof gives a size_t

>> }
>>
>> output is   "size of A is 1 " ,I can not understand this result ,
>>  there is no data in class A ,why here its size is 1?
>
> Because it's not possible to have an object with nonzero size.  The
> address of every object must be unique, so they have to be separated by
> one byte anyway.

Just to be clear, this is required by the standard (and the platform ABI)


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux