473,652 Members | 2,965 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rationale behind copy semantics in STL containers.

Just a simple theoritical question to the experts.

What was the rationale behind making STL containers follow copy
semantics rather than reference semantics. References almost always
make things easier without much of overhead. Then why not reference ?

Thanks
/P

Oct 25 '06 #1
35 2825
dragoncoder wrote:
Just a simple theoritical question to the experts.

What was the rationale behind making STL containers follow copy
semantics rather than reference semantics. References almost always
make things easier without much of overhead. Then why not reference ?

Thanks
/P
Because to have a valid reference you must somewhere else have an object
and you must therefore be aware of the lifetime of that object. This
leaves open the possibilities of dangling references and memory leaks.
STL containers own their objects and clean up after themselves when the
container is destructed, essentially solving both of these problems.

If you really want the equivalent of a stored reference, you can make a
container of pointers-- likely that's what a reference is "under the
hood" anyway.
Oct 25 '06 #2

Mark P kirjoitti:
If you really want the equivalent of a stored reference, you can make a
container of pointers-- likely that's what a reference is "under the
hood" anyway.
Or more precisely, a memory address. ;)

Oct 26 '06 #3

dragoncoder wrote:
Just a simple theoritical question to the experts.

What was the rationale behind making STL containers follow copy
semantics rather than reference semantics. References almost always
make things easier without much of overhead. Then why not reference ?
Because C and C++ always has been value-based and always has allowed
you to use pointers in case you really wanted "reference" semantics. (I
believe the wording is from Java - thus it is misleading in a C++
context). And references do have quite a lot of overhead: that is why
"simple objects" such as integers are value-based in languages such as
Java (needing "boxing" to work as real objects).

/Peter

Oct 26 '06 #4

peter koch wrote:
dragoncoder wrote:
Just a simple theoritical question to the experts.

What was the rationale behind making STL containers follow copy
semantics rather than reference semantics. References almost always
make things easier without much of overhead. Then why not reference ?
Because C and C++ always has been value-based and always has allowed
you to use pointers in case you really wanted "reference" semantics. (I
believe the wording is from Java - thus it is misleading in a C++
context). And references do have quite a lot of overhead: that is why
"simple objects" such as integers are value-based in languages such as
Java (needing "boxing" to work as real objects).

/Peter
and containers of built-in types (e.g. int and double) and value types
(e.g., complex and pair) are very common and important.

-- Bjarne Stroustrup; http://www.reasearch.att.com/~bs

Oct 30 '06 #5

"bjarne" <bj****@gmail.c omwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
>
peter koch wrote:
>dragoncoder wrote:
Just a simple theoritical question to the experts.

What was the rationale behind making STL containers follow copy
semantics rather than reference semantics. References almost always
make things easier without much of overhead. Then why not reference ?
Because C and C++ always has been value-based and always has allowed
you to use pointers in case you really wanted "reference" semantics. (I
believe the wording is from Java - thus it is misleading in a C++
context). And references do have quite a lot of overhead: that is why
"simple objects" such as integers are value-based in languages such as
Java (needing "boxing" to work as real objects).
Peter:

Can you quantify/explain, "references do have quite a lot of overhead" a
bit?

Tony
Dec 13 '06 #6
On Dec 13, 2:31 am, "Tony" <rdnewsNOSPAM2. ..@sbcglobal.ne twrote:
"bjarne" <bja...@gmail.c omwrote in messagenews:11* *************** ******@i42g2000 cwa.googlegroup s.com...
peter koch wrote:
dragoncoder wrote:
Just a simple theoritical question to the experts.
What was the rationale behind making STL containers follow copy
semantics rather than reference semantics. References almost always
make things easier without much of overhead. Then why not reference ?
Because C and C++ always has been value-based and always has allowed
you to use pointers in case you really wanted "reference" semantics. (I
believe the wording is from Java - thus it is misleading in a C++
context). And references do have quite a lot of overhead: that is why
"simple objects" such as integers are value-based in languages such as
Java (needing "boxing" to work as real objects).Peter:

