473,387 Members | 1,453 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,387 software developers and data experts.

std::vector help!!

I have an stl vector array which stores the pointers to objects.
To delete the array i am using:

std::vector<*foo> bar;
....
for (vector<*foo>::iterator itr = bar.begin(); itr != bar.end(); )
{
delete itr;
itr = NULL;
}

Do i need to clear the bar array after this using bar.clear();
if i do not use bar.clear() what could be the implications.

Jun 26 '06 #1
13 1840
In article <11**********************@c74g2000cwc.googlegroups .com>,
"linux_bp" <ri**********@gmail.com> wrote:
I have an stl vector array which stores the pointers to objects.
To delete the array i am using:

std::vector<*foo> bar;
...
for (vector<*foo>::iterator itr = bar.begin(); itr != bar.end(); )
{
delete itr;
itr = NULL;
}

Do i need to clear the bar array after this using bar.clear();
You don't have to, but you might want to.
if i do not use bar.clear() what could be the implications.


Assuming bar.size() > 0 you will have a number of null pointers in your
program, dereferencing any of them will cause undefined behavior, and
adding a new pointer to an object with push_back will not get rid of any
of them.
Jun 26 '06 #2

linux_bp wrote:
I have an stl vector array which stores the pointers to objects.
To delete the array i am using:

std::vector<*foo> bar;
...
for (vector<*foo>::iterator itr = bar.begin(); itr != bar.end(); )
{
delete itr;
itr = NULL;
}

Do i need to clear the bar array after this using bar.clear();
if i do not use bar.clear() what could be the implications.


You need to erase the pointer that you have delete'd from the bar.
This will help from dereferencing NULL pointers.

Jun 26 '06 #3

linux_bp wrote:
I have an stl vector array which stores the pointers to objects.
To delete the array i am using:

std::vector<*foo> bar;
...
for (vector<*foo>::iterator itr = bar.begin(); itr != bar.end(); )
{
delete itr;
itr = NULL;
}


Shouldnt this be
delete (*itr);
?
I guess you are deleting the objects being referred to by the vector
elements. Maybe I am wrong.

Jun 26 '06 #4
vi************@yahoo.com wrote:
linux_bp wrote:
I have an stl vector array which stores the pointers to objects.
To delete the array i am using:

std::vector<*foo> bar;
...
for (vector<*foo>::iterator itr = bar.begin(); itr != bar.end(); )
{
delete itr;
itr = NULL;
}


Shouldnt this be
delete (*itr);
?
I guess you are deleting the objects being referred to by the vector
elements. Maybe I am wrong.


It's really hard to conclude anything (although you're probably right)
since the code presented is not real code. For example, 'vector<*foo>'
is a definite syntax error.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 26 '06 #5

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:e7**********@news.datemas.de...
vi************@yahoo.com wrote:
linux_bp wrote:
I have an stl vector array which stores the pointers to objects.
To delete the array i am using:

std::vector<*foo> bar;
...
for (vector<*foo>::iterator itr = bar.begin(); itr != bar.end(); )
{
delete itr;
itr = NULL;
}


Shouldnt this be
delete (*itr);
?
I guess you are deleting the objects being referred to by the vector
elements. Maybe I am wrong.


It's really hard to conclude anything (although you're probably right)
since the code presented is not real code. For example, 'vector<*foo>'
is a definite syntax error.


Not to mention the fact that such a loop would loop forever, since itr is
set to NULL on the first iteration, is never changed by the loop statement,
and would thus never equal bar.end().

-Howard
Jun 26 '06 #6
It is ok if the following lines are used;

delete (*itr);
*itr = NULL;

Yong Hu

Howard wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:e7**********@news.datemas.de...
vi************@yahoo.com wrote:
linux_bp wrote:
I have an stl vector array which stores the pointers to objects.
To delete the array i am using:

std::vector<*foo> bar;
...
for (vector<*foo>::iterator itr = bar.begin(); itr != bar.end(); )
{
delete itr;
itr = NULL;
}

Shouldnt this be
delete (*itr);
?
I guess you are deleting the objects being referred to by the vector
elements. Maybe I am wrong.


