473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Struct vs. class as simple container

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 |
+-------------------------------------------------+
Jul 22 '05 #1
7 2948

"Wellu Mäkinen" <we***@n0spamN0rHTml.w3llu.0r9.invalid> wrote in message
news:sl******************@wellu.org...
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
Jul 22 '05 #2
On 2004-04-27, John Harrison <jo*************@hotmail.com> wrote:
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 |
+-------------------------------------------------+
Jul 22 '05 #3

"Wellu Mäkinen" <we***@n0spamN0rHTml.w3llu.0r9.invalid> wrote in message
news:sl******************@wellu.org...
On 2004-04-27, John Harrison <jo*************@hotmail.com> wrote:
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..


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

Jul 22 '05 #4
On 2004-04-27, John Harrison <jo*************@hotmail.com> wrote:
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 |
+-------------------------------------------------+
Jul 22 '05 #5

"Wellu Mäkinen" <we***@n0spamN0rHTml.w3llu.0r9.invalid> wrote in message
news:sl******************@wellu.org...

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.
Jul 22 '05 #6
On Tue, 27 Apr 2004 11:58:27 +0000 (UTC), Wellu Mäkinen
<we***@n0spamN0rHTml.w3llu.0r9.invalid> wrote:
On 2004-04-27, John Harrison <jo*************@hotmail.com> wrote:
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.


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
Jul 22 '05 #7
> 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.

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


Jul 22 '05 #8

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

Similar topics

2
by: Peter Steiner | last post by:
hi there! my problem: i need arrays of float-triples (math vector). i need these available in a flat float* array for batch processing (opengl). is an array of POD structs with three float...
6
by: Michael C | last post by:
Is it possible to use an ArrayList inside a struct? I keep running into a null reference exception when I try to Add to the ArrayList in the struct, and it won't let me initialize the ArrayList in...
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
5
by: Sunny | last post by:
Hi, I want to create a separate container for a bunch of constants used in different classes, like: internal class Constants { private Constants(){} public const string CONST1 = "1"; .....
12
by: jimmij | last post by:
Hi, Please look at the code bellow /*******************/ class ctab { private: static const unsigned n=48;
37
by: JohnGoogle | last post by:
Hi, Newbie question... After a recent article in VSJ I had a go at implementing a Fraction class to aid my understanding of operator overloading. After a previous message someone suggested...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
2
by: Ninereeds | last post by:
I'm messing around with using mixin-layers (look for papers by Yannis Smaragdakis and Don Batory) to define data structures. One issue is that nodes tend to have pointers to other nodes - the...
6
jwwicks
by: jwwicks | last post by:
Hello Experts, This is a student question and I know you could use a list or some other keyed container but they haven't been introduced to the class so they aren't free game so to speak. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.