473,729 Members | 2,353 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deleting objects obtained from a STL library

Hey guys. A new question:

I want to use an STL libarary to hold a bunch of objects I create.
Actually, it will hold references to the objects, but that's beside the
point, for the most part. Here's the question: I want to be able to change
the references (including deleting them). Is there any way to do that
besides using pointers rather than references for the STL library? I'd also
prefer to avoid using const_cast, if it is indeed avoidable.

Thanks much.
Jul 22 '05 #1
9 1887
Aguilar, James wrote in news:ce******** **@newsreader.w ustl.edu in
comp.lang.c++:
Hey guys. A new question:

I want to use an STL libarary to hold a bunch of objects I create.
Actually, it will hold references to the objects, but that's beside
the point, for the most part. Here's the question: I want to be able
to change the references (including deleting them). Is there any way
to do that besides using pointers rather than references for the STL
library? I'd also prefer to avoid using const_cast, if it is indeed
avoidable.


http://www.boost.org/libs/smart_ptr/shared_ptr.htm

And *don't* use const_cast, if you feel tempted go back and
redesign until the *itch* goes away.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

"Aguilar, James" <jf**@cec.NOBOT Swustl.edu> wrote in message
news:ce******** **@newsreader.w ustl.edu...
Hey guys. A new question:

I want to use an STL libarary to hold a bunch of objects I create.
Actually, it will hold references to the objects, but that's beside the
point, for the most part. Here's the question: I want to be able to change the references (including deleting them). Is there any way to do that
besides using pointers rather than references for the STL library? I'd also prefer to avoid using const_cast, if it is indeed avoidable.

Thanks much.


I think I might have misunderstood my own question. First, please clarify
something for me:

Are the objects in the container the same objects as the ones that I used to
fill the container? Are they references to those objects, or are they
copies of those objects? How did the container know how to make the copies?
Are they just shallow copies of the original objects (i.e. all pointers are
the same, all other members were copied completely)?

Secondly, suppose I have a function. There is a type called Object which I
use here:

Object& fun1()
{
Object a;
return a;
}

is not legal but

Object fun1()
{
Object a;
return a;
}

is. I think I understand why. However, let me ask you this: is this code
going to be inefficient if "Object" is ten kilobytes in size? Is what's
actually happening in the second example is that the entire object is being
copied and returned? Lastly, if this is so, can this overhead be avoided by
only passing ints and assigning all medium or large sized objects to the
free store?

Lastly, is there anything I can do with references that I can't do with
pointers? Is there anything that is especially inelegant with pointers that
references accomplish well? For instance, I had a class that we going to
have a vector (that was itself a vector of vectors) member earlier.
However, there are two ways to do it. One is to tell the class that all it
has is a pointer to a vector, and one is to make the vector actually a
member of the class. I found that with a pointer to a vector, accessing the
vector would look ugly indeed:

(*(*grid)[10])[25]->foo(); /*Isn't there any way to at least make this look
better? It does the same thing as the second
example */

is the same as

grid[10][25].foo();

only the second uses the actual objects or references to them, and the first
uses pointers. However, my experience in Java (which is the language I
began working on before I began teaching myself C++) tells me that passing
pointers around is more efficient than passing objects around (in the event
that large objects need to be communicated between different parts of the
program). I'm also used to the convenience of never touching an actual
object, but always working with pointers. I have this natural, builtin
aversion to copying objects unless you are actually conceptually going to be
using a copy of them. For instance, if my suspicions are correct and the
first example in which I pass out an "Object" named a is correct and the
method is copying a to return it, I would rather use the free store.
There's no reason why the object should have to be constructed twice in an
example like that.

I guess my question is, on which side should I err: using the free store a
lot and using pointers a lot, or using references a lot and perhaps paying
overhead when I return large objects from methods?
Jul 22 '05 #3
"Aguilar, James" <jf**@cec.NOBOT Swustl.edu> wrote in message
news:ce******** **@newsreader.w ustl.edu...

"Aguilar, James" <jf**@cec.NOBOT Swustl.edu> wrote in message
news:ce******** **@newsreader.w ustl.edu...
Hey guys. A new question:

I want to use an STL libarary to hold a bunch of objects I create.
Actually, it will hold references to the objects, but that's beside the
point, for the most part. Here's the question: I want to be able to change
the references (including deleting them). Is there any way to do that
besides using pointers rather than references for the STL library? I'd

