Izak Smit (ZA Cont) on Mon, 03 Mar 2003 14:08:18 +0200 (SAST)


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

[Linux dev] Re: STL and pointers


Hi Pieter.

Well the answer to your problem is:

1. YOU MUST LOOP THROUGHT THE STL VECTOR AND CALL THE delete on each pointer

Here's some code to show the result if you call call_back, and if you call
the delete on the vector.

ALWAYS remember.

The compiler will not see anything wrong if you call a delete on a NULL
POINTER!!!!! It's good practice :)...

Hope this helps.

#include <vector>
#include <iostream>

using namespace std;

vector<int *> myINT;
vector<int *>::iterator myINT_itr;

int main(int argc, char* argv[])
{
	int* i2;

	i2 = new int;
	*i2 = 100;

	cout << "ADDING NEW POINTER VALUE of 100 into vecotr" << endl;
	myINT.push_back( i2 );
	cout << "CALLING pop_back" << endl;
	myINT.back( );

	myINT.pop_back( );
	cout << "VALUE IN STL VECTOR: " << *myINT[0] << endl;

	cout << "Calling delete" << endl;
	delete myINT[0];
	
	cout << "VALUE AFTER DELETE: " << *myINT[0] << endl;

	return 0;
}





 -----Original Message-----
From: 	Pieter Louw [mailto:pieter@xxxxxxxxxx] 
Sent:	03 March 2003 01:31 PM
To:	dev@xxxxxxxxxxxx
Subject:	[Linux dev] STL and pointers

Hi there,

When you have a STL container with pointers, do you still need to delete the
pointers when you erase an item, or does the container handle the memory
management?

Example:

vector <Item*> items;

items->push_back(new Item());

//do stuff

items->pop_back();

/*does this delete the pointer, or do you have to delete it yourself like:
		delete items.back();
		items->pop_back();
*/

Thanks in advance

Pieter



***************************************************************************************

This message contains information intended solely for the addressee,
which is confidential or private in nature and subject to legal privilege.
If you are not the intended recipient, you may not peruse, use,
disseminate, distribute or copy this message or any file attached to this
message. Any such unauthorised use is prohibited and may be unlawful. If
you have received this message in error, please notify the sender
immediately by e-mail, facsimile or telephone and thereafter delete the
original message from your machine. 
 
Furthermore, the information contained in this message, and any
attachments thereto, is for information purposes only and may contain the
personal views and opinions of the author, which are not necessarily the
views and opinions of Dimension Data (South Africa) (Proprietary) Limited
or its subsidiaries and associated companies ("Dimension Data"). Dimension
Data therefore does not accept liability for any claims, loss or damages
of whatsoever nature, arising as a result of the reliance on such
information by anyone. 
 
Whilst all reasonable steps are taken to ensure the accuracy and
integrity of information transmitted electronically and to preserve the
confidentiality thereof, Dimension Data accepts no liability or
responsibility whatsoever if information or data is, for whatsoever
reason, incorrect, corrupted or does not reach its intended destination. 

*************************************************************************************