struct and class size guarantees

Discussion in 'C++' started by =?ISO-8859-1?Q?Tanguy_Fautr=E9?=, Jun 2, 2004.

  1. Hi,

    I was wondering whether you can always assume that

    sizeof(foo) == sizeof(T) * Size;

    for

    template <class T, size_t Size>
    class foo
    {
    T bar[Size];
    };

    considering that the foo class will never have any virtual functions.
    Or is the compiler allowed to add padding or extra information, even in
    that case?

    Regards,

    Tanguy
     
    =?ISO-8859-1?Q?Tanguy_Fautr=E9?=, Jun 2, 2004
    #1
    1. Advertising

  2. Tanguy Fautré wrote:
    > I was wondering whether you can always assume that
    >
    > sizeof(foo) == sizeof(T) * Size;
    >
    > for
    >
    > template <class T, size_t Size>
    > class foo
    > {
    > T bar[Size];
    > };
    >
    > considering that the foo class will never have any virtual functions.


    No, you cannot assume that.

    > Or is the compiler allowed to add padding or extra information, even in
    > that case?


    Yes, most certainly.

    Victor
     
    Victor Bazarov, Jun 2, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tanguy Fautré

    std::multimap insertion order guarantees

    Tanguy Fautré, Oct 5, 2003, in forum: C++
    Replies:
    13
    Views:
    833
    David B. Held
    Oct 6, 2003
  2. Shane Groff
    Replies:
    1
    Views:
    806
    Howard Hinnant
    Oct 2, 2004
  3. bytebro

    Alignment guarantees?

    bytebro, Mar 17, 2008, in forum: C++
    Replies:
    6
    Views:
    456
    James Kanze
    Mar 18, 2008
  4. mdh

    Array definition guarantees

    mdh, Aug 13, 2008, in forum: C Programming
    Replies:
    14
    Views:
    532
  5. Fredrik Eriksson
    Replies:
    7
    Views:
    267
    Fredrik Eriksson
    Oct 19, 2011
Loading...

Share This Page