CFanatic

Go Back   CFanatic > Programming > C Programming

Join CFanatic Forum Now

sizeof for a class topic posted under C Programming which is a part of Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 06-04-2007, 11:21 AM
coderzone's Avatar
Senior Member
 
Join Date: May 2007
Posts: 273
Thanks: 16
Thanked 12 Times in 12 Posts
coderzone is on a distinguished road
| More
Angry sizeof for a class

Code:
Class A
{
  int a;
  float b;
};
Now what will be the value of sizeof(A);
Reply With Quote
  #2  
Old 06-05-2007, 08:48 PM
Junior Member
 
Join Date: Jun 2007
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
tiger12506 is on a distinguished road
| More
Re: sizeof for a class

Check it out for yourself.
"class" is not a keyword in C. A struct is basically the same thing. I do know that "class" in c++ is just a "struct" with a default of private...

Code:
#include <stdio.h>

struct A {
  int a;
  float b;
};

int main() {
  printf("sizeof(int) = %d\n",sizeof(int));
  printf("sizeof(float) = %d\n",sizeof(float));
  printf("sizeof(struct A) = %d\n",sizeof(struct A));
}
I get:

sizeof(int) = 4
sizeof(float) = 4
sizeof(struct A) = 8

But those values could be different on different platforms. The trick is that usually the size of the items in the struct add up to the size of the struct itself
Reply With Quote
  #3  
Old 06-05-2007, 10:29 PM
coderzone's Avatar
Senior Member
 
Join Date: May 2007
Posts: 273
Thanks: 16
Thanked 12 Times in 12 Posts
coderzone is on a distinguished road
| More
Re: sizeof for a class

I just wanted to post something else. Something like

Code:
struct A
{
  int a;
  double b;
};
Now check what will be the size of and you will see the difference and why I have put it here.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
get Class johny10151981 Win32 Programming 1 10-07-2008 03:58 AM
Simple class not working psppb C++ Programming 3 01-08-2008 10:24 PM
How can you get the current class name without using StackTrace? codeunlock C# Programming 0 04-29-2007 12:42 AM
Preprocessor and sizeof() Amigo C Programming 0 09-30-2006 06:23 AM
Class Project aaholland C++ Programming 0 09-11-2006 11:40 PM

 

Advertisement

CFanatic Search
Custom Search

Advertisement

All times are GMT -5. The time now is 01:57 AM.



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.
Cfanatic.com is a premier member of the IDG TechNetwork. For advertising opportunities contact here
Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board