Topic: C/C++ sizeof operator difference


Author: celtschk@web.de (Christopher Eltschka)
Date: Wed, 16 Oct 2002 15:08:01 +0000 (UTC)
Raw View
jpsasiku@ssd.usa.alcatel.com ("JP.Sasikumar") writes:

> This is a multi-part message in MIME format.
> --------------4521687F2E81BED794589439
> Content-Type: text/plain; charset=us-ascii
> Content-Transfer-Encoding: 7bit
>
>
> Hi,
>     Is there any difference between C/C++ sizeof operator.
>
>     please check my following program.
>
>     Ex: size.c
>     -------
> #include <stdio.h>
>
> struct test /* Empty Structure */
> {
> };
> int main()
> {
>     struct test t1;
>     printf("size of :%d\n",sizeof(t1));
>     return 0;
> }
>
> if i compile this program in gcc compiler it returns 0.In g++ compiler
> it returns 1.
>
> if i change the file name from size.c to size.cc and compile the program
>
> in gcc ,it returns 1.
>
> can any one explain this one.why it is giving different output for C/C++
>
> compiler.
>
> Copiler version:
> gcc : gcc version 2.9-gnupro-99r1.000 for native 2.7Solaris
> g++: 2.9-gnupro-99r1.000

In C++, empty structs are defined by the language. And the standard
demands that they have nonzero size.

In C, empty structs are not part of the language, but a gcc extension
(which AFAIK predates the C++ rules). Therefore gcc empty structs
behave however they are defined in gcc, and that is having size 0.

Compiling with -ansi -pedantic should give a diagnostic, since those
options are meant to turn gcc into an ANSI/ISO C compiler. Indeed,
with those options gcc gives "warning: struct has no members".

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: francis.glassborow@ntlworld.com (Francis Glassborow)
Date: Wed, 11 Sep 2002 18:31:03 +0000 (UTC)
Raw View
In article <d6651fb6.0209100031.44514523@posting.google.com>, James
Kanze <kanze@gabi-soft.de> writes
>It the sentence you quote is all that the C standard says, it is
>undefined behavior, and anything the compiler does is OK.

The sentence I quote covers cases where none of the declarators provide
a name, but the grammar, if I have followed the thread correctly
requires at least one 'member' to be declared. I.e. an empty struct does
not meet the needs of the grammar (for C) and one without any named
member is a constraint violation resulting in UB. Quoting almost a page
of grammar seemed excessive, quoting the one line that indubitably makes
an empty struct have UB in C did not.


--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: ron@sensor.com ("Ron Natalie")
Date: Tue, 10 Sep 2002 19:52:28 +0000 (UTC)
Raw View
""Victor Bazarov"" <vAbazarov@dAnai.com> wrote in message news:unhn0qs3mh7d8d@corp.supernews.com...
> >
> I don't have the C90 Standard handy, but it is quite possible
> that sizeof(emptystruct) is 0.  In C++ it's 1.
>
There's no such thing as emptystruct in C.   All objects have non-zero
size.



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: barmar@genuity.net (Barry Margolin)
Date: Tue, 10 Sep 2002 20:02:01 +0000 (UTC)
Raw View
In article <1031341110.835503@master.nyc.kbcfp.com>,
Hyman Rosen <hyrosen@mail.com> wrote:
>JP.Sasikumar wrote:
>>     Is there any difference between C/C++ sizeof operator.
>
>Yes. In C++ sizeof('a') is 1 and in C++ it is sizeof(int) which is
>usually greater than 1.

That's not really a difference in the sizeof operator, it's a difference in
the types associated with character literals.  In C 'a' is an int, while in
C++ it's a char.  In both languages, the sizeof operator returns the size
of the corresponding type.

--
Barry Margolin, barmar@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: kanze@gabi-soft.de (James Kanze)
Date: Wed, 11 Sep 2002 02:55:28 +0000 (UTC)
Raw View
francis.glassborow@ntlworld.com (Francis Glassborow) wrote in message
news:<4J8rWVyuVRf9EwCQ@robinton.demon.co.uk>...
> In article <unhn0qs3mh7d8d@corp.supernews.com>, Victor Bazarov
> <vAbazarov@dAnai.com> writes
> >I don't have the C90 Standard handy, but it is quite possible
> >that sizeof(emptystruct) is 0.  In C++ it's 1.

