473,804 Members | 3,278 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

size and order of declaration

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
Dec 29 '06 #1
5 1830
Gary Wessle wrote:
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<doubleis likely to be exactly the same as
vector<int>.
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.
Dec 29 '06 #2
"Gary Wessle" <ph****@yahoo.c omwrote in message
news:m3******** ****@localhost. localdomain
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<i nt>) vs sizeof(vector<d ouble>). 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).

--
John Carson


Dec 29 '06 #3
Gianni Mariani <gi*******@mari ani.wswrites:
Gary Wessle wrote:
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.
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
Dec 29 '06 #4
"Gary Wessle" <ph****@yahoo.c omwrote in message
news:m3******** ****@localhost. localdomain
Gianni Mariani <gi*******@mari ani.wswrites:
>Gary Wessle wrote:
>>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.

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.

--
John Carson
Dec 29 '06 #5
"Gary Wessle" <ph****@yahoo.c omwrote in message
news:m3******** ****@localhost. localdomain...
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.
Dec 29 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
14533
by: Coder Droid | last post by:
I'm trying my first table-less site, and I've bumped my head up against a wall. I can't change the font size within a div. Real quick, my style sheet has: ------------------------------------- body { font: 8pt "face" } #nav {float: left; width: 144px; } #test { font: 12pt; font-weight: bold } -------------------------------------
16
697
by: Bhushit Joshipura | last post by:
This post contains one question and one proposal. A. May I know why order of evaluation of arguments is not specified in C/C++? I asked a question in comp.lang.c++ for the following possibility and because the languages do not specify the order of evaluation, doing so was an error. int B::f ( int i, int j = i + 1 ) { // j defaults to i + 1
10
2108
by: Chris Gordon-Smith | last post by:
I am currently revisiting the code of a C++ application and restructuring the code where appropriate to make it consistent with the overall logical design. As part of this, I am looking at the way .cpp files #include headers. My question is this: Is there a standard order in which heders should be included? I tend to #include STL headers first, and my own headers later, but I can't really think of a logical justification for this.
6
3172
by: Carl-Olof Almbladh | last post by:
Already in the 1st edition of the "White book", Kerigham and Ritchie states the "C is a general purpose language". However, without what is usually called "assumed size arrays" and built-in complex, C is not entirely suitable for numerical work. In particular, in order to have functions which can manipulate matrices whose dimensions are not determined at compile time one needs pointers of the form, say, double (*a) where n is not a...
7
5644
by: arkobose | last post by:
hey everyone! i have this little problem. consider the following declaration: char *array = {"wilson", "string of any size", "etc", "input"}; this is a common data structure used to store strings of any lengths into an array of pointers to char type variable. my problem is: given the declaration
6
5019
by: Laurent | last post by:
Hello, This is probably a dumb question, but I just would like to understand how the C# compiler computes the size of the managed structure or classes. I'm working on this class: public class MyClass {
0
1065
by: todd.freed | last post by:
Hello. I'll make this quick - its quite a conundrum I have the following markup <MyNS:MyControl runat="server" id="myId" CustomProperty1="value" CustomProperty1="value" CustomProperty1="value" > <Columns> <CustomColumnType1 value1="value" value2="value"/>
9
9420
by: joshc | last post by:
Hi, I have an array defined in one file with an intializer as follows: int arr = {0, 1, 2, 3}; I have a declaration of the array in another file as follows: extern int arr;
6
2159
by: Salman | last post by:
I want to know abt the size of the class using sizeof function. i have pasted 2 programs. Both gives different sizes of the class just by re- arranging the order of the private varibles. Tell me whats the reason behind it. Im pasting both the programs. I using VC++ 6 ---------------Program 1 ---------------------------- #include <iostream> using namespace std; class A {
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9582
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7621
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6854
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5652
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3821
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2993
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.