473,748 Members | 7,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

storing pointer vs storing object

Hi,
all of the containers in STL stores object itself (thus copy
contsructabilit y & assignability is needed), while NTL or boost
ptr_container stores pointer to the object in the container (either
exclusively owns, or just stores).
Now, my question is for a general guideline when to use which one?
What I understand,
1) polymorphic objects need ptr_container.
2) non copy constructable, non assignable objects need ptr_container.

Other than that, any guideline is there? I am specially interested for
small devices (handheld, pda etc) with limited memory, where gaining
performance is important.
To be specific, can it be a guideline like, lots of small objects =>
STL container (faster due to cache effect)., a few large objects (like
GUI widgets) , use ptr_container. So, why there should be two different
types of container, and when one or the other would be used?

thanks
abir

Oct 11 '06 #1
11 2366
toton wrote:
>
Hi,
all of the containers in STL stores object itself (thus copy
contsructabilit y & assignability is needed), while NTL or boost
ptr_container stores pointer to the object in the container (either
exclusively owns, or just stores).
Now, my question is for a general guideline when to use which one?
What I understand,
1) polymorphic objects need ptr_container.
2) non copy constructable, non assignable objects need ptr_container.

Other than that, any guideline is there? I am specially interested for
small devices (handheld, pda etc) with limited memory, where gaining
performance is important.
To be specific, can it be a guideline like, lots of small objects =>
STL container (faster due to cache effect)., a few large objects (like
GUI widgets) , use ptr_container. So, why there should be two different
types of container, and when one or the other would be used?

thanks
abir
Hi,

Storing a copy of the original data is in my opinion only usefull when
you need to have very fast access to little values, as you mentionned.
For exemple in a very demanding loop, storuing object copies might be a
good idea, if those are often used. Because accessing to pointed data
is always slower than accessing directly to the copy.

A rule of thumb I'd say :

If your concern is memory optimization with reasonable speed ->
pointers/reference

If your concern is speed AND you have sufficient memory AND you store
little objects -copy ( because the copy of big objects can be a
little slow )

Cheers,
K

Oct 11 '06 #2

KiLVaiDeN wrote:
toton wrote:

Hi,
all of the containers in STL stores object itself (thus copy
contsructabilit y & assignability is needed), while NTL or boost
ptr_container stores pointer to the object in the container (either
exclusively owns, or just stores).
Now, my question is for a general guideline when to use which one?
What I understand,
1) polymorphic objects need ptr_container.
2) non copy constructable, non assignable objects need ptr_container.

Other than that, any guideline is there? I am specially interested for
small devices (handheld, pda etc) with limited memory, where gaining
performance is important.
To be specific, can it be a guideline like, lots of small objects =>
STL container (faster due to cache effect)., a few large objects (like
GUI widgets) , use ptr_container. So, why there should be two different
types of container, and when one or the other would be used?

thanks
abir

Hi,

Storing a copy of the original data is in my opinion only usefull when
you need to have very fast access to little values, as you mentionned.
For exemple in a very demanding loop, storuing object copies might be a
good idea, if those are often used. Because accessing to pointed data
is always slower than accessing directly to the copy.

A rule of thumb I'd say :

If your concern is memory optimization with reasonable speed ->
pointers/reference

If your concern is speed AND you have sufficient memory AND you store
little objects -copy ( because the copy of big objects can be a
little slow )
Unless and until proven otherwise, both those concerns are secondary
to:

"Which option produces the most clearly human-readable code" and
"Which option enables you to get to the point where that code is
written, teted and error-free more quickly"

So, to the OP: consider the options in that context and choose
whichever answers those questions best. If you find, for example, that
it takes longer (including testing and debugging time) to write correct
code using containers of pointers, and that code is less clear to a
human reader, then use containers of objects.

Premature optimisation is the root of all evil and all that.

Gavin Deane

Oct 11 '06 #3
Gavin Deane wrote:
>
Unless and until proven otherwise, both those concerns are secondary
to:

