By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,300 Members | 1,377 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,300 IT Pros & Developers. It's quick & easy.

std::vector help!!

P: n/a
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
Share this Question
Share on Google+
13 Replies


P: n/a
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

P: n/a

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

P: n/a

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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.