simon said:
struct sFileData
{
char*sSomeString1;
char*sSomeString2;
int iSomeNum1;
int iSomeNum2;
sFileData(){ NullAll(); }
~sFileData(){ CleanAll(); }
sFileData(const sFileData&sfd) { NullAll(); *this = sfd; }
const sFileData& operator=( const sFileData &sfd ) {
if( this != &sfd)
{
CleanAll();
iSomeNum1 = sfd.iSomeNum1;
iSomeNum2 = sfd.iSomeNum2;
if( sfd.sSomeString1 ){
sSomeString1 = new char[strlen(sfd.sSomeString1)+1];
strcpy( sSomeString1, sfd.sSomeString1 );
}
if( sfd.sSomeString2 ){
sSomeString2 = new char[strlen(sfd.sSomeString2)+1];
strcpy( sSomeString2, sfd.sSomeString2 );
}
}
return *this;
}
void CleanAll(){
if(sSomeString1) delete [] sSomeString1;
if(sSomeString2) delete [] sSomeString2;
}
BTW, these if() tests are not needed, as delete[] is defined
to have no effect if the pointer is null.
void NullAll(){
sSomeString1 = 0;
sSomeString2 = 0;
iSomeNum1 = 0;
iSomeNum2 = 0;
}
std::vector< sFileData, std::allocator<sFileData> > address_;
It appears (from discussion elsewhere in the thread) that your
problem is excessive memory chewing by copying strings around
all over the place.
In an ideal world, your compiler would optimise to avoid this,
but it looks like we are in the real world

I would suggest
trying some hand-optimisations in this case.
(Before you do this, try compiling in release mode instead of
debug mode, that may help).
sFileData sfd;
sfd.iSomeNum1 = 1;
sfd.iSomeNum2 = 2;
sfd.sSomeString1 = new char[5];
sfd.sSomeString2 = new char[8];
strcpy( sfd.sSomeString1, "Helo" );
strcpy( sfd.sSomeString2, "Goodbye" );
address_.push_back(sfd);
Try creating the object directly in the vector.. then the strings
only have to be allocated once:
address_.resize( address_.size() + 1 );
sFileData &sfd = address_.end()[-1];
sfd.iSomeNum1 = 1;
sfd.iSomeNum2 = 2;
sfd.sSomeString1 = new char[5];
sfd.sSomeString2 = new char[8];
strcpy( sfd.sSomeString1, "Helo" );
strcpy( sfd.sSomeString2, "Goodbye" );
You could avoid vector copies (using lots of time and memory)
by reserving all of the memory at the start:
address_.reserve(100000);
In your real program, where you don't know the exact size in
advance, you might like to estimate this number from the file size,
or something. Or, preferably,use a deque or a list, which don't
require huge reallocations when you add new members.
Another thing you might do (if the strings aren't going to be
manipulated too much by the rest of your program) is to allocate
them both at once (since your OS probably has a minimum allocation
size anyway, we will be halving memory usage):
sfd.sSomeString1 = new char[13];
sfd.sSomeString2 = sfd.sSomeString1 + 5;
(and modify your destructors and operator= accordingly).