public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: Differences in struct size when compiling C vs. C++
       [not found] <13FCCC1F3509D411B1C700A0C969DEBB05E20D01@fmsmsx91.fm.intel .com>
@ 2002-01-22 13:09 ` Andrea 'Fyre Wyzard' Bocci
  0 siblings, 0 replies; 4+ messages in thread
From: Andrea 'Fyre Wyzard' Bocci @ 2002-01-22 13:09 UTC (permalink / raw)
  To: Gonzalez, Inaky, gcc-help

I'm not sure how it REALLY works.
 From what I understood, the problem should arise when coding the class like

>template<class T>
>struct A
>{
>   T c;
>   int a;
>   int b;
>};

because the member class would otherwise (ie, without the extra byte) have 
the same address as the container struct.
On the other hand, if you write, like in your example,

>template<class T>
>struct A
>{
>   int a;
>   int b;
>   T c;
>};

the extrra space should not be needed, as A and A.c already have differetn 
addresses.
If the proble shows up even in this second example, then there must 
something else causing it.
In this case, you might try posting again to gcc-help@gcc.gnu.org and 
gcc@gcc.gnu.org (this might be a feature, a bug, or whatever...).

HTH
fwyzard

^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: Differences in struct size when compiling C vs. C++
@ 2002-01-22 12:20 Gonzalez, Inaky
  0 siblings, 0 replies; 4+ messages in thread
From: Gonzalez, Inaky @ 2002-01-22 12:20 UTC (permalink / raw)
  To: 'Andrea 'Fyre Wyzard' Bocci', gcc-help



> I think this is the same as in issue discussed a month ago on 
> the gcc ML 
> for classes (http://gcc.gnu.org/ml/gcc/2001-12/msg00836.html).
> Shortly:
> The empty struct sub-object must have a different address 
> from the struct 
> that contains it.
> This is obvious with A, where "int d" between the two makes 
> this happen 
> automatically.
> With B, the compiler must use an empty byte to let the struct 
> {} c addres 
> be different from that of B.

	Do you know if there is anyway then to do it? I have this data type,
and I template it:

template<class T>
struct A
{
  int a;
  int b;
  T c;
};

The idea is T is normally a class that offers an interface like:

struct T1
{
  void fn1 (void);
  void fn2 (void);
 private:
  // data members
};

And sometimes it is like:

struct T2
{
  static void fn1 (void);
  static void fn2 (void);
  // no data members, stateless
}

So, I want struct A<T> to be minimal size (8 bytes) when I am using T2 [as
it in theory should be] and to be whatever it has to be if it is T1. Note
there is no runtime discovery of types implied here, as it is all know at
compile time (love templates :).

I would expect that being that empty struct, it'd be optimized out by the
compiler, so I can use the same interface w/o the size penalty, but looks
like GCC thinks differently. Maybe there is a way to trick it ...


Iñaky Pérez González -- (503) 677 6807
I do not speak for Intel Corp, opinions are my own.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Differences in struct size when compiling C vs. C++
  2002-01-16 20:14 inaky.gonzalez
@ 2002-01-17  4:20 ` Andrea 'Fyre Wyzard' Bocci
  0 siblings, 0 replies; 4+ messages in thread
From: Andrea 'Fyre Wyzard' Bocci @ 2002-01-17  4:20 UTC (permalink / raw)
  To: inaky.gonzalez, gcc-help

At 20.06 16/01/2002 (GMT -0800), inaky.gonzalez@intel.com wrote:

>$ gcc kk.c -o kk -Wall
>
>output is:
>   sizeof (struct A) 4, sizeof (struct B) 0
>
>$ g++ kk.c -o kk -Wall
>
>output is:
>   sizeof (struct A) 8, sizeof (struct B) 1
>
>
>Why? :) I would expect this if there were vtables around, but I am
>afraid there aren't any. Can anybody give me some insight? [pls copy
>me, as I am not subscribed to the list]
>
>     Thanks!
>
>PS: Great compiler, though :)
>
>
>-- begin kk.c...
>
>#include <stdio.h>
>
>struct A
>{
>   int d;
>   struct {} c;
>};
>
>struct B
>{
>   struct
>   {
>   } c;
>};
>
>int main (void)
>{
>   printf ("sizeof (struct A) %d, sizeof (struct B) %d\n",
>           sizeof (struct A), sizeof (struct B));
>   return 0;
>}

I think this is the same as in issue discussed a month ago on the gcc ML 
for classes (http://gcc.gnu.org/ml/gcc/2001-12/msg00836.html).
Shortly:
The empty struct sub-object must have a different address from the struct 
that contains it.
This is obvious with A, where "int d" between the two makes this happen 
automatically.
With B, the compiler must use an empty byte to let the struct {} c addres 
be different from that of B.
Then everything is alligned to 32 bit boundaries.
Well - I don't know why this doesn't happen with plai gcc. Maybe C doesn't 
need to keep the addresses different ?

HTH
fwyzard

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Differences in struct size when compiling C vs. C++
@ 2002-01-16 20:14 inaky.gonzalez
  2002-01-17  4:20 ` Andrea 'Fyre Wyzard' Bocci
  0 siblings, 1 reply; 4+ messages in thread
From: inaky.gonzalez @ 2002-01-16 20:14 UTC (permalink / raw)
  To: gcc-help


Hi people

Tried this with gcc and g++ versions 2.95.4 and 3.0.3. I am looking
for some enlightment on why the struct's sizes are so wildly different
in C and in C++:


Compile with gcc:

$ gcc kk.c -o kk -Wall

output is: sizeof (struct A) 4, sizeof (struct B) 0

now with G++: 

$ g++ kk.c -o kk -Wall

output is: sizeof (struct A) 8, sizeof (struct B) 1


Why? :) I would expect this if there were vtables around, but I am
afraid there aren't any. Can anybody give me some insight? [pls copy
me, as I am not subscribed to the list]

    Thanks!

PS: Great compiler, though :)


-- begin kk.c...

#include <stdio.h>

struct A
{
  int d;
  struct {} c;
};

struct B
{
  struct 
  {
  } c;
};

int main (void)
{
  printf ("sizeof (struct A) %d, sizeof (struct B) %d\n",
          sizeof (struct A), sizeof (struct B));
  return 0;
}



-- 

 naky @ somewhere in time  --  e-ssing PQ even++

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2002-01-22 21:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <13FCCC1F3509D411B1C700A0C969DEBB05E20D01@fmsmsx91.fm.intel .com>
2002-01-22 13:09 ` Differences in struct size when compiling C vs. C++ Andrea 'Fyre Wyzard' Bocci
2002-01-22 12:20 Gonzalez, Inaky
  -- strict thread matches above, loose matches on Subject: below --
2002-01-16 20:14 inaky.gonzalez
2002-01-17  4:20 ` Andrea 'Fyre Wyzard' Bocci

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).