"Which option produces the most clearly human-readable code" and
"Which option enables you to get to the point where that code is
written, teted and error-free more quickly"

So, to the OP: consider the options in that context and choose
whichever answers those questions best. If you find, for example, that
it takes longer (including testing and debugging time) to write correct
code using containers of pointers, and that code is less clear to a
human reader, then use containers of objects.

Premature optimisation is the root of all evil and all that.

Gavin Deane
I totally agree with your point, in a normal environnement, but he's
working for small devices, therefore it's not only a matter of
Optimization, but more a matter of good practices for those targeted
platforms.

The considerations you make are very valuable though; The best is to
test both of the scenarios, see how it's managed in your targeted
device, and then sticking to one or another; Tell me if you agree :)

Cheers,
K

Oct 11 '06 #4

KiLVaiDeN wrote:
Gavin Deane wrote:

Unless and until proven otherwise, both those concerns are secondary
to:

"Which option produces the most clearly human-readable code" and
"Which option enables you to get to the point where that code is
written, teted and error-free more quickly"

So, to the OP: consider the options in that context and choose
whichever answers those questions best. If you find, for example, that
it takes longer (including testing and debugging time) to write correct
code using containers of pointers, and that code is less clear to a
human reader, then use containers of objects.

Premature optimisation is the root of all evil and all that.

Gavin Deane

I totally agree with your point, in a normal environnement, but he's
working for small devices, therefore it's not only a matter of
Optimization, but more a matter of good practices for those targeted
platforms.
He did say he was working with small devices. I am not sure that good
practice differs very much though. My comments boil down to "Unless and
until proven necessary, don't optimise at the expense of clear, concise
code". It may happen to be that in the OP's case, such optimisation is
proven necessary more often than not. That would not contradict my
comments.
The considerations you make are very valuable though; The best is to
test both of the scenarios, see how it's managed in your targeted
device, and then sticking to one or another; Tell me if you agree :)
Absolutely. And with increasing experience of your development tools
and target environment, you will become better able to correctly
predict what will and will not be a problem without necessarily testing
every last detail.

What does not work however, is to guess, or to work to arbitrary
guidelines not based on evidence, which is the trap it looked like the
OP might be wandering towards.

Gavin Deane

Oct 11 '06 #5
toton wrote:
Hi,
all of the containers in STL stores object itself (thus copy
contsructabilit y & assignability is needed),
False. The STL containers can be used over pointers, e.g.:

std::set<Type *>

instead of

std::set<Type>

And of course they can be used over smart pointers also

std::set<SmartP ointer<Type.

The Type might not be copy-constructable or assignable, but the smart
pointer is.

Oct 11 '06 #6
Gavin Deane wrote:
Unless and until proven otherwise, both those concerns are secondary
to:

"Which option produces the most clearly human-readable code" and
"Which option enables you to get to the point where that code is
written, teted and error-free more quickly"
That would be, whichever option involves the least possible amount of
C++.

Since you're using C++, you've relegated these concerns to a very low
priority.

Oct 11 '06 #7

Kaz Kylheku wrote:
Gavin Deane wrote:
Unless and until proven otherwise, both those concerns are secondary
to:

"Which option produces the most clearly human-readable code" and
"Which option enables you to get to the point where that code is
written, teted and error-free more quickly"

That would be, whichever option involves the least possible amount of
C++.

Since you're using C++, you've relegated these concerns to a very low
priority.
I'm not sure I follow you. At what point in the software development
process does either the need to produce clearly human-readable and
therefore easily maintainable code or the need to get from the point of
having no code to the point of having written, tested and debugged code
faster rather than slower become a very low priority?

Gavin Deane

Oct 12 '06 #8

Gavin Deane wrote:
Kaz Kylheku wrote:
Gavin Deane wrote:
Unless and until proven otherwise, both those concerns are secondary
to:
>
"Which option produces the most clearly human-readable code" and
"Which option enables you to get to the point where that code is
written, teted and error-free more quickly"
That would be, whichever option involves the least possible amount of
C++.