> Perhaps I am completely wrong but I do not think the grammar of C90
> allows an empty struct. In addition we have in ISO/IEC 9899 1990,
> 6.5.2.1 penultimate sentence of third par on semantics:

> If the struct-declarator-list contains no name members, the behaviour
> is undefined.

> So I think the key difference is not in sizeof but in the requirements
> for a struct. Of course in C mode, if I am right about empty structs
> being forbidden in C, you should get a diagnostic for a syntax error.

It the sentence you quote is all that the C standard says, it is
undefined behavior, and anything the compiler does is OK.

As a QoI issue, I agree with you that a diagnostic would be the best
solution.  And note that pointer arithmetic causes problems with zero
length objects; it isn't just a question of identity.  Consider the
following (C or C++):

    #include <stdio.h>

    struct Zero {} ;

    int
    main()
    {
        struct Zero a[ 10 ] ;
        printf( "%lu\n", (unsigned long)(sizeof( a ) ) ) ;
        printf( "%l\n", (long)( &a[ 5 ] - &a[ 4 ] ) ) ;
        return 0 ;
    }

It generates a core dump when compiled with gcc (as C); the second
printf divides by zero.

--
James Kanze                           mailto:jkanze@caicheuvreux.com
Conseils en informatique orient   e objet/
                    Beratung in objektorientierter Datenverarbeitung

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: vAbazarov@dAnai.com ("Victor Bazarov")
Date: Mon, 9 Sep 2002 18:59:30 +0000 (UTC)
Raw View
""JP.Sasikumar"" <jpsasiku@ssd.usa.alcatel.com> wrote...
>
> Hi,
>     Is there any difference between C/C++ sizeof operator.

There probably is.

>
>     please check my following program.
>
>     Ex: size.c
>     -------
> #include <stdio.h>
>
> struct test /* Empty Structure */
> {
> };
> int main()
> {
>     struct test t1;
>     printf("size of :%d\n",sizeof(t1));
>     return 0;
> }
>
> if i compile this program in gcc compiler it returns 0.In g++ compiler
> it returns 1.
>
> if i change the file name from size.c to size.cc and compile the program
>
> in gcc ,it returns 1.

gcc knows that .cc is a C++ source.

> can any one explain this one.why it is giving different output for C/C++
>
> compiler.

I don't have the C90 Standard handy, but it is quite possible
that sizeof(emptystruct) is 0.  In C++ it's 1.

Victor
--
Please remove capital A's from my address when replying by mail


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: hyrosen@mail.com (Hyman Rosen)
Date: Mon, 9 Sep 2002 19:03:12 +0000 (UTC)
Raw View
JP.Sasikumar wrote:
> you mean to say that gcc compiler implementation of sizeof operator is wrong.

Yes.

>     In gcc compiler source file extension(.c/.cc) ,is it make any
> differnence in compilation?

Yes, .c is for C and .cc is for C++.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: francis.glassborow@ntlworld.com (Francis Glassborow)
Date: Mon, 9 Sep 2002 22:58:57 +0000 (UTC)
Raw View
In article <unhn0qs3mh7d8d@corp.supernews.com>, Victor Bazarov
<vAbazarov@dAnai.com> writes
>I don't have the C90 Standard handy, but it is quite possible
>that sizeof(emptystruct) is 0.  In C++ it's 1.

Perhaps I am completely wrong but I do not think the grammar of C90
allows an empty struct. In addition we have in ISO/IEC 9899 1990,
6.5.2.1 penultimate sentence of third par on semantics:

If the struct-declarator-list contains no name members, the behaviour is
undefined.

So I think the key difference is not in sizeof but in the requirements
for a struct. Of course in C mode, if I am right about empty structs
being forbidden in C, you should get a diagnostic for a syntax error.




--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: jpsasiku@ssd.usa.alcatel.com ("JP.Sasikumar")
Date: Fri, 6 Sep 2002 15:36:08 +0000 (UTC)
Raw View
This is a multi-part message in MIME format.
--------------4521687F2E81BED794589439
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit


