The size of a Class ?

This is a discussion on The size of a Class ? within the C++ Programming forums, part of the General Programming Boards category; Code: class A{ public: int x; void foo() const{ cout<<"A Class"<<endl; } }; int main(){ cout<<sizeof(A)<<"Bytes"; return 0; } The ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Exclamation The size of a Class ?

    Code:
    class A{
    public:
    	int x;
    	void foo() const{
    		cout<<"A Class"<<endl;
    	}
    };
    
    int main(){
    
    cout<<sizeof(A)<<"Bytes";
    return 0;
    
    }
    The result is "4 Bytes". Why is that? Where is the space for the foo() function? Thank you for your comments.

  2. #2
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    I suspect it is just checking the size based on all the data types you have in the class. Since int is most likely 32-bit, it comes out at 4 bytes.

  3. #3
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    The size of a class is the sum of the sizes of its data members. The methods only exist once, they aren't duplicated for each object.

    A class method is really a normal function that takes a hidden parameter, the address of the object you invoked it on ('this').

    For example, the signature of A::foo is effectively this:

    Code:
    void A::foo(A *this) const
    {
        //stuff
    }

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Quote Originally Posted by AverageSoftware View Post
    The size of a class is the sum of the sizes of its data members.
    No, it's not. Obviously, it is at least that large, but it could be larger for many reasons (padding, hidden implementation helpers such as vptrs, ...)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Quote Originally Posted by CornedBee View Post
    No, it's not. Obviously, it is at least that large, but it could be larger for many reasons (padding, hidden implementation helpers such as vptrs, ...)
    True, but since the rest of that stuff is implementation dependent, it really can't be relied upon.

    Perhaps "at least a large" would have been the better way to phrase it.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > The size of a class is the sum of the sizes of its data members.
    I believe that any class, even one with no data members, is required to have a size of at least 1 byte. Try
    Code:
    #include <iostream>
    
    class A {};
    
    int main() {
      std::cout << sizeof(A) << std::endl;
    }

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Yes, that's also true, except for the special case of the empty base optimization, where a specific instance of a class may have size 0. (But there is no way to access this subobject in a way you can pass it to sizeof.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM
  5. Replies: 11
    Last Post: 03-25-2003, 04:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21