Program crashes when leaving a c++ function…What do you think it is?-Collection of common programming errors
I have a c++ code, I use MSC9 to compile it. It keeps crashing randomly. For example it crashes if it is called from Perl using “ but it does not crash when It is called from command line or from Ultimate++.
I mean calling it from perl eg. f.exe arg1 arg2 arg3
Stack trace does not show much. Tracing the program line by line proved that the program fails at the end when returning…
So it is like that
int funcname()
{
return 0; 10)
{
defsize = s;
}
m_rgArray = new value_type[defsize];
allocated = defsize;
fill(0);
}
pvector(pvector const& pv)
: m_rgArray(new value_type[pv.allocated]),
m_nIndex(pv.m_nIndex),allocated(pv.allocated)
{
std::copy(pv.m_rgArray, pv.m_rgArray + pv.allocated, m_rgArray);
}
pvector operator=(const pvector &pv)
{
m_rgArray=new value_type[pv.allocated];
m_nIndex=pv.m_nIndex;
allocated=pv.allocated;
std::copy(pv.m_rgArray, pv.m_rgArray + pv.allocated, m_rgArray);
return *this;
}
void clear()
{
m_nIndex=0;
fill(allocated);
}
~pvector() {
delete []m_rgArray;
}
size_type size() const
{ // return length of sequence
return m_nIndex;
}
size_type max_size() const
{ // return maximum possible length of sequence
return 0;
}
void fill(size_type si)
{
for (size_type i = si;i 0 ? false : true);
}
iterator begin()
{ // return iterator for beginning of mutable sequence
return iterator(&m_rgArray[0]);
}
const_iterator begin() const
{
return const_iterator(&m_rgArray[0]);
}
iterator end()
{ // return iterator for end of mutable sequence
return iterator(&m_rgArray[m_nIndex]);
}
const_iterator end() const
{
return const_iterator(&m_rgArray[m_nIndex]);
}
reference operator[](size_type i)
{
if (m_nIndex>i)
{
return m_rgArray[i];
}
else if (i >= allocated)
{
resize(i * 2);
}
m_nIndex = i + 1;
return m_rgArray[i];
}
void resize(size_type s)
{
value_type * m_rgArray2;
size_type old_allocated = allocated;
allocated = s;
m_rgArray2 = new value_type[allocated];
//if (allocated>m_nIndex)
//{
// m_nIndex=allocated;
// }
// cout std::vector::capacity())
{
std::vector::reserve(need * 2);
std::vector::resize(need);
}
else if(need > std::vector::size())
{
std::vector::resize(need);
}
return std::vector::at(n);
}
reference operator[](size_type n)
{
return at(n);
}
};
I tested it with GCC 4.1.2 on Linux, so hopefully it’ll compile on Windows too.
Edit: Here’s a new version that doesn’t inherit, since it seems experts agree it’s not a good idea (I learned something new, yay). You can implement the rest of the methods you want and just pass-through to m_vector:
template
class pvector
{
public:
typedef StoredType& reference;
typedef int size_type;
reference at(size_type n)
{
int need = n+1;
if(need >= m_vector.capacity())
{
m_vector.reserve(need * 2);
m_vector.resize(need);
}
else if(need >= m_vector.size())
{
m_vector.resize(need);
}
return m_vector.at(n);
}
reference operator[](size_type n)
{
return at(n);
}
size_type capacity() { return m_vector.capacity(); }
size_type size() { return m_vector.size(); }
private:
std::vector m_vector;
};