It's really hard to conclude anything (although you're probably right)
since the code presented is not real code. For example, 'vector<*foo>'
is a definite syntax error.


Not to mention the fact that such a loop would loop forever, since itr is
set to NULL on the first iteration, is never changed by the loop statement,
and would thus never equal bar.end().

-Howard


Jun 27 '06 #7
In article <11**********************@75g2000cwc.googlegroups. com>,
"Yong Hu" <yh*******@gmail.com> wrote:
It is ok if the following lines are used;

delete (*itr);
*itr = NULL;


It depends on what type 'itr' is of course. Here is some code from
Stroustrup:

template < typename T >
T* delete_ptr( T* p )
{
delete p;
return 0;
}
The above can be used in std::transform, for example:

void purge( vector<foo*>& vec )
{
transform( vec.begin(), vec.end(), vec.begin(), delete_ptr<foo> );
}

The above will delete every 'foo' held in the vector and set the pointer
to 0.
Jun 27 '06 #8
In message <Oh*******************@bgtnsc05-news.ops.worldnet.att.net>,
Howard <al*****@hotmail.com> writes

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:e7**********@news.datemas.de...
vi************@yahoo.com wrote:
linux_bp wrote:
I have an stl vector array which stores the pointers to objects.
To delete the array i am using:

std::vector<*foo> bar;
...
for (vector<*foo>::iterator itr = bar.begin(); itr != bar.end(); )
{
delete itr;
itr = NULL;
}

Shouldnt this be
delete (*itr);
?
I guess you are deleting the objects being referred to by the vector
elements. Maybe I am wrong.
It's really hard to conclude anything (although you're probably right)
since the code presented is not real code. For example, 'vector<*foo>'
is a definite syntax error.


Not to mention the fact that such a loop would loop forever, since itr is
set to NULL on the first iteration,


And that line will probably only compile at all if vector<T>::iterator
happens to be implemented as T*, which is not necessarily the case.
is never changed by the loop statement,
and would thus never equal bar.end().


--
Richard Herring
Jun 27 '06 #9

Richard Herring wrote:
In message <Oh*******************@bgtnsc05-news.ops.worldnet.att.net>,
Howard <al*****@hotmail.com> writes

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:e7**********@news.datemas.de...
vi************@yahoo.com wrote:
linux_bp wrote:
> I have an stl vector array which stores the pointers to objects.
> To delete the array i am using:
>
> std::vector<*foo> bar;
> ...
> for (vector<*foo>::iterator itr = bar.begin(); itr != bar.end(); )
> {
> delete itr;
> itr = NULL;
> }

Shouldnt this be
delete (*itr);
?
I guess you are deleting the objects being referred to by the vector
elements. Maybe I am wrong.

It's really hard to conclude anything (although you're probably right)
since the code presented is not real code. For example, 'vector<*foo>'
is a definite syntax error.


Not to mention the fact that such a loop would loop forever, since itr is
set to NULL on the first iteration,


And that line will probably only compile at all if vector<T>::iterator
happens to be implemented as T*, which is not necessarily the case.


The vector<T>::iterator is a type defined as T* for sure.

This is how the iterator is defined in vector:

template<class _Ty, class _A = allocator<_Ty> >
class vector {
public:
........
typedef _A::pointer _Tptr;
typedef _Tptr iterator;
........
}

and the following shows how the _A::pointer is defined in allocator<T>:

template<class T>
class allocator {
...............
typedef T *pointer;
typedef const T *const_pointer;
typedef T& reference;
....................
allocator();
allocator<T>& operator=(const allocator<T>);
pointer allocate(size_type n, const void *hint);
void deallocate(pointer p, size_type n);
void construct(pointer p, const T& val);
void destroy(pointer p);
size_type max_size() const;
};

Yong Hu
is never changed by the loop statement,
and would thus never equal bar.end().


--
Richard Herring


Jun 28 '06 #10
Yong Hu wrote:
[...]
The vector<T>::iterator is a type defined as T* for sure.
As soon as you can find a quote from the Standard to support that claim,
do share it with us.
[..]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 28 '06 #11
Yong Hu wrote:
And that line will probably only compile at all if vector<T>::iterator
happens to be implemented as T*, which is not necessarily the case.


