Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why the sizeof is 4

Reply
Thread Tools

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?
 
Reply With Quote
 
 
 
 
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].

 
Reply With Quote
 
 
 
 
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 } };


If your compiler didn't warn about this, turn the volume up until it does!

> void main()


Ditto this.

--
Ian Collins
 
Reply With Quote
 
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
 
Reply With Quote
 
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 .
 
Reply With Quote
 
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].


 
Reply With Quote
 
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].


 
Reply With Quote
 
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
 
Reply With Quote
 
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.

 
Reply With Quote
 
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
sizeof( int ) != sizeof( void * ) blufox C Programming 2 05-22-2006 03:25 PM
#define ARR_SIZE sizeof(arr)/sizeof(arr[0]) Vinu C Programming 13 05-12-2005 06:00 PM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM



Advertisments