also
prefer to avoid using const_cast, if it is indeed avoidable.

Thanks much.


I think I might have misunderstood my own question. First, please clarify
something for me:

Are the objects in the container the same objects as the ones that I used

to fill the container? Are they references to those objects, or are they
copies of those objects? How did the container know how to make the copies? Are they just shallow copies of the original objects (i.e. all pointers are the same, all other members were copied completely)?
The objects stored inside an STL container are copies. For example:

MyClass c;
std::vector<MyC lass> v;
v.push_back(c);

The vector now contains one instance of MyClass that was copied from c
(using the copy constructor). If the copy constructor is defined as a
"shallow" copy, then the copy made is "shallow".
Secondly, suppose I have a function. There is a type called Object which I use here:

Object& fun1()
{
Object a;
return a;
}

is not legal but

Object fun1()
{
Object a;
return a;
}

is. I think I understand why. However, let me ask you this: is this code
going to be inefficient if "Object" is ten kilobytes in size? Is what's
actually happening in the second example is that the entire object is being copied and returned? Lastly, if this is so, can this overhead be avoided by only passing ints and assigning all medium or large sized objects to the
free store?
Questions about efficiency are hard to answer without measurements. There
are a few reasonable guidelines, but it's usually best to run some tests to
determine how expensive an operation is. However, I will say that one
rarely allocates something using dynamic allocation because it is more
efficient that way. I could see how there might be other concerns, though,
like stack space.

Also, it is possible that your compiler would use NRVO (named return value
optimization) to avoid copying objects in certain cases, so it's hard to say
if there is a copy made or not.
Lastly, is there anything I can do with references that I can't do with
pointers? Is there anything that is especially inelegant with pointers that references accomplish well? For instance, I had a class that we going to
have a vector (that was itself a vector of vectors) member earlier.
However, there are two ways to do it. One is to tell the class that all it has is a pointer to a vector, and one is to make the vector actually a
member of the class. I found that with a pointer to a vector, accessing the vector would look ugly indeed:
Pointers are useful because their values may change. A single pointer, over
time, may point to different locations, or nothing at all (usually signified
with a null pointer). References, however, lack this ability, and that
makes them simpler when such functionality is not required. Also,
references cannot be null. The simplicity is the main reason why references
and not pointers are usually used to pass an object to a function without
creating a copy.
(*(*grid)[10])[25]->foo(); /*Isn't there any way to at least make this look better? It does the same thing as the second
example */

is the same as

grid[10][25].foo();

only the second uses the actual objects or references to them, and the first uses pointers. However, my experience in Java (which is the language I
began working on before I began teaching myself C++) tells me that passing
pointers around is more efficient than passing objects around (in the event that large objects need to be communicated between different parts of the
program). I'm also used to the convenience of never touching an actual
object, but always working with pointers. I have this natural, builtin
aversion to copying objects unless you are actually conceptually going to be using a copy of them. For instance, if my suspicions are correct and the
first example in which I pass out an "Object" named a is correct and the
method is copying a to return it, I would rather use the free store.
There's no reason why the object should have to be constructed twice in an
example like that.

I guess my question is, on which side should I err: using the free store a
lot and using pointers a lot, or using references a lot and perhaps paying
overhead when I return large objects from methods?


One thing to keep in mind is that when an object is dynamically allocated,
it is typically the memory allocation itself that is the most expensive part
of the ordeal. However, if the object is a big, complex object that is
expensive to copy, then it may make sense to avoid copying it. That expense
is not usually measured in terms of bytes, but in terms of the time and
possibly extra memory it takes to execute the copy constructor (It's usually
not hard to copy a reasonable number of bytes from one location to another).
There is no one, true answer. In general, C++ programmers avoid using
dynamic allocation unless it is called for, so don't make dynamic allocation
the "default" method for allocation objects in your programs. If you do
that, C++ programmers will be able to smell the Java a mile away. ;-)

For objects that are expensive to copy, it is sometimes possible to
implement a "swap" method to avoid copying them. For example:

void foo(std::vector <int>& out) {
std::vector<int > v(20);
// fill in v
v.swap( out );
}

The std::vector::sw ap method doesn't perform an expensive copy operation and
instead quickly exchanges the vector's contents with the vector passed
(think internal pointer, etc value swapping).

If you do find yourself passing pointers to objects around, consider using
smart pointers, like boost::shared_p tr, because it can help protect your
code from memory leaks.

