Hi Guys,

Can you please enlighten me, why the following output is this way?
I thought A class size is the summation of the byte size of its member variables.

My machine is a 64 bit machine.

#include <iostream>
using namespace std;

class A{
public:
	char y;
	int x;
};

int main() {
	A a;

	cout << sizeof(a.x) << endl; //Prints 4, understandable, it is an int
	cout << sizeof(a.y) << endl; // Prints 1, ok, its a character
	cout << sizeof(a) << endl; //Prints 8 !!! why is this 8, has it actually alllocated 4 byte for the charecter and using just 1, then other 3 byte is wasted. Isn't it?

	return 0;
}

Why the above class size is showing 8?

Following code is priting 1 and 1 for the charecter:

class A{
public:
	char y;
};

int main() {
	A a;

	cout << sizeof(a) << endl; //Prints 1
	cout << sizeof(a.y) << endl; // Prints 1
	return 0;
}

Thanks in advance,

S.

Thanks for the reply.

What if I want to avoid this, lets say I have to create 10 millions of this object (10 million is too much?, mmm lets say I have unlimited memory?) then this padding will cost me 30 million of extra byte.

I have tried the rearranging of data types which is suggested on the link you posted, but all the time I am getting the same result which is class size 8 bytes.

Regards,
S.

May be I need to read that link more carefully. :)

Thanks for the reply.

What if I want to avoid this, lets say I have to create 10 millions of this object (10 million is too much?, mmm lets say I have unlimited memory?) then this padding will cost me 30 million of extra byte.

I have tried the rearranging of data types which is suggested on the link you posted, but all the time I am getting the same result which is class size 8 bytes.

Regards,
S.

http://publications.gbdirect.co.uk/c_book/chapter6/bitfields.html

Hi,

I am aware of the bit fields and I tried that first, but didnt work for me, may be I am missing something?

class A{
public:
	int y:8;
	char x:8;

};

int main() {
	A a;

	cout << sizeof(a) << endl; //Prints 4

	return 0;
}

Thanks.

Hmmm, I think I got it now, as its a 64 bit machine, it will always pad it to have a magnitude of 4 bytes for accessing the memory address more precisely and easily and handling the data faster, as it will take less calculation to pass data over the address bus, data bus and everything else i guess.

Thanks.

Yeah, you are correct. You will usually not gain anything at the end by trying to force the padding to not be done (if there is a way to do so). Usually, it will be much less efficient.

If memory is really a problem, you might consider clustering several objects into one. As follows:

#include <iostream>

struct A {
  int x[sizeof(int)];
  char c[sizeof(int)];
};

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

That way, each object of class A actually stores "sizeof(int)" number of records, and at the end, there won't be any padding because usually the sizeof(int) corresponds, on most systems to the alignment size. Then, to store the records, you can wrap a vector of objects of class A with a custom container that translates element access to sub-elements of the 'A' objects.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.