 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Joona I Palaste Guest
|
Posted: Wed Jul 30, 2003 6:10 am Post subject: Re: A question about variables declaration in C++ |
|
|
Dog <markyu (AT) seed (DOT) net.tw> scribbled the following
on comp.lang.c:
| Quote: | Hi,
A question about variables declaration.
There're 3 variables declared in a class.
|
"Class"? What is that?
| Quote: | Which one is better? Why?
(A)
{
int i1;
double d;
int i2;
}
(B)
{
int i1;
int i2;
double d;
}
(C)
{
double d;
int i1;
int i2;
}
|
Use any of those. They're equivalent as far as the C and C++
standards are concerned.
If you want specific help for your own implementation, ask in a
newsgroup dedicated to your own implementation.
--
/-- Joona Palaste (palaste (AT) cc (DOT) helsinki.fi) ---------------------------
| Quote: | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
http://www.helsinki.fi/~palaste W++ B OP+ |
----------------------------------------- Finland rules! ------------/ |
"Normal is what everyone else is, and you're not."
- Dr. Tolian Soran
|
|
| Back to top |
|
 |
Greg P. Guest
|
Posted: Wed Jul 30, 2003 6:43 am Post subject: Re: A question about variables declaration in C++ |
|
|
If you are concerned about packing order (and optimizations),it all depends
on your compiler and memory model. I would say that "shortest to longest"
would be better, but I may be wrong as I do not know what you are using to
compile and run the code with.
"Dog" <markyu (AT) seed (DOT) net.tw> wrote
| Quote: | Hi,
A question about variables declaration.
There're 3 variables declared in a class.
Which one is better? Why?
(A)
{
int i1;
double d;
int i2;
}
(B)
{
int i1;
int i2;
double d;
}
(C)
{
double d;
int i1;
int i2;
}
I've no idea about this.
Could you please provide me some hints.
Thanks.
|
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Wed Jul 30, 2003 9:21 am Post subject: Re: A question about variables declaration in C++ |
|
|
Greg P. wrote:
| Quote: | If you are concerned about packing order (and optimizations),it all
depends on your compiler and memory model. I would say that "shortest
to longest" would be better,
|
I would expect the opposite to be true.
| Quote: | but I may be wrong as I do not know what you are using to compile and
run the code with.
|
|
|
| Back to top |
|
 |
Chris ( Val ) Guest
|
Posted: Wed Jul 30, 2003 11:55 am Post subject: Re: A question about variables declaration in C++ |
|
|
"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote
| Quote: |
"Dog" <markyu (AT) seed (DOT) net.tw> wrote in message
news:bg7n3d$irl$1 (AT) news (DOT) seed.net.tw...
Hi,
A question about variables declaration.
There're 3 variables declared in a class.
Which one is better? Why?
(A)
{
int i1;
double d;
int i2;
}
(B)
{
int i1;
int i2;
double d;
}
(C)
{
double d;
int i1;
int i2;
}
I've no idea about this.
Could you please provide me some hints.
Thanks.
They are all fine, its a stupid question, possibly trying to say something
about the packing of member variables in a class. But that is a compiler
specific topic, so the correct answer for one compiler will be the wrong
answer for another. In any case it makes very little difference.
|
Hi John.
I value your contributions in c.l.c++, however, I must disagree
with your strong response. There really are no stupid questions
in C++ (especially for newbie's).
However, I don't believe the OP even mentioned anything about
a performance issue, that everyone seems to have assumed to be
the case of the question.
There *may* be some implementation dependant performance issues,
however, no one has yet to mention that there *is* a difference
with the order data members are declared.
That is, they are *initialised* in a specific order, and that
*does* matter.
For example:
# include <iostream>
# include <ostream>
class Base
{
private:
int First;
int Second;
public:
Base( const int& N ) : Second( N ), First( Second )
{
std::cout << First << std::endl;
}
};
int main()
{
Base B( 10 );
return 0;
}
It is not the order that they are initialised in the
initialiser list that matters here, it is the order
in which these data members are declared.
As you can see by the output, you can create a nasty
bug for yourself, that is difficult to track down.
Cheers.
Chris Val
|
|
| Back to top |
|
 |
Greg Comeau Guest
|
Posted: Wed Jul 30, 2003 12:51 pm Post subject: Re: A question about variables declaration in C++ |
|
|
In article <bg7n3d$irl$1 (AT) news (DOT) seed.net.tw>, Dog <markyu (AT) seed (DOT) net.tw> wrote:
| Quote: | Hi,
A question about variables declaration.
There're 3 variables declared in a class.
Which one is better? Why?
(A)
{
int i1;
double d;
int i2;
}
(B)
{
int i1;
int i2;
double d;
}
(C)
{
double d;
int i1;
int i2;
}
I've no idea about this.
Could you please provide me some hints.
|
There may be some mild alignment or struct packing considerations
you may have (which would favor C since a double is usually
larger than an int, and with a most severe alightment constraint),
but generally speaking, no way is better.
BTW, this is not a C question, so you shouldn't post it
to comp.lang.c
--
Greg Comeau/ 4.3.0.1: FULL CORE LANGUAGE, INCLUDING TC1
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
|
|
| Back to top |
|
 |
Howard Guest
|
Posted: Wed Jul 30, 2003 2:26 pm Post subject: Re: A question about variables declaration in C++ |
|
|
(Sounds like another homework question to me!)
"Chris ( Val )" <chrisval (AT) bigpond (DOT) com.au> wrote
| Quote: | However, I don't believe the OP even mentioned anything about
a performance issue, that everyone seems to have assumed to be
the case of the question.
|
"Packing" is not performance-related, but space-related. Re-arranging the
variables may, in certain compiler and operating system combinations, allow
less space to be used, due to alignment issues. (Then again, it may not.)
Performance is a speed issue, and may or may not be affected by such a small
difference in size as could possibly occur here.
| Quote: |
There *may* be some implementation dependant performance issues,
however, no one has yet to mention that there *is* a difference
with the order data members are declared.
That is, they are *initialised* in a specific order, and that
*does* matter.
|
But the example is not using user-defined types, but simple built-in types,
and there is initialization taking place, simply the allocation of enough
memory space to hold them. So order does not matter here.
Perhaps the OP was told in his class that the teacher wants their variables
declared in a certain manner? I can think of no other reason why it would
matter the order in which these *particular* variables are declared could
possibly matter. Certainly, they do not need to be in alphabetic order, nor
is there any particular reason to group int's together as opposed to having
an int then a double then another int. (But the variable names themselves
ought to be more descriptive of their function...in a real world case at
least.)
If this actual curiosity on the part of the OP, then the answer is that it
doesn't really matter. If it's a homework question, then I agree that it IS
a stupid question! :-)
-Howard
|
|
| Back to top |
|
 |
