Velocity Reviews > Why the sizeof is 4

Why the sizeof is 4

Hill Pang
Guest
Posts: n/a

 06-12-2012
The is my code:

struct f1 {
int x; int y[];
} f1 = { 1, { 2, 3, 4 } };

void main()
{
printf("sizeof %d\n", sizeof f1);
}

The output(by GCC) is 4.
My question is why it doesn't count the size of the flexible array while it is static initialized?
In other words, if I want to know the *really* size of f1, how to do it? is it possible?

Johann Klammer
Guest
Posts: n/a

 06-12-2012
Hill Pang wrote:
> The is my code:
>
> struct f1 {
> int x; int y[];
> } f1 = { 1, { 2, 3, 4 } };
>
> void main()
> {
> printf("sizeof %d\n", sizeof f1);
> }
>
> The output(by GCC) is 4.
> My question is why it doesn't count the size of the flexible array while it is static initialized?
> In other words, if I want to know the *really* size of f1, how to do it? is it possible?

Not using sizeof alone.
You could try:

struct f1 {
int x; size_t size; int y[];
} f1 = { 1, 3, { 2, 3, 4 } };

void main()
{
printf("sizeof %d\n", sizeof(f1)+sizeof(f1.y)*f1.size);
}

possibly wrap it in some #define

I doubt it is safe to use in a static declaration like this.
You may be overrunning the end...
Better do a malloc(), or declare with y[3].

Ian Collins
Guest
Posts: n/a

 06-12-2012
On 06/12/12 03:39 PM, Hill Pang wrote:
> struct f1 {
> int x; int y[];
> } f1 = { 1, { 2, 3, 4 } };

> void main()

Ditto this.

--
Ian Collins

Ian Collins
Guest
Posts: n/a

 06-12-2012
On 06/12/12 03:39 PM, Hill Pang wrote:
> The is my code:
>
> struct f1 {
> int x; int y[];
> } f1 = { 1, { 2, 3, 4 } };
>
> void main()
> {
> printf("sizeof %d\n", sizeof f1);
> }
>
> The output(by GCC) is 4.
> My question is why it doesn't count the size of the flexible array while it is static initialized?

See paragraph 17 in section 6.7.2.1 of C99 and note the example.

> In other words, if I want to know the *really* size of f1, how to do it? is it possible?

No, you have to save it explicitly somewhere.

--
Ian Collins

Ike Naar
Guest
Posts: n/a

 06-12-2012
On 2012-06-12, Johann Klammer <(E-Mail Removed)1.net> wrote:
> printf("sizeof %d\n", sizeof(f1)+sizeof(f1.y)*f1.size);

sizeof f1.y is wrong,
you probably meant sizeof f1.y[0] or sizeof *f1.y .

Zhang Yuan
Guest
Posts: n/a

 06-12-2012
On Tuesday, June 12, 2012 1:11:15 PM UTC+8, Johann Klammer wrote:
> Hill Pang wrote:
> > The is my code:
> >
> > struct f1 {
> > int x; int y[];
> > } f1 = { 1, { 2, 3, 4 } };
> >
> > void main()
> > {
> > printf("sizeof %d\n", sizeof f1);
> > }
> >
> > The output(by GCC) is 4.
> > My question is why it doesn't count the size of the flexible array while it is static initialized?
> > In other words, if I want to know the *really* size of f1, how to do it? is it possible?

>
> Not using sizeof alone.
> You could try:
>
> struct f1 {
> int x; size_t size; int y[];
> } f1 = { 1, 3, { 2, 3, 4 } };
>
> void main()
> {
> printf("sizeof %d\n", sizeof(f1)+sizeof(f1.y)*f1.size);

why sizeof(f1) become to 8;?

> }
>
> possibly wrap it in some #define
>
> I doubt it is safe to use in a static declaration like this.
> You may be overrunning the end...
> Better do a malloc(), or declare with y[3].

Zhang Yuan
Guest
Posts: n/a

 06-12-2012
On Tuesday, June 12, 2012 1:11:15 PM UTC+8, Johann Klammer wrote:
> Hill Pang wrote:
> > The is my code:
> >
> > struct f1 {
> > int x; int y[];
> > } f1 = { 1, { 2, 3, 4 } };
> >
> > void main()
> > {
> > printf("sizeof %d\n", sizeof f1);
> > }
> >
> > The output(by GCC) is 4.
> > My question is why it doesn't count the size of the flexible array while it is static initialized?
> > In other words, if I want to know the *really* size of f1, how to do it? is it possible?