Hi,
    Is there any difference between C/C++ sizeof operator.

    please check my following program.

    Ex: size.c
    -------
#include <stdio.h>

struct test /* Empty Structure */
{
};
int main()
{
    struct test t1;
    printf("size of :%d\n",sizeof(t1));
    return 0;
}

if i compile this program in gcc compiler it returns 0.In g++ compiler
it returns 1.

if i change the file name from size.c to size.cc and compile the program

in gcc ,it returns 1.

can any one explain this one.why it is giving different output for C/C++

compiler.

Copiler version:
gcc : gcc version 2.9-gnupro-99r1.000 for native 2.7Solaris
g++: 2.9-gnupro-99r1.000

thanks
Sasikumar


--------------4521687F2E81BED794589439
Content-Type: text/x-vcard; charset=us-ascii;
 name="jpsasiku.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for JP.Sasikumar
Content-Disposition: attachment;
 filename="jpsasiku.vcf"

begin:vcard
n:JP;Sasikumar
x-mozilla-html:FALSE
org:Axes Technologies (I) Pvt Ltd;System Manager
adr:;;;;;;
version:2.1
email;internet:jpsasiku@ssd.usa.alcatel.com
x-mozilla-cpt:;0
fn:Sasikumar JP
end:vcard

--------------4521687F2E81BED794589439--

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: hyrosen@mail.com (Hyman Rosen)
Date: Fri, 6 Sep 2002 19:49:55 +0000 (UTC)
Raw View
JP.Sasikumar wrote:
>     Is there any difference between C/C++ sizeof operator.

Yes. In C++ sizeof('a') is 1 and in C++ it is sizeof(int) which is
usually greater than 1.

> struct test { };
>
> if i compile this program in gcc compiler it returns 0.In g++ compiler
> it returns 1.

No structure size is permitted to be zero, whether in C or C++.
If a C compiler is producing sizeof(test) == 0, then it is wrong.

In fact, try running this program under gcc:

#include <stdio.h>
struct t { };
int main()
{
 volatile struct t *p, *q;
 struct t ts[10];
 p = ts + 0;
 q = ts + 10;
 printf("%d\n", q - p);
}

I got a core dump.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: jpsasiku@ssd.usa.alcatel.com ("JP.Sasikumar")
Date: Sat, 7 Sep 2002 04:58:32 +0000 (UTC)
Raw View
This is a multi-part message in MIME format.
--------------56A514B70EC5E6F60DD2849E
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

 Hi,
    you mean to say that gcc compiler implementation of sizeof operator is
wrong.

    if i save this same program with .cc extension and comiple it with gcc
comipler,it gives difference 10.

    In gcc compiler source file extension(.c/.cc) ,is it make any
differnence in compilation?


thanks
Sasikumar


Hyman Rosen wrote:

> JP.Sasikumar wrote:
> >     Is there any difference between C/C++ sizeof operator.
>
> Yes. In C++ sizeof('a') is 1 and in C++ it is sizeof(int) which is
> usually greater than 1.
>
> > struct test { };
> >
> > if i compile this program in gcc compiler it returns 0.In g++ compiler
> > it returns 1.
>
> No structure size is permitted to be zero, whether in C or C++.
> If a C compiler is producing sizeof(test) == 0, then it is wrong.
>
> In fact, try running this program under gcc:
>
> #include <stdio.h>
> struct t { };
> int main()
> {
>         volatile struct t *p, *q;
>         struct t ts[10];
>         p = ts + 0;
>         q = ts + 10;
>         printf("%d\n", q - p);
> }
>
> I got a core dump.
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]

--------------56A514B70EC5E6F60DD2849E
Content-Type: text/x-vcard; charset=us-ascii;
 name="jpsasiku.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for JP.Sasikumar
Content-Disposition: attachment;
 filename="jpsasiku.vcf"

begin:vcard
n:JP;Sasikumar
x-mozilla-html:FALSE
org:Axes Technologies (I) Pvt Ltd;System Manager
adr:;;;;;;
version:2.1
email;internet:jpsasiku@ssd.usa.alcatel.com
x-mozilla-cpt:;0
fn:Sasikumar JP
end:vcard

--------------56A514B70EC5E6F60DD2849E--

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]