* why is this result ?
@ 2013-01-18 13:46 horseriver
2013-01-18 14:26 ` Andrew Haley
0 siblings, 1 reply; 7+ messages in thread
From: horseriver @ 2013-01-18 13:46 UTC (permalink / raw)
To: gcc-help
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));
}
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?
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: why is this result ?
2013-01-18 13:46 why is this result ? horseriver
@ 2013-01-18 14:26 ` Andrew Haley
2013-01-18 20:49 ` Jonathan Wakely
2013-01-19 3:00 ` horseriver
0 siblings, 2 replies; 7+ messages in thread
From: Andrew Haley @ 2013-01-18 14:26 UTC (permalink / raw)
To: horseriver; +Cc: gcc-help
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));
> }
>
> 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.
Andrew.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: why is this result ?
2013-01-18 14:26 ` Andrew Haley
@ 2013-01-18 20:49 ` Jonathan Wakely
2013-01-19 3:22 ` horseriver
2013-01-19 3:00 ` horseriver
1 sibling, 1 reply; 7+ messages in thread
From: Jonathan Wakely @ 2013-01-18 20:49 UTC (permalink / raw)
To: horseriver; +Cc: gcc-help
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)
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: why is this result ?
2013-01-18 14:26 ` Andrew Haley
2013-01-18 20:49 ` Jonathan Wakely
@ 2013-01-19 3:00 ` horseriver
[not found] ` <CABJqhQOL7=EwaGORsWmuZTu8R3+pQOcYxBwbkxzqossrK7NB1w@mail.gmail.com>
1 sibling, 1 reply; 7+ messages in thread
From: horseriver @ 2013-01-19 3:00 UTC (permalink / raw)
To: Andrew Haley; +Cc: gcc-help
On Fri, Jan 18, 2013 at 01:46:22PM +0000, 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));
> > }
> >
> > 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.
thanks!
Here I do not define a object of type A ,just do sizeof operation to a A struct ,not a specified object.
So if I defined A a , does sizeof(a) have the same mean with sizeof(A) ?
what does the sizeof operator essentially?
>
> Andrew.
>
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: why is this result ?
2013-01-18 20:49 ` Jonathan Wakely
@ 2013-01-19 3:22 ` horseriver
2013-01-19 14:11 ` Jonathan Wakely
0 siblings, 1 reply; 7+ messages in thread
From: horseriver @ 2013-01-19 3:22 UTC (permalink / raw)
To: Jonathan Wakely; +Cc: gcc-help
On Fri, Jan 18, 2013 at 03:27:25PM +0000, Jonathan Wakely wrote:
> 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
Thanks!
I have not understand what you mean ? size_t is a unsigned int ,I think it does not matter here
>
> >> }
> >>
> >> 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)
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: why is this result ?
[not found] ` <20130118234043.GI2376@debian.localdomain>
@ 2013-01-19 13:24 ` Jędrzej Dudkiewicz
0 siblings, 0 replies; 7+ messages in thread
From: Jędrzej Dudkiewicz @ 2013-01-19 13:24 UTC (permalink / raw)
To: gcc-help; +Cc: horseriver
On Sat, Jan 19, 2013 at 12:40 AM, horseriver <horserivers@gmail.com> wrote:
> On Sat, Jan 19, 2013 at 09:15:23AM +0100, Jędrzej Dudkiewicz wrote:
>> >> 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.
>> >
>> > thanks!
>> > Here I do not define a object of type A ,just do sizeof operation to a A struct ,not a specified object.
>> > So if I defined A a , does sizeof(a) have the same mean with sizeof(A) ?
>> > what does the sizeof operator essentially?
>>
>> Yes, sizeof(a) and sizeof(A) are identical. I understand sizeof(expr)
>> as "tell me how many bytes I need to store result of expression expr"
> .
> When a is stored in one bytes, what is the mean or use of that byte data?
Apologies for sending this message only to you and not to gcc-help.
There is no use for this byte, it's only purpose is to avoid objects
of size 0, so you can say that it's wasted. But if you derive from
such class/struct, you get "empty-base-class-optimization", so no
space wasted here.
--
Jędrzej Dudkiewicz
I really hate this damn machine, I wish that they would sell it.
It never does just what I want, but only what I tell it.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: why is this result ?
2013-01-19 3:22 ` horseriver
@ 2013-01-19 14:11 ` Jonathan Wakely
0 siblings, 0 replies; 7+ messages in thread
From: Jonathan Wakely @ 2013-01-19 14:11 UTC (permalink / raw)
To: horseriver; +Cc: gcc-help
On Jan 19, 2013 3:00 AM, "horseriver" wrote:
>
> On Fri, Jan 18, 2013 at 03:27:25PM +0000, Jonathan Wakely wrote:
> > >> 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
> Thanks!
> I have not understand what you mean ? size_t is a unsigned int ,I think it does not matter here
It's quite easy to understand.
Your printf should be passed an int
The sizeof operator does not give you an int. On my platform it gives
a 64-bit integer. Even on your platform it does not give you an int,
unsigned int is not int.
The code is not portable, so you should be careful about writing and
using such code. If you compile with warnings you will get a warning
about it. It's your code so you can choose to ignore it if you think
you know better than the compiler. But you should be careful.
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2013-01-19 13:26 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-18 13:46 why is this result ? horseriver
2013-01-18 14:26 ` Andrew Haley
2013-01-18 20:49 ` Jonathan Wakely
2013-01-19 3:22 ` horseriver
2013-01-19 14:11 ` Jonathan Wakely
2013-01-19 3:00 ` horseriver
[not found] ` <CABJqhQOL7=EwaGORsWmuZTu8R3+pQOcYxBwbkxzqossrK7NB1w@mail.gmail.com>
[not found] ` <20130118234043.GI2376@debian.localdomain>
2013-01-19 13:24 ` Jędrzej Dudkiewicz
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).