The vector<T>::iterator is a type defined as T* for sure.


As Victor B. says, you are mistaken. In YOUR implementation of the
Standard Library that may well be the case, but you should not assume
it is true of ALL implementations. In mine (gcc 3.4.6),
std::vector<T>::iterator is a class, not T*. The same is true, I
believe, of Dinkumware's current implementation, which is used in
Microsoft's C++ compiler.

Best regards,

Tom

Jun 28 '06 #12
In message <11**********************@p79g2000cwp.googlegroups .com>, Yong
Hu <yh*******@gmail.com> writes

Richard Herring wrote:
In message <Oh*******************@bgtnsc05-news.ops.worldnet.att.net>,
Howard <al*****@hotmail.com> writes
>
>"Victor Bazarov" <v.********@comAcast.net> wrote in message
>news:e7**********@news.datemas.de...
>> vi************@yahoo.com wrote:
>>> linux_bp wrote:
>>>> I have an stl vector array which stores the pointers to objects.
>>>> To delete the array i am using:
>>>>
>>>> std::vector<*foo> bar;
>>>> ...
>>>> for (vector<*foo>::iterator itr = bar.begin(); itr != bar.end(); )
>>>> {
>>>> delete itr;
>>>> itr = NULL;
>>>> }
>>>
>>> Shouldnt this be
>>> delete (*itr);
>>> ?
>>> I guess you are deleting the objects being referred to by the vector
>>> elements. Maybe I am wrong.
>>
>> It's really hard to conclude anything (although you're probably right)
>> since the code presented is not real code. For example, 'vector<*foo>'
>> is a definite syntax error.
>>
>
>Not to mention the fact that such a loop would loop forever, since itr is
>set to NULL on the first iteration,
And that line will probably only compile at all if vector<T>::iterator
happens to be implemented as T*, which is not necessarily the case.


The vector<T>::iterator is a type defined as T* for sure.


Not "for sure" at all. See the other replies.
This is how the iterator is defined in vector:


In one particular library implementation.

--
Richard Herring
Jun 29 '06 #13
You guys are right. Thanks!!

Yong Hu

Richard Herring wrote:
In message <11**********************@p79g2000cwp.googlegroups .com>, Yong
Hu <yh*******@gmail.com> writes

Richard Herring wrote:
In message <Oh*******************@bgtnsc05-news.ops.worldnet.att.net>,
Howard <al*****@hotmail.com> writes
>
>"Victor Bazarov" <v.********@comAcast.net> wrote in message
>news:e7**********@news.datemas.de...
>> vi************@yahoo.com wrote:
>>> linux_bp wrote:
>>>> I have an stl vector array which stores the pointers to objects.
>>>> To delete the array i am using:
>>>>
>>>> std::vector<*foo> bar;
>>>> ...
>>>> for (vector<*foo>::iterator itr = bar.begin(); itr != bar.end(); )
>>>> {
>>>> delete itr;
>>>> itr = NULL;
>>>> }
>>>
>>> Shouldnt this be
>>> delete (*itr);
>>> ?
>>> I guess you are deleting the objects being referred to by the vector
>>> elements. Maybe I am wrong.
>>
>> It's really hard to conclude anything (although you're probably right)
>> since the code presented is not real code. For example, 'vector<*foo>'
>> is a definite syntax error.
>>
>
>Not to mention the fact that such a loop would loop forever, since itr is
>set to NULL on the first iteration,

And that line will probably only compile at all if vector<T>::iterator
happens to be implemented as T*, which is not necessarily the case.


The vector<T>::iterator is a type defined as T* for sure.


Not "for sure" at all. See the other replies.
This is how the iterator is defined in vector:


In one particular library implementation.

--
Richard Herring


Jun 29 '06 #14

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

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
5
by: aaragon | last post by:
Hello everybody, I appreciate your taking the time to take a look at this example. I need some help to start the design of an application. To that purpose I'm using policy-based design. The...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.