--
David Hilsee
Jul 22 '05 #4
Hello,

"David Hilsee" <da************ *@yahoo.com> wrote in message news:<2d******* *************@c omcast.com>...
The objects stored inside an STL container are copies. For example:

MyClass c;
std::vector<MyC lass> v;
v.push_back(c);

The vector now contains one instance of MyClass that was copied from c
(using the copy constructor).
does it mean that 2*sizeof(c) memory space is now occupied?
If the copy constructor is defined as a
"shallow" copy, then the copy made is "shallow".


What is a "shallow" copy?

Thank you,

Fabio.
Jul 22 '05 #5
"Aguilar, James" <jf**@cec.NOBOT Swustl.edu> wrote:
"Aguilar, James" <jf**@cec.NOBOT Swustl.edu> wrote:
Hey guys. A new question:

I want to use an STL libarary to hold a bunch of objects I create.
Actually, it will hold references to the objects, but that's beside the
point, for the most part. Here's the question: I want to be able to
change
the references (including deleting them). Is there any way to do that
besides using pointers rather than references for the STL library?

Don't put references in stl containers. IE vector<int&> is bad.

Are the objects in the container the same objects as the ones that I used to
fill the container?
No, stl containers make copies. For example:

void foo( vector<int>& vec ) {
int i = 5;
vec.push_back( i );
}

The fact that 'i' goes out of scope at the end of the function isn't a
problem because 'i' isn't in the vector, a copy of 'i' is.

How did the container know how to make the copies?
The container uses the copy constructor, if no copy c_tor is provided
then the compiler will make one, if the copy c_tor is disabled (by
making it private and not implementing it) then the object type cannot
be held in an stl container.

Are they just shallow copies of the original objects (i.e. all pointers are
the same, all other members were copied completely)?
If the copy c_tor makes shallow copies then they are just shallow copies.

Secondly, suppose I have a function. There is a type called Object which I
use here:

Object& fun1()
{
Object a;
return a;
}

is not legal but

Object fun1()
{
Object a;
return a;
}

is. I think I understand why. However, let me ask you this: is this code
going to be inefficient if "Object" is ten kilobytes in size?
Possibly, you won't know until you measure.

Is what's
actually happening in the second example is that the entire object is being
copied and returned?
Possibly.

Lastly, if this is so, can this overhead be avoided by
only passing ints and assigning all medium or large sized objects to the
free store?
The usual way to avoid this is to pass in an object by reference and let
the function modify it. As in:

Object& fun1( Object& a ) {
// do stuff to 'a'
return a;
}

But again, this usually isn't necessary. Measure first.

Lastly, is there anything I can do with references that I can't do with
pointers?
No.

Is there anything that is especially inelegant with pointers that
references accomplish well?
Yes, operator overloading for example.

For instance, I had a class that we going to
have a vector (that was itself a vector of vectors) member earlier.
Don't use vector of vectors. Write a Matrix class instead. (See the FAQ
on this.)

However, there are two ways to do it. One is to tell the class that all it
has is a pointer to a vector, and one is to make the vector actually a
member of the class.
Make the vector a member of the class. There is almost no reason to
dynamically allocate a standard container (I say 'almost' simply because
I'm hedging, I can think of no reason to dynamically allocate a
container.)

I found that with a pointer to a vector, accessing the
vector would look ugly indeed:

(*(*grid)[10])[25]->foo(); /*Isn't there any way to at least make this look
better? It does the same thing as the second
example */

is the same as

grid[10][25].foo();

only the second uses the actual objects or references to them, and the first
uses pointers. However, my experience in Java (which is the language I
began working on before I began teaching myself C++) tells me that passing
pointers around is more efficient than passing objects around (in the event
that large objects need to be communicated between different parts of the
program).
It may, or may not, be more efficient. You won't know until you measure.

I'm also used to the convenience of never touching an actual
object, but always working with pointers. I have this natural, builtin
aversion to copying objects unless you are actually conceptually going to be
using a copy of them. For instance, if my suspicions are correct and the
first example in which I pass out an "Object" named a is correct and the
method is copying a to return it, I would rather use the free store.
There's no reason why the object should have to be constructed twice in an
example like that.

I guess my question is, on which side should I err: using the free store a
lot and using pointers a lot, or using references a lot and perhaps paying
overhead when I return large objects from methods?


Don't do either. Err on the side of using objects a lot, switch over to
using references and pointers only when the language requires it, or
measurement shows its necessary.
Jul 22 '05 #6