Since you're using C++, you've relegated these concerns to a very low
priority.

I'm not sure I follow you. At what point in the software development
process does either the need to produce clearly human-readable and
therefore easily maintainable code or the need to get from the point of
having no code to the point of having written, tested and debugged code
faster rather than slower become a very low priority?
At that point in the process when it was decided to do it in C++.

Oct 12 '06 #9

Kaz Kylheku wrote:
Gavin Deane wrote:
Kaz Kylheku wrote:
Gavin Deane wrote:
Unless and until proven otherwise, both those concerns are secondary
to:

"Which option produces the most clearly human-readable code" and
"Which option enables you to get to the point where that code is
written, teted and error-free more quickly"
>
That would be, whichever option involves the least possible amount of
C++.
>
Since you're using C++, you've relegated these concerns to a very low
priority.
I'm not sure I follow you. At what point in the software development
process does either the need to produce clearly human-readable and
therefore easily maintainable code or the need to get from the point of
having no code to the point of having written, tested and debugged code
faster rather than slower become a very low priority?

At that point in the process when it was decided to do it in C++.
You appear to be considering what criteria one might use to select
between programming languages. I am assuming that C++ has already been
selected as the language. There could be many reasons for that and for
the purposes of my point, the reason is irrelevant. So, given that C++
is the language chosen, and understanding that for a given programming
problem there will be multiple C++ solutions with varying degrees of
human-readability and varying rates of progress to correct, tested and
debugged completion, let me rephrase:

At what point in the C++ software development process does either the
need to produce clearly human-readable and therefore easily
maintainable code or the need to get from the point of having no code
to the point of having written, tested and debugged code faster rather
than slower become a very low priority?

Gavin Deane

Oct 12 '06 #10

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

Similar topics

3
1663
by: William Djaja Tjokroaminata | last post by:
Hi, As I am interfacing my code in C++ with a scripting language which is written in C, I need to store pointers to objects in a class hierarchy: ParentClass *obj1; obj1 = new ParentClass (...); ... do things with obj1... STORE_POINTER (scriptObj1, obj1);
2
3105
by: Chester | last post by:
Is it possible to new an object A, downcast it to B, and store B in a vector? I'm having problem with the following code: B is a derived class of A Blist.push_back( new B() ); Blist.push_back( new B() ); A *a = new A(); B *b = static_cast<B*>(a);
10
2025
by: Whitney Kew | last post by:
Hello, I have a question regarding type conversion operators. Let's say I have a simple (nonsensical) class as follows: class MakeNewInt { private: int* _n;
6
2577
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a "SEGV" when run (presumably - attempt to delete deleted memory. Please take a look and see if you can notice any mistakes I'm making. Basically, I want to store classes of my objects in a vector. I also have three further questions:
12
3944
by: Alfonso Morra | last post by:
I have the ff code for testing the concept of storing objects: #include <vector> #include <iostream> using namespace std ; class MyClass { public: MyClass(){
4
1965
by: gsyoon | last post by:
hi, all. I'm trying to make a "framework" to store the return value of a function to a global memory. My first attempt was 1) void store_to_global( char * type_name ) { if ( strcmp( type_name, "CLASS_A") )
5
1838
by: Joe | last post by:
Coming from C++ I'm so used to being able to use pointers in Tag properties that I'm a little lost. I want to assign an instance of a class to a Tag property and be able to change one of it's members. I need this change to take effect in the original class instance (as it would with a pointer). How can this be done? Thanks
3
4698
by: marco_segurini | last post by:
Hi, the 'user' class describes what is my target: I want to save a reference to a class instance and a reference to the reference to the class instance calling a 'user' member function and later use them in another member function to restore the original value. Is there any C# programming technique that may help me?
10
2715
by: Jess | last post by:
Hello, I have a program that stores dynamically created objects into a vector. #include<iostream> #include<vector> using namespace std;
0
8991
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
8830
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
9370
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...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8242
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
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
6074
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
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2215
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.