473,406 Members | 2,698 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,406 software developers and data experts.

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.uk (remove [no-spam] to reply by e-mail)
Jul 19 '05 #1
14 2098

"Michael Winter" <M.Winter@[no-spam]blueyonder.co.uk> 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.uk (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.uk (remove [no-spam] to reply)
Jul 19 '05 #5

"Marijn" <ma******@hotmail.com> wrote in message news:3f*********************@news.inter.NL.net...
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.uk> wrote in message news:Dl*******************@news-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.uk (remove [no-spam] to reply)
Jul 19 '05 #8

"Michael Winter" <M.Winter@[no-spam]blueyonder.co.uk> wrote in message news:9H*******************@news-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

"Ron Natalie" <ro*@sensor.com> schreef in bericht
news:3f***********************@news.newshosting.co m...

"Marijn" <ma******@hotmail.com> wrote in message news:3f*********************@news.inter.NL.net...
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.


Well, in some cases it is. If you resize an std::vector it will fill the new
space with default objects, std::map will generate default objects if you
access an unused key value for example. I think that the gcc implementation
(somehow?) complains about a class not having a default constructor when you
create a vector or map with a class that has constructors but no default
constructor.

Marijn
Jul 19 '05 #11
Michael Winter <M.Winter@[no-spam]blueyonder.co.uk> wrote:
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 doubt so.

Writing them yourself has the potential of missing a member or such,
especially if the class is augmented later in the process.

If the compiler generated constructor and assignment works
conceptually, there is no need at all to try to build equivalent code.

Andre'
Jul 19 '05 #12
Michael Winter <M.Winter@[no-spam]blueyonder.co.uk> wrote:
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.


You can put the 'difficult' part in another class with user defined
c'ctor etc and keep the simple stuff as well as an instance of the
'difficult class' as member the your 'real' class.

In fact, if you have to make such a distinction between 'simple' and
'difficult' members, I'd guess that your class is too big already...
Andre'

Jul 19 '05 #13

"Marijn" <ma******@hotmail.com> wrote in message news:3f*********************@news.inter.NL.net...

A default constructor is not required.
Well, in some cases it is. If you resize an std::vector it will fill the new
space with default objects,


Incorrect. It fills the vector with copies of the second argument to resize.
This just happens to be defaulted to a default constructed object of that type.
But nothing precludes you from using an object that can not be default
constructed provided that in the few places where the "fill" object is required
that you provide one rather than relying on the default.

std::map will generate default objects if you access an unused key value for example.
You are correct here. Operator[] has no provision for a non default constructed value_type
(the key_type doesn't matter). However, you're free to use insert rather than operator[]
if you want to use non-default constructable maps.
I think that the gcc implementation
(somehow?) complains about a class not having a default constructor when you
create a vector or map with a class that has constructors but no default
constructor.


If it does it is defective. My version of G++ handles this fine:

#include <vector>
#include <map>
using namespace std;

class NoDefault {
public:
NoDefault(int i);
};

vector<NoDefault> v;
map<int, NoDefault> k1;
map<NoDefault, int> k2;
..
Jul 19 '05 #14

"Ron Natalie" <ro*@sensor.com> schreef in bericht
news:3f*********************@news.newshosting.com. ..
I think that the gcc implementation
(somehow?) complains about a class not having a default constructor when you create a vector or map with a class that has constructors but no default
constructor.


If it does it is defective. My version of G++ handles this fine:


Hmm i remember having to make a default constructor
for some classes when i did not really need one for anything i did with
them. I'll look into it again, because the way i currently have it -
assert(false) within the default constructor - is really ugly.

Marijn
Jul 19 '05 #15

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

Similar topics

25
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.)...
5
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,...
3
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... ...
1
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...
43
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...
13
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;} };...
45
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...
16
by: subramanian100in | last post by:
Program 1: --------------- #include <cstdlib> #include <iostream> #include <vector> using namespace std; int main() {
32
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...

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.