Struct vs. class as simple container

  • Thread starter Wellu =?iso-8859-1?Q?M=E4kinen?=
  • Start date
W

Wellu =?iso-8859-1?Q?M=E4kinen?=

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 |
+-------------------------------------------------+
 
J

John Harrison

Wellu Mäkinen said:
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.

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.
Is there any advantage using structs e.g. can the compiler
optimize struct better than the class?

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.
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?

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
 
W

Wellu =?iso-8859-1?Q?M=E4kinen?=

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.

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 |
+-------------------------------------------------+
 
J

John Harrison

Wellu Mäkinen said:
As I said the question was totally theoretical, but thanks
for noticing my code was meaningless anyway..

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
 
W

Wellu =?iso-8859-1?Q?M=E4kinen?=

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.

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 |
+-------------------------------------------------+
 
J

jeffc

Wellu Mäkinen said:
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..

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.
 
T

tom_usenet

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.

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
 
P

Peter van Merkerk

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.

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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top