Can you quantify/explain, "references do have quite a lot of overhead" a
bit?
When using references/pointers you need to perform an additional
memory-access, first one when you get the pointer from the container
and a second one when following the pointer to the actual object. Take
for example a vector (in which the contained elements are stored
contiguously), what you need to keep in the processors cache when
working with it can be the iterator and the contained elements, if you
use pointers you need to keep the iterator, the contained elements
_and_ the actual data, which might be spread all over the place leading
to repeated cache-misses and severely affecting performance.

--
Erik Wikström

Dec 13 '06 #7

<er****@student .chalmers.sewro te in message
news:11******** **************@ f1g2000cwa.goog legroups.com...
On Dec 13, 2:31 am, "Tony" <rdnewsNOSPAM2. ..@sbcglobal.ne twrote:
"bjarne" <bja...@gmail.c omwrote in
messagenews:11* *************** ******@i42g2000 cwa.googlegroup s.com...
peter koch wrote:
dragoncoder wrote:
Just a simple theoritical question to the experts.
What was the rationale behind making STL containers follow copy
semantics rather than reference semantics. References almost always
make things easier without much of overhead. Then why not reference ?
Because C and C++ always has been value-based and always has allowed
you to use pointers in case you really wanted "reference" semantics. (I
believe the wording is from Java - thus it is misleading in a C++
context). And references do have quite a lot of overhead: that is why
"simple objects" such as integers are value-based in languages such as
Java (needing "boxing" to work as real objects).Peter:

Can you quantify/explain, "references do have quite a lot of overhead" a
bit?
"When using references/pointers you need to perform an additional
memory-access, first one when you get the pointer from the container
and a second one when following the pointer to the actual object. Take
for example a vector (in which the contained elements are stored
contiguously), what you need to keep in the processors cache when
working with it can be the iterator and the contained elements, if you
use pointers you need to keep the iterator, the contained elements
_and_ the actual data, which might be spread all over the place leading
to repeated cache-misses and severely affecting performance."

OK. I thought he (you?) meant that references had some kind of overhead
over that which pointers have (something behind the scenes). The above
is obvious. "references do have quite a lot of overhead" sounds like
something more than just the extra level of indirection.

Tony
Dec 13 '06 #8
On Dec 13, 12:12 pm, "Tony" <rdnewsNOSPAM2. ..@sbcglobal.ne twrote:
<eri...@student .chalmers.sewro te in messagenews:11* *************** ******@f1g2000c wa.googlegroups .com...
On Dec 13, 2:31 am, "Tony" <rdnewsNOSPAM2. ..@sbcglobal.ne twrote:
peter koch wrote:
>dragoncoder wrote:
Just a simple theoritical question to the experts.
What was the rationale behind making STL containers follow copy
semantics rather than reference semantics. References almost always
make things easier without much of overhead. Then why not reference ?
>Because C and C++ always has been value-based and always has allowed
>you to use pointers in case you really wanted "reference" semantics.(I
>believe the wording is from Java - thus it is misleading in a C++
>context). And references do have quite a lot of overhead: that is why
>"simple objects" such as integers are value-based in languages such as
>Java (needing "boxing" to work as real objects).Peter:
Can you quantify/explain, "references do have quite a lot of overhead"a
bit?"When using references/pointers you need to perform an additional
memory-access, first one when you get the pointer from the container
and a second one when following the pointer to the actual object. Take
for example a vector (in which the contained elements are stored
contiguously), what you need to keep in the processors cache when
working with it can be the iterator and the contained elements, if you
use pointers you need to keep the iterator, the contained elements
_and_ the actual data, which might be spread all over the place leading
to repeated cache-misses and severely affecting performance."

OK. I thought he (you?) meant that references had some kind of overhead
over that which pointers have (something behind the scenes). The above
is obvious. "references do have quite a lot of overhead" sounds like
something more than just the extra level of indirection.
Maybe, I don't know what he was thinking of, but as others have pointed
out reference semantics is not clearly defined in C++ since we have
both pointers and references and neither of them are the same thing as
in Java or C#. <speculationI n those languages the references are more
than just pointers since they are also used to manage garbage
collection, this might have some kind of impact on performance too.
</speculation>