"Aguilar, James" <jf**@cec.NOBOT Swustl.edu> wrote in message
news:ce******** **@newsreader.w ustl.edu...

"Aguilar, James" <jf**@cec.NOBOT Swustl.edu> wrote in message
news:ce******** **@newsreader.w ustl.edu...
Hey guys. A new question:
Are the objects in the container the same objects as the ones that I used

to fill the container? Are they references to those objects, or are they
No, STL containers hold copies of the values you inserted into them. You can
not insert references into a container.
copies of those objects? How did the container know how to make the copies?

They use the copy constructor for the type passed.
Are they just shallow copies of the original objects (i.e. all pointers are the same, all other members were copied completely)?
That's up to you when you implement the copy constructor. If you don't
supply one, the compiler provided default is a member wise copy. ie: invokes
the copy constructor of each memeber.
Secondly, suppose I have a function. There is a type called Object which I use here:

Object& fun1()
{
Object a;
return a;
}

is not legal but
Because upon exit of fun1 a is destroyed, so you end up with a reference to
a destroyed object.
Object fun1()
{
Object a;
return a;
}

is. I think I understand why. However, let me ask you this: is this code
You end up with an Object that is a copy of 'a'.
going to be inefficient if "Object" is ten kilobytes in size? Is what's
actually happening in the second example is that the entire object is being copied and returned? Lastly, if this is so, can this overhead be avoided by

Maybe so, maybe not. Many compilers provide RVO and/or NRVO ( named return
value optimization ). Which would elide the copy. Hence, the rule - avoid
premature optimization.
only passing ints and assigning all medium or large sized objects to the
free store?
As another poster suggested, use boost::shared_p tr:

typedef boost::shared_p tr<Object> tObjPtr;
tObjPtr fun1()
{
return tObjPtr( new Object );
}
Lastly, is there anything I can do with references that I can't do with
pointers? Is there anything that is especially inelegant with pointers that

References can't be reseated, and must be initialized.

[...]
I guess my question is, on which side should I err: using the free store a
lot and using pointers a lot, or using references a lot and perhaps paying
overhead when I return large objects from methods?


You should really get a good C++ book and study the FAQ.

Jeff F
Jul 22 '05 #7
"fabio de francesco" <fm**@tiscali.i t> wrote in message
news:ba******** *************** **@posting.goog le.com...
Hello,

"David Hilsee" <da************ *@yahoo.com> wrote in message news:<2d******* *************@c omcast.com>...
The objects stored inside an STL container are copies. For example:

MyClass c;
std::vector<MyC lass> v;
v.push_back(c);

The vector now contains one instance of MyClass that was copied from c
(using the copy constructor).


does it mean that 2*sizeof(c) memory space is now occupied?


At a minimum. If by "occupied" you mean "allocated" then there could easily
be more than that, because the std::vector object is probably holding on to
a single block of memory that could potentially hold more instances of
MyClass so it can have more efficient behavior if it is asked to hold more
instances.
If the copy constructor is defined as a
"shallow" copy, then the copy made is "shallow".


What is a "shallow" copy?


Not a deep copy. ;-) It is typically used to mean that the copy might leave
some data as shared between the two objects. For example, if you create a
copy of a tree object and the data stored in the nodes is shared between the
two trees, then that could be called a "shallow" copy, because it didn't
copy everything stored in the tree.

--
David Hilsee
Jul 22 '05 #8
"David Hilsee" <da************ *@yahoo.com> wrote in message news:<PK******* *************@c omcast.com>...
"fabio de francesco" <fm**@tiscali.i t> wrote in message
news:ba******** *************** **@posting.goog le.com...
"David Hilsee" <da************ *@yahoo.com> wrote in message

news:<2d******* *************@c omcast.com>...
The objects stored inside an STL container are copies. For example:

MyClass c;
std::vector<MyC lass> v;
v.push_back(c);

The vector now contains one instance of MyClass that was copied from c
(using the copy constructor).


does it mean that 2*sizeof(c) memory space is now occupied?


At a minimum. If by "occupied" you mean "allocated" then there could easily
be more than that, because the std::vector object is probably holding on to
a single block of memory that could potentially hold more instances of
MyClass so it can have more efficient behavior if it is asked to hold more
instances.


Thank you.
So, in the example given, and in order to avoid the original "c"
object to be kept around, should we delete it? And how? By an explicit
call to the destructor? Please let me know.

