473,734 Members | 2,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Standard Template Library as container for Objects

Is the Standard Template Library capable of storing complete objects rather
than pointers. For example, would either of the vectors below (a, c)
function correctly (note the member in C).

class A {};
class B {};
class C {
stl::vector<B> b;
};

std::vector<A> a;
std::vector<C> c;

The reason that I ask is that I had something similar to this in a library
that I was writing. It worked as far as I could add objects to the various
containers and index through them. However, when the objects dropped out of
scope and deallocated their lists an assertion was thrown (I forget the
message, but it was from deep within the bowels of CRT new) for each class
object that had a vector containing a class. In release mode, all hell
broke loose (access violations, everywhere).
I know that this would seem to scream, "No, don't use STL containers with
objects!" but I was wondering if there was any other explanation, like using
STL with DLLs, or linking the wrong run-time and using STL, or using
different and incompatible run-times between the test program and the DLL
project. So, could those be the cause, or is it just a case of using
pointers to objects instead? Would that include the stl::auto_ptr template
class?
Are there any rules that should be followed when it comes to the various
run-time libraries? Such as what combinations produce problems, and when
generally should the different libraries be used? Those sorts of details
seemed to be omitted from books, so are there any good references out there
(preferably web-sites, I'm poor, but *really* excellent books are OK).

Thank you (in advance),

Mike

--
Michael Winter
M.Winter@[no-spam]blueyonder.co.u k (remove [no-spam] to reply by e-mail)
Jul 19 '05 #1
14 2124

"Michael Winter" <M.Winter@[no-spam]blueyonder.co.u k> schreef in bericht
news:eE******** *************** @news-text.cableinet. net...
Is the Standard Template Library capable of storing complete objects rather than pointers. For example, would either of the vectors below (a, c)
function correctly (note the member in C).
Basically, storing objects in std:: containers is safe as long as the class
of those objects
support value semantics. Your object will basically need a safe (and
preferably efficient)
default constructor, copy constructor and assignment operator. I suspect
that in the example
you show below you are using a class that does not (properly) implement
these, and that
is what causes the problem at cleanup (double deletes maybe?).

Marijn Haverbeke

class A {};
class B {};
class C {
stl::vector<B> b;
};

std::vector<A> a;
std::vector<C> c;

The reason that I ask is that I had something similar to this in a library
that I was writing. It worked as far as I could add objects to the various containers and index through them. However, when the objects dropped out of scope and deallocated their lists an assertion was thrown (I forget the
message, but it was from deep within the bowels of CRT new) for each class
object that had a vector containing a class. In release mode, all hell
broke loose (access violations, everywhere).
I know that this would seem to scream, "No, don't use STL containers with
objects!" but I was wondering if there was any other explanation, like using STL with DLLs, or linking the wrong run-time and using STL, or using
different and incompatible run-times between the test program and the DLL
project. So, could those be the cause, or is it just a case of using
pointers to objects instead? Would that include the stl::auto_ptr template class?
Are there any rules that should be followed when it comes to the various
run-time libraries? Such as what combinations produce problems, and when
generally should the different libraries be used? Those sorts of details
seemed to be omitted from books, so are there any good references out there (preferably web-sites, I'm poor, but *really* excellent books are OK).

Thank you (in advance),

Mike

--
Michael Winter
M.Winter@[no-spam]blueyonder.co.u k (remove [no-spam] to reply by e-mail)

Jul 19 '05 #2
Michael Winter wrote:
Is the Standard Template Library capable of storing complete objects
rather than pointers. For example, would either of the vectors below
(a, c) function correctly (note the member in C).

class A {};
class B {};
class C {
stl::vector<B> b;
};

std::vector<A> a;
std::vector<C> c;

The reason that I ask is that I had something similar to this in a
library that I was writing. It worked as far as I could add objects
to the various containers and index through them. However, when the
objects dropped out of scope and deallocated their lists an assertion
was thrown (I forget the message, but it was from deep within the
bowels of CRT new) for each class object that had a vector containing
a class. In release mode, all hell broke loose (access violations,
everywhere).
I know that this would seem to scream, "No, don't use STL containers
with objects!" but I was wondering if there was any other
explanation, like using STL with DLLs, or linking the wrong run-time

[SNIP]

It was most probably using DLLs. There is some issue (off-topic here) on
Windows if you static-link the runtime to the DLL and the EXE you will get
separate heaps and trouble.

The STL containers are fully capable of storing complete objects and
destroying them properly when they are removed.

--
Attila aka WW
Jul 19 '05 #3
Attila Feher wrote:

It was most probably using DLLs. There is some issue (off-topic here) on
Windows if you static-link the runtime to the DLL and the EXE you will get
separate heaps and trouble.


Yes, I find that using the dynamic runtime for all DLLs and EXEs is the
safest practice (and produces the smallest output file). Note that this
means that MSVCPxx.DLL needs to be installed on the target machine if it
is not already present.

--
Mike Smith

Jul 19 '05 #4
Thanks for all your help.

Marjin: I didn't specify the copy constructor and assignment operator as
the defaults should suffice, but I'll check. I suppose it's better practice
to explicitly define them anyway.

Attila and Mike: I've posted a question along those lines to a Windows
group, so hopefully they'll provide me some more information.

Again, thank you.

Mike

--
Michael Winter
M.Winter@[no-spam]blueyonder.co.u k (remove [no-spam] to reply)
Jul 19 '05 #5

"Marijn" <ma******@hotma il.com> wrote in message news:3f******** *************@n ews.inter.NL.ne t...
Basically, storing objects in std:: containers is safe as long as the class
of those objects
support value semantics. Your object will basically need a safe (and
preferably efficient)
default constructor, copy constructor and assignment operator.


A default constructor is not required.
Jul 19 '05 #6

"Michael Winter" <M.Winter@[no-spam]blueyonder.co.u k> wrote in message news:Dl******** ***********@new s-text.cableinet. net...
Thanks for all your help.

Marjin: I didn't specify the copy constructor and assignment operator as
the defaults should suffice, but I'll check. I suppose it's better practice
to explicitly define them anyway.

I don't feel it is. The implementation of these is not trivial to reproduce.
You have to copy/assign each base class and member seperately. If
the compiler generated ones are correct (and if your base classes and
members are all well behaved independentally , it will be).
Jul 19 '05 #7
> > Marjin: I didn't specify the copy constructor and assignment operator
as
the defaults should suffice, but I'll check. I suppose it's better practice to explicitly define them anyway.
I don't feel it is. The implementation of these is not trivial to

reproduce. You have to copy/assign each base class and member seperately. If
the compiler generated ones are correct (and if your base classes and
members are all well behaved independentally , it will be).


I didn't consider large, complex classes. The ones this post relates to
aren't. It's a shame that the we can't have the best of both worlds: have
the compiler worry about simple members and leave the more complicated parts
(deep-copies, for example) for the developer.

Mike

--
Michael Winter
M.Winter@[no-spam]blueyonder.co.u k (remove [no-spam] to reply)
Jul 19 '05 #8

"Michael Winter" <M.Winter@[no-spam]blueyonder.co.u k> wrote in message news:9H******** ***********@new s-text.cableinet. net...
. It's a shame that the we can't have the best of both worlds: have
the compiler worry about simple members and leave the more complicated parts
(deep-copies, for example) for the developer.


Well, it's not clear how you would do that. How do you specify which members/bases
are not automatically copied and which are?

It's workable in C++. If you only have a few things that require special copying
semantics, wrap them in classes that perform those operations. That's one of
the reasons that string is better than char*, and vector is better than handling your
own new'd array, etc...
Jul 19 '05 #9
> Well, it's not clear how you would do that. How do you specify which
members/bases
are not automatically copied and which are?
That's why I said it's a shame :) You can't in any real sense. Sorry, it
was a pointless comment.
It's workable in C++. If you only have a few things that require special copying semantics, wrap them in classes that perform those operations. That's one of the reasons that string is better than char*, and vector is better than handling your own new'd array, etc...


Mike
Jul 19 '05 #10

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

Similar topics

25
3790
by: Magnus Lie Hetland | last post by:
Is there any interest in a (hypothetical) standard graph API (with 'graph' meaning a network, consisting of nodes and edges)? Yes, we have the standard ways of implementing graphs through (e.g.) dicts mapping nodes to neighbor-sets, but if one wants a graph that's implemented in some other way, this may not be the most convenient (or abstract) interface to emulate. It might be nice to have the kind of polymorphic freedom that one has with,...
5
3035
by: sks_cpp | last post by:
Are the standard library functions pertinent to both sequence containers and associative containers? For example, if "find_if", "remove_if", etc... valid for both lists, deques, vectors, sets, and maps? I know that the word "iterator" is typedef'd from something for everyone of these containers so I was curious if all these functions are valid. On the same note, should I use "find_if" or "remove_if" for the following
3
2672
by: Thomas Matthews | last post by:
Hi, I would like to apply inheritance to a template parameter, but my design fails to compile: cannot initialize one template class with child child parameterized class. I'll explain... Given: #include <string> // Parent class
1
1851
by: Aaron Walker | last post by:
Hey folks, I've got some code for an options class that stores user run time options (as static members) so that they are availably any time an instance is created; currently the code has a set_OPTION get_OPTION kind of function interface, but as you can imagine that can get quite large after a while. I've never messed with templates before, but this seemed like a good opportunity to learn. The code (that doesn't compile) is below,...
43
5008
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own libraries. I'm not sure if that's a good thing or not. AFAIK, most of Qt is compatable with the Standard Library. That is, QLT can interoperate with STL, and you can convert back and forth between std::string and Qt::QString, etc. Are there any...
13
2533
by: Dan Tsafrir | last post by:
is the following code standard? (cleanly compiles under g++-4.0.2): struct Asc { bool operator()(int a, int b) {return a < b;} }; struct Des { bool operator()(int a, int b) {return b > a;} }; int main() { int arr = {1, 2, 3}; set<int,Asc> asc(arr, arr+3); set<int,Des>::iterator beg = asc.begin(); // set<int,Des>::iterator end = asc.end(); // copy(beg, end, ostream_iterator<int>(cout,...
45
2909
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs to check. What I expected was that there would be minor code bloat and some speed improvement when using templates. However... I wrote a basic list container (using templates), and a list container (using virtual derived classes). I also tried...
16
2064
by: subramanian100in | last post by:
Program 1: --------------- #include <cstdlib> #include <iostream> #include <vector> using namespace std; int main() {
32
2727
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template issue. A noddy mixin layer example should illustrate the issue... class Base { protected: int m_Field;
0
8776
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
9449
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
9236
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
8186
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
6735
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
4550
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
3261
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
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
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.