3 Replies - 2263 Views - Last Post: 05 March 2010 - 08:18 AM Rate Topic: -----

#1 pshanoop  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 18
  • Joined: 02-March 10

help ! memory size of char....

Posted 02 March 2010 - 09:55 AM

hai guys i am new i joined today..
my problem is this
#include<iostream>
using namespace std;
class a
{
int a;
float b;
char d;
static float c;
};
int main()
{
cout<<sizeof(a);
return 0;
}


OUTPUT:
12

but here what i should get is (4+4+1)9 .but i am getting 12 (in Visual studio 2008 32bit)
.bcz of this i just tested in another compiler turbo c++ 16Bit - in that
it is coming 2+4+1 = 8
can anybody tell me why in vs2008 c++ i got 12 . i am check the size of char but it is coming 1 only ...

This post has been edited by JackOfAllTrades: 02 March 2010 - 09:59 AM
Reason for edit:: Added code tags. PLEASE!!! [code]...PUT YOUR CODE IN HERE!!!...[/code]


Is This A Good Question/Topic? 0
  • +

Replies To: help ! memory size of char....

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: help ! memory size of char....

Posted 02 March 2010 - 09:58 AM

This is actually a very helpful site: http://www.cprogramm...ass_object.html
Was This Post Helpful? 0
  • +
  • -

#3 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: help ! memory size of char....

Posted 02 March 2010 - 10:02 AM

View Postpshanoop, on 02 March 2010 - 03:55 PM, said:

hai guys i am new i joined today..
my problem is this
#include<iostream>
using namespace std;
class a
{
int a;
float b;
char d;
static float c;
};
int main()
{
cout<<sizeof(a);
return 0;
}

OUTPUT:
12

but here what i should get is (4+4+1)9 .but i am getting 12 (in Visual studio 2008 32bit)
.bcz of this i just tested in another compiler turbo c++ 16Bit - in that
it is coming 2+4+1 = 8
can anybody tell me why in vs2008 c++ i got 12 . i am check the size of char but it is coming 1 only ...


Yup, and they are both absolutely correct! The Microsoft C++ compiler places all variables on a 32-bit boundary. This is basically due to the fact that their compiler also runs on an itanium processor which will hardware exception if they are not. So the size of class a is actually 4+4+4, not 4+4+1 as you thought. The character occupies the first 8 bits of the last four and the remaining 24 bits are padding. To overcome this, you would use the pragma statement like this:

#include<iostream>
using namespace std;
#pragma pack(1)
class a
{
    int a;
    float b;
    char d;
    static float c;
};

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


Was This Post Helpful? 1
  • +
  • -

#4 pshanoop  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 18
  • Joined: 02-March 10

Re: help ! memory size of char....

Posted 05 March 2010 - 08:18 AM

View PostMartyn.Rae, on 02 March 2010 - 09:02 AM, said:

View Postpshanoop, on 02 March 2010 - 03:55 PM, said:

hai guys i am new i joined today..
my problem is this
#include<iostream>
using namespace std;
class a
{
int a;
float b;
char d;
static float c;
};
int main()
{
cout<<sizeof(a);
return 0;
}

OUTPUT:
12

but here what i should get is (4+4+1)9 .but i am getting 12 (in Visual studio 2008 32bit)
.bcz of this i just tested in another compiler turbo c++ 16Bit - in that
it is coming 2+4+1 = 8
can anybody tell me why in vs2008 c++ i got 12 . i am check the size of char but it is coming 1 only ...


Yup, and they are both absolutely correct! The Microsoft C++ compiler places all variables on a 32-bit boundary. This is basically due to the fact that their compiler also runs on an itanium processor which will hardware exception if they are not. So the size of class a is actually 4+4+4, not 4+4+1 as you thought. The character occupies the first 8 bits of the last four and the remaining 24 bits are padding. To overcome this, you would use the pragma statement like this:

#include<iostream>
using namespace std;
#pragma pack(1)
class a
{
    int a;
    float b;
    char d;
    static float c;
};

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




thanx guys
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1