Re: Memory Leaks - Can you help me find them in ths snippet
"nmehring@gmail.com" <nmehring@gmail.com> wrote:
I am writing some c++ code that interacts with a native C library and
have to do some dynamic memory allocation to support it. I am getting
memory leaks and I think this piece of code is the culprit. Can
anyone tell me what I might be doing wrong?
Yes. Remove the mallocs and deletes. Use std::vectors instead.
vector< vector< char > > columns( lColumnCount,
SE_QUALIFIED_COLUMN_LEN );
//put the data in the char**
for ( int column_index = 0; column_index < lColumnCount;
column_index++)
{
strcpy (&columns[column_index].front(),
CStringColumnName[column_index]);
}
//do some stuff with columns
//.......
Or maybe better:
vector< string > columns( lColumnCount );
//put the data in the char**
for ( int column_index = 0; column_index < lColumnCount;
column_index++)
{
columns[column_index] = CStringColumnName[column_index];
}
//do some stuff with columns
//.......
Or maybe better still:
vector< string > columns;
copy( CStringColumnName, CStringColumnName + lColumnCount,
back_inserter( columns ) );
//do some stuff with columns
//.......