std::vector help!!

L

linux_bp

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.
 
D

Daniel T.

"linux_bp said:
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.
 
S

Sunil Varma

linux_bp said:
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.
 
V

vikram_p_nayak

linux_bp said:
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.
 
V

Victor Bazarov

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
 
H

Howard

Victor Bazarov said:
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
 
D

Daniel T.

"Yong Hu said:
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.
 
R

Richard Herring

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().
 
Y

Yong Hu

Richard said:
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
 
T

Thomas Tutone

Yong said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,266
Latest member
DavidaAlla

Latest Threads

Top