Hello
What is "minimal framework" (necessary methods) of object, which I will store in STL <vector>
?
For my assumptions:
#include <vector>
#include <cstring>
using namespace std;
class Doit {
private:
char *a;
public:
Doit(){a=(char*)malloc(10);}
~Doit(){free(a);}
};
int main(){
vector<Doit> v(10);
}
gives
*** glibc detected *** ./a.out: double free or corruption (fasttop): 0x0804b008 ***
Aborted
and in valgrind:
malloc/free: 2 allocs, 12 frees, 50 bytes allocated.
UPDATE:
Minimal methods for such object are: (based on sbi answer)
class DoIt{
private:
char *a;
public:
DoIt() { a=new char[10]; }
~DoIt() { delete[] a; }
DoIt(const DoIt& rhs) { a=new char[10]; std::copy(rhs.a,rhs.a+10,a); }
DoIt& operator=(const DoIt& rhs) { DoIt tmp(rhs); swap(tmp); return *this;}
void swap(DoIt& rhs) { std::swap(a,rhs.a); }
};
Thanks, sbi, http://stackoverflow.com/users/140719/sbi