Ben Guest
|
Posted: Wed Jul 30, 2003 2:35 pm Post subject: Re: A question about variables declaration in C++ |
|
|
Chris ( Val ) wrote:
| Quote: | That is, they are *initialised* in a specific order, and that
*does* matter.
|
But only the bare bones was given in the question so the below can't be
what was being got at I would have thought.
regards,
Ben
| Quote: |
For example:
# include
# include
class Base
{
private:
int First;
int Second;
public:
Base( const int& N ) : Second( N ), First( Second )
{
std::cout << First << std::endl;
}
};
int main()
{
Base B( 10 );
return 0;
}
It is not the order that they are initialised in the
initialiser list that matters here, it is the order
in which these data members are declared.
As you can see by the output, you can create a nasty
bug for yourself, that is difficult to track down.
Cheers.
Chris Val
|
--
BTW. I can be contacted at Username:newsgroup4.replies.benaltw
Domain:xoxy.net
|
|
| Back to top |
|
 |
Dan Pop Guest
|
Posted: Wed Jul 30, 2003 3:14 pm Post subject: Re: A question about variables declaration in C++ |
|
|
In <bg8f0a$cko$1 (AT) panix5 (DOT) panix.com> [email]comeau (AT) panix (DOT) com[/email] (Greg Comeau) writes:
| Quote: | In article <bg7n3d$irl$1 (AT) news (DOT) seed.net.tw>, Dog <markyu (AT) seed (DOT) net.tw> wrote:
Hi,
A question about variables declaration.
There're 3 variables declared in a class.
Which one is better? Why?
(A)
{
int i1;
double d;
int i2;
}
(B)
{
int i1;
int i2;
double d;
}
(C)
{
double d;
int i1;
int i2;
}
I've no idea about this.
Could you please provide me some hints.
There may be some mild alignment or struct packing considerations
you may have (which would favor C since a double is usually
larger than an int, and with a most severe alightment constraint),
but generally speaking, no way is better.
BTW, this is not a C question, so you shouldn't post it
to comp.lang.c
|
Substitute "class" by "structure" and it becomes a C question :-)
The issue is exactly the same in both languages. It is preferrable to
put the wider types at the beginning, but it's not always obvious which
type is wider. An educated guess would favour the following order for
modern implementations:
long double
double
long
pointer
float
int
short
char
long long (if supported) should be either right above or right below
double.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: [email]Dan.Pop (AT) ifh (DOT) de[/email]
|
|
| Back to top |
|
 |
