Connecting Tech Pros Worldwide Help | Site Map

Struct vs. class as simple container

Wellu Mäkinen
Guest
 
Posts: n/a
#1: Jul 22 '05
Hi all,


let's say I have a need to store 4 int values in somekind
of a structure. I have atleast two options:

class A
{
public:
int a, b, c, b;
};

or

struct A
{
int a, b, c, d;
};

I know that when the compiler, in my case gcc 2.9.5,
compiles the class it generates constructor and destructor
automatically which makes the class a bit less efficient
than the struct. Note, that I don't need any getters or
setters. This whole question is totally theoretic.

Is there any advantage using structs e.g. can the compiler
optimize struct better than the class? Is this kind of a
struct same as four independent ints somewhere in the code?
I mean does the struct declaration itself reserve some
memory or could it make the program harder to optimize for
the compiler than just four ints?

Thanks for any ideas,

--
|||
(0-0)
+--------oOO--(_)--OOo----------------------------+
| Wellu Mäkinen www.wellu.0rg |
| |
| No tears please, it's a waste of good suffering |
+-------------------------------------------------+
John Harrison
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Struct vs. class as simple container



"Wellu Mäkinen" <wellu@n0spamN0rHTml.w3llu.0r9.invalid> wrote in message
news:slrnc8rprk.h8b.wellu@wellu.org...[color=blue]
> Hi all,
>
>
> let's say I have a need to store 4 int values in somekind
> of a structure. I have atleast two options:
>
> class A
> {
> public:
> int a, b, c, b;
> };
>
> or
>
> struct A
> {
> int a, b, c, d;
> };
>
> I know that when the compiler, in my case gcc 2.9.5,
> compiles the class it generates constructor and destructor
> automatically which makes the class a bit less efficient
> than the struct. Note, that I don't need any getters or
> setters. This whole question is totally theoretic.[/color]

Firstly it generates the constructor and destructor for a struct as well.
Structs are essentially no different from classes in C++. Secondly a good
compiler will realise that the constructor and destructor are no-ops and not
generate any code for them, though notionally at least they still exist.
[color=blue]
>
> Is there any advantage using structs e.g. can the compiler
> optimize struct better than the class?[/color]

No, the rules for structs are exactly the same as the rules for classes, the
sole exception being access which has no effect on code generation.
[color=blue]
> Is this kind of a
> struct same as four independent ints somewhere in the code?
> I mean does the struct declaration itself reserve some
> memory or could it make the program harder to optimize for
> the compiler than just four ints?[/color]

The struct declaration itself does not generate any code. I would say the
struct declaration gives the compiler more oppotunity to optimise not less,
since it gives the compiler more information about how those four ints will
be used (e.g. that they always go round in a group).

To me it sounds like you are concentrateing too much (far too much) on
micro-optmisation while ignoring more important aspects of C++ such as
writing meaningful code.

john


Wellu Mäkinen
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Struct vs. class as simple container


On 2004-04-27, John Harrison <john_andronicus@hotmail.com> wrote:[color=blue]
> To me it sounds like you are concentrateing too much (far too much) on
> micro-optmisation while ignoring more important aspects of C++ such as
> writing meaningful code.[/color]

As I said the question was totally theoretical, but thanks
for noticing my code was meaningless anyway..

--
|||
(0-0)
+--------oOO--(_)--OOo----------------------------+
| Wellu Mäkinen www.wellu.0rg |
| |
| No tears please, it's a waste of good suffering |
+-------------------------------------------------+
John Harrison
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Struct vs. class as simple container



"Wellu Mäkinen" <wellu@n0spamN0rHTml.w3llu.0r9.invalid> wrote in message
news:slrnc8s2j8.hcl.wellu@wellu.org...[color=blue]
> On 2004-04-27, John Harrison <john_andronicus@hotmail.com> wrote:[color=green]
> > To me it sounds like you are concentrateing too much (far too much) on
> > micro-optmisation while ignoring more important aspects of C++ such as
> > writing meaningful code.[/color]
>
> As I said the question was totally theoretical, but thanks
> for noticing my code was meaningless anyway..
>[/color]

I didn't mean your posted code was meaningless. I meant that writing code
whose structure reflects the task for which it is designed is far more
important than worrying about the efficiency of structs or ints. If four
integers have some logical connection in the task you are doing then the
should go into a struct (or class) for that reason alone. The relative
efficiency of structs, class and ints should not be a consideration.

john



Wellu Mäkinen
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Struct vs. class as simple container


