regarding sizeof operator for class with no data variables

Discussion in 'C++' started by pratap, Aug 19, 2007.

  1. pratap

    pratap Guest

    consider this code snippet

    why is the compiler forced to allocate 1 byte of memory for a class
    which contains no data members.suppose i define more than 1 functions
    inside this class ,why is the size of the class still 1 byte. What is
    the reason for the allocation of 1 byte by the compiler for the class
    A.

    #include<iostream>
    using namespace std;

    class A
    {
    void func()
    { }
    void func1()
    { }
    void func2()
    { }
    void func3()
    { }
    };

    int main()
    {
    cout<<sizeof(A)<<endl;
    return 0;
    }


    Regards
    Pratap
     
    pratap, Aug 19, 2007
    #1
    1. Advertising

  2. On 2007-08-19 20:33, pratap wrote:
    > consider this code snippet
    >
    > why is the compiler forced to allocate 1 byte of memory for a class
    > which contains no data members.suppose i define more than 1 functions
    > inside this class ,why is the size of the class still 1 byte. What is
    > the reason for the allocation of 1 byte by the compiler for the class
    > A.


    The class does not have a size, an object of the class does. And the
    reason is that each object must have an unique address, and how you you
    have an unique address if you don't have a size? And the reason they
    need an address is because that's how you refer to an object (in the end
    when all fancy variable names have been removed by the compiler).

    --
    Erik Wikström
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Aug 19, 2007
    #2
    1. Advertising

  3. pratap

    Guest Guest

    I think you should read "Inside the C++ Object Model" written by
    Stanley B. Lippman, you will get what you want there.
     
    Guest, Aug 20, 2007
    #3
  4. On 2007-08-20 03:50, wrote:
    > I think you should read "Inside the C++ Object Model" written by
    > Stanley B. Lippman, you will get what you want there.


    Please quote the text you are replying to, if you don't it's hard to
    determine your intentions. As an example, since you replied to my reply
    to the op, your reply might be taken to mean that I was wrong and you
    are correcting me.

    --
    Erik Wikström
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Aug 20, 2007
    #4
    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. GRenard
    Replies:
    2
    Views:
    443
    Salt_Peter
    Nov 3, 2006
  2. pratap
    Replies:
    0
    Views:
    302
    pratap
    Aug 19, 2007
  3. pratap
    Replies:
    1
    Views:
    368
    Rolf Magnus
    Aug 20, 2007
  4. Kislay

    The sizeof operator : sizeof(++i)

    Kislay, Oct 18, 2007, in forum: C Programming
    Replies:
    10
    Views:
    707
    Peter Pichler
    Oct 19, 2007
  5. Nikolai Weibull

    sizeof(Class)/sizeof(Object)

    Nikolai Weibull, Dec 31, 2004, in forum: Ruby
    Replies:
    2
    Views:
    155
    Robert Klemme
    Dec 31, 2004
Loading...

Share This Page