Fabio De Francesco.
Jul 22 '05 #9
"fabio de francesco" <fm**@tiscali.i t> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
"David Hilsee" <da************ *@yahoo.com> wrote in message

news:<PK******* *************@c omcast.com>...
"fabio de francesco" <fm**@tiscali.i t> wrote in message
news:ba******** *************** **@posting.goog le.com...
"David Hilsee" <da************ *@yahoo.com> wrote in message

news:<2d******* *************@c omcast.com>...

> The objects stored inside an STL container are copies. For example:
>
> MyClass c;
> std::vector<MyC lass> v;
> v.push_back(c);
>
> The vector now contains one instance of MyClass that was copied from c > (using the copy constructor).

does it mean that 2*sizeof(c) memory space is now occupied?


At a minimum. If by "occupied" you mean "allocated" then there could easily be more than that, because the std::vector object is probably holding on to a single block of memory that could potentially hold more instances of
MyClass so it can have more efficient behavior if it is asked to hold more instances.


Thank you.
So, in the example given, and in order to avoid the original "c"
object to be kept around, should we delete it? And how? By an explicit
call to the destructor? Please let me know.


The c object will be destroyed when it goes out of scope. No explicit call
to anything is required. Calling the destructor explicitly will only cause
the destructor to execute and it will not cause the storage for the object
itself to be released. Also, it would probably cause problems because the
destructor would be invoked again when the c object goes out of scope.

--
David Hilsee
Jul 22 '05 #10

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

Similar topics

2
519
by: Thomas Philips | last post by:
I'm teaching myself OOP using Michael Dawson's "Python Programming For The Absolute Beginner" and have a question about deleting objects. My game has two classes: Player and Alien, essentially identical, instances of which can shoot at each other. Player is described below class Player(object): #Class attributes for class Player n=0 #n is the number of players #Private methods for class Player
6
2036
by: Thomas Philips | last post by:
I have a question about deleting objects. My game has two classes, Player and Alien, essentially identical, instances of which can shoot at each other. Player is described below class Player(object): #Class attributes for class Player n=0 #n is the number of players #Private methods for class Player def __init__(self,name):
7
3568
by: Xamalek | last post by:
Greetings, I have a question. When using an STL container, does deleting a container of pointers also call delete on the (contained) pointers? For example, if I have (ignore the fluff, it is simply used to explain my issue) struct Proc {
6
1624
by: charliewest | last post by:
I have developed an application for WM 2003, which frequently transacts with a sql server ce 2.0 database. I have several procedures which utilize the following code: cn = new SqlCeConnection(@"<< sdf file >>"); cn.Open(); cmd = new SqlCeCommand(<< sql string >>, cn); cmd.ExecuteNonQuery(); cn.Close()
4
14025
by: al havrilla | last post by:
hi all what does the phrase: "scalar deleting destructor" mean? i'm getting this in a debug error message using c++ 7.1 thanks Al
13
3284
by: Alek Davis | last post by:
Hi, Is it possible to access intrinsic ASP objects, such as Request, from a .NET class. Say, I have a .NET library exposed via a COM or COM+ wrapper. Can this library retrieve the request info (basically, server variables exposed via the Request object), when it is invoked from a traditional ASP (not ASP.NET) application? Any ideas? Thanks in advance. Alek
51
10441
by: Joe Van Dyk | last post by:
When you delete a pointer, you should set it to NULL, right? Joe
0
1001
by: Terry Reedy | last post by:
"Jacob Davis" <j.foster.davis@gmail.comwrote in message news:C673A5C7-9971-4CAA-8CEB-3993C3E93F9C@gmail.com... | I read in some earlier messages that an object in Python is only | removed or freed from memory when all references to that object have | been deleted. Is this so? A Python interpreter *may* delete an object when, but only when, it becomes inaccessible from the currently running program. What interpreters do depends on the...
5
505
by: cham | last post by:
Hi, I am working on c++ in a linux system ( Fedora core 4 ), kernel version - 2.6.11-1.1369_FC4 gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 ) In my code i am creating a vector to store pointers of type structure "SAMPLE_TABLE_STRUCT" ( size of this structure is 36 bytes ). I create an instance of structure "SAMPLE_TABLE_STRUCT" using operator "new" and push back into the vector,this is done inside a for loop for
0
8917
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
8761
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
9426
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
6722
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
4525
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...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
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
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2163
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.