C
Christian Chrismann
Hi,
I've a runtime problem with STL vectors.
Here is the simplified version of the code:
template <class T> class A {
...
private:
vector<T*> myvector;
typename vector<T*>::itarator mIt;
...
};
template <class T>
T* A<T>::function1()
{
typename vector<T*>::iterator it( mIt );
if ( it != myvector.end() )
return function2();
return NULL;
}
template <class T>
T* A<T>::function2()
{
T *temp = *mIt;
if ( mIt != ( --( myvector.end()) ) )
++mIt;
else // line1
mIt = myvector.end(); // line 2
return temp;
}
When the gcc-compiled program is run, I get the error message:
*** glibc detected *** double free or corruption (fasttop): 0x0819b210 ***
When "line1" and "line2" are removed (the else-part of function2),
the program works fine.
What might be the problem?
And another question:
What happens exactly in line "T *temp = *mIt;" ?
I assume that a new variable of type "pointer to T" is defined and
initialized with the address to an element of type T that is stored in
the STL vector. The class T constructor is never invoked here, right? And
why do I actually need to define the type of the pointer (here pointer to
class T)? I mean, the size of the address is always the same. Or is
it important for type checking?
Regards,
Chris
I've a runtime problem with STL vectors.
Here is the simplified version of the code:
template <class T> class A {
...
private:
vector<T*> myvector;
typename vector<T*>::itarator mIt;
...
};
template <class T>
T* A<T>::function1()
{
typename vector<T*>::iterator it( mIt );
if ( it != myvector.end() )
return function2();
return NULL;
}
template <class T>
T* A<T>::function2()
{
T *temp = *mIt;
if ( mIt != ( --( myvector.end()) ) )
++mIt;
else // line1
mIt = myvector.end(); // line 2
return temp;
}
When the gcc-compiled program is run, I get the error message:
*** glibc detected *** double free or corruption (fasttop): 0x0819b210 ***
When "line1" and "line2" are removed (the else-part of function2),
the program works fine.
What might be the problem?
And another question:
What happens exactly in line "T *temp = *mIt;" ?
I assume that a new variable of type "pointer to T" is defined and
initialized with the address to an element of type T that is stored in
the STL vector. The class T constructor is never invoked here, right? And
why do I actually need to define the type of the pointer (here pointer to
class T)? I mean, the size of the address is always the same. Or is
it important for type checking?
Regards,
Chris