Greg Comeau Guest
|
Posted: Wed Jul 30, 2003 7:06 pm Post subject: Re: A question about variables declaration in C++ |
|
|
In article <bg8nde$hnn$2 (AT) sunnews (DOT) cern.ch>, Dan Pop <Dan.Pop (AT) cern (DOT) ch> wrote:
| Quote: | In <bg8f0a$cko$1 (AT) panix5 (DOT) panix.com> [email]comeau (AT) panix (DOT) com[/email] (Greg Comeau) writes:
In article <bg7n3d$irl$1 (AT) news (DOT) seed.net.tw>, Dog <markyu (AT) seed (DOT) net.tw> wrote:
A question about variables declaration.
There're 3 variables declared in a class.
Which one is better? Why?
(A)
{
int i1;
double d;
int i2;
}
(B)
{
int i1;
int i2;
double d;
}
(C)
{
double d;
int i1;
int i2;
}
I've no idea about this.
Could you please provide me some hints.
There may be some mild alignment or struct packing considerations
you may have (which would favor C since a double is usually
larger than an int, and with a most severe alightment constraint),
but generally speaking, no way is better.
BTW, this is not a C question, so you shouldn't post it
to comp.lang.c
Substitute "class" by "structure" and it becomes a C question
|
Maybe. I'll play for now though ....
| Quote: | The issue is exactly the same in both languages. It is preferrable to
put the wider types at the beginning, but it's not always obvious which
type is wider. An educated guess would favour the following order for
modern implementations:
long double
double
long
pointer
float
int
short
char
long long (if supported) should be either right above or right below
double.
|
.... so in that case (this being a C and C++ question), don't
forget _Bool/bool, enum's, the ever lovely _Complex/_Imaginary
and the always dashing bitfield.
--
Greg Comeau/ 4.3.0.1: FULL CORE LANGUAGE, INCLUDING TC1
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Thu Jul 31, 2003 6:23 am Post subject: Re: A question about variables declaration in C++ |
|
|
| Quote: |
Hi John.
I value your contributions in c.l.c++, however, I must disagree
with your strong response. There really are no stupid questions
in C++ (especially for newbie's).
|
I certainly wasn't trying to suggest that the OP was being stupid, apologies
if I gave that impression. I thought it fairly obvious that the OP was
repeating a question that s/he had been asked.
| Quote: |
However, I don't believe the OP even mentioned anything about
a performance issue, that everyone seems to have assumed to be
the case of the question.
There *may* be some implementation dependant performance issues,
however, no one has yet to mention that there *is* a difference
with the order data members are declared.
That is, they are *initialised* in a specific order, and that
*does* matter.
|
It does, but I doubt this was the intention behind the question in this
case. If it was then the class quoted should have included some constructors
to illustrated the difference. This is off topic in comp.lang.c as well.
[snip]
john
|
|
| Back to top |
|
 |
Chris ( Val ) Guest
|
Posted: Thu Jul 31, 2003 11:39 am Post subject: Re: A question about variables declaration in C++ |
|
|
"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote
| Quote: |
Hi John.
I value your contributions in c.l.c++, however, I must disagree
with your strong response. There really are no stupid questions
in C++ (especially for newbie's).
I certainly wasn't trying to suggest that the OP was being stupid, apologies
if I gave that impression. I thought it fairly obvious that the OP was
repeating a question that s/he had been asked.
|
Hi John.
I didn't think you were implying that the OP was stupid at all, it
was the question I was referring too .
| Quote: | However, I don't believe the OP even mentioned anything about
a performance issue, that everyone seems to have assumed to be
the case of the question.
There *may* be some implementation dependant performance issues,
however, no one has yet to mention that there *is* a difference
with the order data members are declared.
That is, they are *initialised* in a specific order, and that
*does* matter.
It does, but I doubt this was the intention behind the question in this
case. If it was then the class quoted should have included some constructors
to illustrated the difference. This is off topic in comp.lang.c as well.
|
My apologies, I didn't realise this was posted to c.l.c.
I have removed c.l.c from this post.
You might doubt it <g>, but if we use 'Occams Razor'...er..um...
what is most likely the teacher is asking a newbie ? 'packing'
vs 'what affect the order of member declaration may have' ?
I would choose the latter, if any .
Cheers.
Chris Val
|
|
| Back to top |
|
 |
B. v Ingen Schenau Guest
|
Posted: Thu Jul 31, 2003 7:39 pm Post subject: Re: A question about variables declaration in C++ |
|
|
Chris ( Val ) wrote:
| Quote: |
"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote in message
news:bgackj$mrn2a$1 (AT) ID-196037 (DOT) news.uni-berlin.de...
|
| > That is, they are *initialised* in a specific order, and that
| > *does* matter.
|
|
| It does, but I doubt this was the intention behind the question in this
| case. If it was then the class quoted should have included some
| constructors to illustrated the difference. This is off topic in
| comp.lang.c as well.
My apologies, I didn't realise this was posted to c.l.c.
I have removed c.l.c from this post.
You might doubt it <g>, but if we use 'Occams Razor'...er..um...
what is most likely the teacher is asking a newbie ? 'packing'
vs 'what affect the order of member declaration may have' ?
|
That depends entirely on the course that is being taught.
If it is assumed that the students will write programs that have to be
memory efficient (think of microchip programming), then the packing of a
structure becomes very important.
And also, i dont think that newbies are bitten that often by the
initialisation order of class members in C++.
| Quote: |
I would choose the latter, if any .
|
And I would choose the former, but I would explain the subject before
asking a question like the OP posted.
Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.snurse-l.org/acllc-c++/faq.html
a.c.l.l.c-c++ FAQ mirror: http://nullptr.merseine.nu:8080/acllcc++.html
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|