size and order of declaration

G

Gary Wessle

Hi

I read some where about the order variables are declared in a class
may help space optimization.
find the size of bits a variable takes and declare them from biggest
to the lowest.
so in my system that would be.

double
int
string
char
bool

is the principle correct? if so, then at the top of the list should be
things like
vector<double>
vector<int>
....
and same for map(s) and so on...
but what about function declaration?
where do then fit, what is the size of a function declaration?

thanks
 
G

Gianni Mariani

Gary said:
Hi

I read some where about the order variables are declared in a class
may help space optimization.
find the size of bits a variable takes and declare them from biggest
to the lowest.
so in my system that would be.

double
int
string
char
bool

bool is often implemented as an int. So it should go next to int.
is the principle correct?

No, not quite. It's the alignment of your type that is important, not
the size.


.... if so, then at the top of the list should be
things like
vector<double>
vector<int>
...
and same for map(s) and so on...
but what about function declaration?

The alignment of a vector<double> is likely to be exactly the same as
vector said:
where do then fit, what is the size of a function declaration?

I don't think it will ever matter to you what the alignment of a
function is.
 
J

John Carson

Gary Wessle said:
Hi

I read some where about the order variables are declared in a class
may help space optimization.
find the size of bits a variable takes and declare them from biggest
to the lowest.
so in my system that would be.

double
int
string
char
bool

is the principle correct?

Usually, but it depends on the alignment requirements of your system (which
you may be able to alter with a compiler switch). Almost certainly, it will
do no harm space-wise to organise your code this way.
if so, then at the top of the list should be
things like
vector<double>
vector<int>

That depends on sizeof(vector<int>) vs sizeof(vector<double>). On my system
they are the same size, notwithstanding that double is twice as large as
int. This is because storage for vectors is allocated on the heap, not in
the class object, which only stores a pointer.
...
and same for map(s) and so on...
but what about function declaration?
where do then fit, what is the size of a function declaration?

Function declarations/definitions do not add to the size of an object
(except that if you have at least one virtual function, then your class
object will have a vtable pointer --- or at least that is how it is normally
implemented). Functions exist at the level of the class, not at the level of
the individual object.

I would quote Stroustrup on this whole issue.

"However, it is usually best to order members for readability and sort them
by size only if there is a demonstrated need to optimize." (TCPL, Special
3rd ed., p. 103).
 
G

Gary Wessle

Gianni Mariani said:
bool is often implemented as an int. So it should go next to int.


No, not quite. It's the alignment of your type that is important, not
the size.

Stroustrup 3rd ed. top of p. 103
"You can minimize wasted space by simply ordering members by size
(largest member first)."

is this statement correct only inside the structure and not for
general use?

thanks
 
J

John Carson

Gary Wessle said:
Stroustrup 3rd ed. top of p. 103
"You can minimize wasted space by simply ordering members by size
(largest member first)."

is this statement correct only inside the structure and not for
general use?

thanks

It is not clear what you mean by "general use". However, an implementation
is only required to store members in the order in which they are declared
when there are no intervening access specifiers (section 9.2/12 of the
Standard). Thus given:

class Test
{
private:
double d;
int i;
public:
bool b;
};

d must precede i, but the access specifier that occurs between d and i, on
the one hand, and b on the other hand, means that the placement of b
relative to d and i could be different from the order of declaration.
Specifically, b might come first.
 
J

Jim Langston

Gary Wessle said:
Hi

I read some where about the order variables are declared in a class
may help space optimization.
find the size of bits a variable takes and declare them from biggest
to the lowest.
so in my system that would be.

double
int
string
char
bool

is the principle correct? if so, then at the top of the list should be
things like
vector<double>
vector<int>
...
and same for map(s) and so on...
but what about function declaration?
where do then fit, what is the size of a function declaration?

thanks

Yes and no. Consider a structure (or class) such as:

class MyClass
{
char Char1;
int Int1;
char Char2;
};

What is the size of that class?

(on my system with sizeof int being 4)
Well, the compiler will allocate one byte for the char, then skip 3 bytes so
the int is aligned properly, then 4 bytes for the int, then 1 byte for the
second char, then skip 3 more bytes so an array of this class will be
aligned properly. So 12 bytes, when only 6 are actually used.

So we can arrange it and make the largest first so it would become:

class MyClass
{
int Int1;
char Char1;
char Char2;
};

What's the size of this class?

4 bytes for the int, 1 byte for the first char, 1 byte for the 2nd char,
skip 2 bytes for alignment. 8 bytes total.

But we get the same result with:

class MyClass
{
char Char1;
char Char2;
int Int1;
};

1 byte, 1 byte, skip 2, 4 bytes.

Generally, the compler will align a type on it's size. If a variable is 4
bytes, it will align it every 4 bytes. 2 bytes, 2, etc... I said
generally, there may be exceptions with OS/compilers/CPUs, etc...

As long as you understand what's going on it should be relatively easy to
figure out how to get rid of wasted space. I.E. This is extremly wasteful:

class MyClass
{
char Type1;
int Val1;
char Type2;
int Val2;
char Type3;
int Val3;
};

24 bytes.

A little rearranging:

class MyClass
{
char Type1;
char Type2;
char Type3;
int Val1;
int Val2;
int Val3;
};

16 bytes. I could of put the ints first.

However, I tend to arrange my class variables on their usage, grouping like
with like. Just use a little common sense and things should be fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top