Notice that in certain applications the overhead of the extra
indirection can be quite a large part of the total time needed to
perform an operation, say for example if you have a really large
std::vector<int *and you go through it sum up all the elements, since
the actual operation is quite fast the overhead could be quite
noticeable, if you on the other hand performed a complicated operation
on each element the impact can be relatively small compared to the
total running time.

--
Erik Wikström

Dec 13 '06 #9

er****@student. chalmers.se wrote:
On Dec 13, 2:31 am, "Tony" <rdnewsNOSPAM2. ..@sbcglobal.ne twrote:
"bjarne" <bja...@gmail.c omwrote in messagenews:11* *************** ******@i42g2000 cwa.googlegroup s.com...
peter koch wrote:
>dragoncoder wrote:
Just a simple theoritical question to the experts.
What was the rationale behind making STL containers follow copy
semantics rather than reference semantics. References almost always
make things easier without much of overhead. Then why not reference ?
>Because C and C++ always has been value-based and always has allowed
>you to use pointers in case you really wanted "reference" semantics. (I
>believe the wording is from Java - thus it is misleading in a C++
>context). And references do have quite a lot of overhead: that is why
>"simple objects" such as integers are value-based in languages such as
>Java (needing "boxing" to work as real objects).Peter:
Can you quantify/explain, "references do have quite a lot of overhead" a
bit?

When using references/pointers you need to perform an additional
memory-access, first one when you get the pointer from the container
and a second one when following the pointer to the actual object. Take
for example a vector (in which the contained elements are stored
contiguously), what you need to keep in the processors cache when
working with it can be the iterator and the contained elements, if you
use pointers you need to keep the iterator, the contained elements
_and_ the actual data, which might be spread all over the place leading
to repeated cache-misses and severely affecting performance.
Yes. Great. Means we have container library able to store just values.

Translates in container library really efficient only for fundamental
and trivial concrete types.

Therefore you have to store anything non-copyable (or hard to copyable)
as pointer.

Means we are getting all troubles described above, plus the problem of
that container is no more able to manage object lifetime.

Regards,

Mirek

Dec 13 '06 #10

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

Similar topics

3
4291
by: Naren | last post by:
Hello All, I get an unexpected behaviour for this class D{ /*something*/ }; class B
1
1254
by: Kay Schluehr | last post by:
In almost any case I install a Python package via distutils some directories in the package tree are left behind e.g. the docs, licenses, tests etc. I wonder if there is some rationale behind this? Should it be left to the "creative freedom" of the user to copy the docs whereever she wants or is there a dedicated place for them and if any why isn't it simple to declare it in the setup script?
17
3562
by: baibaichen | last post by:
i have written some code to verify how to disable slicing copy according C++ Gotchas item 30 the follow is my class hierarchy, and note that B is abstract class!! class B { public: explicit B(INT32 i =0):i_(i){} virtual ~B(){}
11
7388
by: PengYu.UT | last post by:
The following program calls the normal constructor and the copy constructor. By calling the copy constuctor is redundandant, all I want is only a vector of a trial object. Is there any way to avoid the use of the copy constructor? #include <vector> #include <iostream>
12
2692
by: Edward Diener | last post by:
Given value class X { public: // Not allowed: X():i(100000),s(10000) { } // Allowed void InitializeDefaults() { i = 100000; s = 10000; } private: int i;
26
15790
by: saxenavaibhav17 | last post by:
what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy? and what is the difference between them? pls help vaibhav
17
2055
by: qazmlp1209 | last post by:
Will the following program cause any problem, because of the missing explicit copy constructor for the 'test' class? #include <vector> #include <iostream> class test { char* cPtr ;
0
8370
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
8811
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8470
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8590
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
5620
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
4147
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2707
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1591
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.