>
> Not using sizeof alone.
> You could try:
>
> struct f1 {
> int x; size_t size; int y[];
> } f1 = { 1, 3, { 2, 3, 4 } };
>

> void main()
> {
> printf("sizeof %d\n", sizeof(f1)+sizeof(f1.y)*f1.size);
> }
>
> possibly wrap it in some #define
>
> I doubt it is safe to use in a static declaration like this.
> You may be overrunning the end...
> Better do a malloc(), or declare with y[3].

On Tuesday, June 12, 2012 1:11:15 PM UTC+8, Johann Klammer wrote:
> Hill Pang wrote:
> > The is my code:
> >
> > struct f1 {
> > int x; int y[];
> > } f1 = { 1, { 2, 3, 4 } };
> >
> > void main()
> > {
> > printf("sizeof %d\n", sizeof f1);
> > }
> >
> > The output(by GCC) is 4.
> > My question is why it doesn't count the size of the flexible array while it is static initialized?
> > In other words, if I want to know the *really* size of f1, how to do it? is it possible?

>
> Not using sizeof alone.
> You could try:
>
> struct f1 {
> int x; size_t size; int y[];
> } f1 = { 1, 3, { 2, 3, 4 } };
>
> void main()
> {
> printf("sizeof %d\n", sizeof(f1)+sizeof(f1.y)*f1.size);
> }

why sizeof(f1) changed from 4 to 8 ?

>
> possibly wrap it in some #define
>
> I doubt it is safe to use in a static declaration like this.
> You may be overrunning the end...
> Better do a malloc(), or declare with y[3].

James Kuyper
Guest
Posts: n/a

 06-12-2012
On 06/12/2012 07:02 AM, Zhang Yuan wrote:
> On Tuesday, June 12, 2012 1:11:15 PM UTC+8, Johann Klammer wrote:

....
>> struct f1 {
>> int x; size_t size; int y[];
>> } f1 = { 1, 3, { 2, 3, 4 } };
>>
>> void main()
>> {
> > printf("sizeof %d\n", sizeof(f1)+sizeof(f1.y)*f1.size);

> why sizeof(f1) become to 8;?

It now includes both 'x' and 'size'. It does not include y: "the size of
the structure is as if the flexible array member were omitted except
that it may have more trailing padding than the omission would imply."
(6.7.2.1p1

--
James Kuyper

Johann Klammer
Guest
Posts: n/a

 06-12-2012
Zhang Yuan wrote:
>> void main()
> > {
> > printf("sizeof %d\n", sizeof(f1)+sizeof(f1.y)*f1.size);

Sorry, Imade a mistake. it should be: "sizeof(f1)+sizeof(f1.y[0])*f1.size"
as Ike Naar posted earlier.
> > }

> why sizeof(f1) changed from 4 to 8 ?
>

It is now something like sizeof(int)+sizeof(size_t).
But may be more if padding is inserted by compiler.

Hill Pang
Guest
Posts: n/a

 06-12-2012
On Tuesday, June 12, 2012 1:31:04 PM UTC+8, Ian Collins wrote:
> On 06/12/12 03:39 PM, Hill Pang wrote:
> > The is my code:
> >
> > struct f1 {
> > int x; int y[];
> > } f1 = { 1, { 2, 3, 4 } };
> >
> > void main()
> > {
> > printf("sizeof %d\n", sizeof f1);
> > }
> >
> > The output(by GCC) is 4.
> > My question is why it doesn't count the size of the flexible array while it is static initialized?

>
> See paragraph 17 in section 6.7.2.1 of C99 and note the example.
>
> > In other words, if I want to know the *really* size of f1, how to do it? is it possible?

>
> No, you have to save it explicitly somewhere.
>
> --

Yes, C99 will ignore the flexible array member while counting the structuresize, but in C99 it also specify that the flexible array member could not be static initialized, the example shows it:
>> struct s t2 = { 1, { 4.2 }}; // invalid.

So the static initialization to flexible array member is a extension of GCC, while it is initialized statically, from my opinion, it should make senseto count the size of the flexible array member. I would suggest this as a enhancement of GCC to the great GCC developer.

> Ian Collins