On 2004-04-27, John Harrison <john_andronicus@hotmail.com> wrote:[color=blue]
> I didn't mean your posted code was meaningless. I meant that writing code
> whose structure reflects the task for which it is designed is far more
> important than worrying about the efficiency of structs or ints. If four
> integers have some logical connection in the task you are doing then the
> should go into a struct (or class) for that reason alone. The relative
> efficiency of structs, class and ints should not be a consideration.[/color]

Symbian OS for instance has 8 kilobytes of stack. If I
don't care of "relative efficiency" at all then my code
becomes useless. The four integers were only an
example. The struct or class could be far more
complicated.. Maybe I need to allocate thousands of them as
fast as possible. So the question was totally theoritacal
and its purpose was to find out which one, either class or
struct, to use when I need speed, small size etc.

But as replied it doesn't really matter which one to
use.

--
|||
(0-0)
+--------oOO--(_)--OOo----------------------------+
| Wellu Mäkinen www.wellu.0rg |
| |
| No tears please, it's a waste of good suffering |
+-------------------------------------------------+
jeffc
Guest
 
Posts: n/a
#6: Jul 22 '05

re: Struct vs. class as simple container



"Wellu Mäkinen" <wellu@n0spamN0rHTml.w3llu.0r9.invalid> wrote in message
news:slrnc8s8b8.hff.wellu@wellu.org...[color=blue]
>
> Symbian OS for instance has 8 kilobytes of stack. If I
> don't care of "relative efficiency" at all then my code
> becomes useless. The four integers were only an
> example. The struct or class could be far more
> complicated..[/color]

John was oversimplifying, and of course the efficiency of your code matters.
In this case though, there is no difference between a struct and class.
Having said that, I'm not aware of any inefficiency that would be caused by
using a struct instead of 4 raw integers, but that would depend on your
compiler. The best way to test would be to create huge numbers of them and
check space and time usage in a real program.


tom_usenet
Guest
 
Posts: n/a
#7: Jul 22 '05

re: Struct vs. class as simple container


On Tue, 27 Apr 2004 11:58:27 +0000 (UTC), Wellu Mäkinen
<wellu@n0spamN0rHTml.w3llu.0r9.invalid> wrote:
[color=blue]
>On 2004-04-27, John Harrison <john_andronicus@hotmail.com> wrote:[color=green]
>> I didn't mean your posted code was meaningless. I meant that writing code
>> whose structure reflects the task for which it is designed is far more
>> important than worrying about the efficiency of structs or ints. If four
>> integers have some logical connection in the task you are doing then the
>> should go into a struct (or class) for that reason alone. The relative
>> efficiency of structs, class and ints should not be a consideration.[/color]
>
> Symbian OS for instance has 8 kilobytes of stack. If I
> don't care of "relative efficiency" at all then my code
> becomes useless. The four integers were only an
> example. The struct or class could be far more
> complicated.. Maybe I need to allocate thousands of them as
> fast as possible. So the question was totally theoritacal
> and its purpose was to find out which one, either class or
> struct, to use when I need speed, small size etc.
>
> But as replied it doesn't really matter which one to
> use.[/color]

A really good compiler will treat:

A a;

as equivalent to

int a, b, c, d;

for code generation purposes for both the class and struct version.
e.g. if any of a, b, c or d aren't used, they will be removed
entirely. They might also be arbitrarily reordered if this produces
better code. Any compiler with a "small object optimizer" should be
able to do that. Kai C++ was famous for it, and I hope other compilers
have caught up (judging recent runs of C++ abstraction penalty tests,
I think they probably have).

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Peter van Merkerk
Guest
 
Posts: n/a
#8: Jul 22 '05

re: Struct vs. class as simple container


> Symbian OS for instance has 8 kilobytes of stack. If I[color=blue]
> don't care of "relative efficiency" at all then my code
> becomes useless. The four integers were only an
> example. The struct or class could be far more
> complicated.. Maybe I need to allocate thousands of them as
> fast as possible. So the question was totally theoritacal
> and its purpose was to find out which one, either class or
> struct, to use when I need speed, small size etc.
>
> But as replied it doesn't really matter which one to
> use.[/color]

When every byte and/or cycle counts it usually a good idea to look at the
assembly output of the compiler. This way you get a better idea what kind
of code the compiler generates, and what it can optimize away and what not.
With this knowledge you can avoid futile optimization attempts, and avoid
constructs that cause code bloat.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl




Closed Thread