#include <iostream>
class MSG
{
public:
std::string title;
std::string msg;
};
void c_way( int cLinesMax )
{
// Allocate Memory
MSG *pmsg = (MSG *)malloc( cLinesMax * sizeof(MSG) );
// Set MSG Values
for( int i = 0; i < cLinesMax; i++ )
{
pmsg[i].title = "C TITLE ";
pmsg[i].title += std::to_string( i+1 );
pmsg[i].msg = "C MESSAGE ";
pmsg[i].msg += std::to_string( i+1 );
}
// Print MSGs
for( int i = 0; i < cLinesMax; i++ )
std::cout << pmsg[i].title << " : " << pmsg[i].msg << std::endl;
// Delete Allocated Memory (deallocate)
free( pmsg );
}
void cpp_way( int cLinesMax )
{
// Allocate Memory
MSG *pmsg = new MSG[cLinesMax];
// Set MSG Values
for( int i = 0; i < cLinesMax; i++ )
{
pmsg[i].title = "CPP TITLE ";
pmsg[i].title += std::to_string( i+1 );
pmsg[i].msg = "CPP MESSAGE ";
pmsg[i].msg += std::to_string( i+1 );
}
// Print MSGs
for( int i = 0; i < cLinesMax; i++ )
std::cout << pmsg[i].title << " : " << pmsg[i].msg << std::endl;
// Delete Allocated Memory (deallocate)
// delete: deallocates one MSG instance
// delete[]: deallocates all MSG instances in array
delete[] pmsg;
}
int main( int argc, char *argv[] )
{
int cLinesMax = 0;
std::cout << "set array size: ";
std::cin >> cLinesMax;
std::cout << std::endl;
c_way( cLinesMax );
cpp_way( cLinesMax );
return 0;
}