Since you are using Qt and making a single atomic use of the UTF8 strings I think that:
char *acsUserName = new char[ Qstr1.length() + 1 ]; // + 1 for zero in the end of string
memcpy( acsUserName, Qstr1.toUtf8().constData() );
char *acsPassword = new char[ Qstr2.length() + 1 ]; // + 1 for zero in the end of string
memcpy( acsPassword, Qstr2.toUtf8().constData() );
foo( acsUserName, acsPassword );
delete []acsUserName;
delete []acsPassword;
char *acsUserName = new char[ Qstr1.length() + 1 ]; // + 1 for zero in the end of string
memcpy( acsUserName, Qstr1.toUtf8().constData() );
char *acsPassword = new char[ Qstr2.length() + 1 ]; // + 1 for zero in the end of string
memcpy( acsPassword, Qstr2.toUtf8().constData() );
foo( acsUserName, acsPassword );
delete []acsUserName;
delete []acsPassword;
To copy to clipboard, switch view to plain text mode
is probably more elegantly put without intervening temporary variables and manual memory shuffle:
foo( Qstr1.toUtf8().constData(), Qstr2.toUtf8().constData() );
foo( Qstr1.toUtf8().constData(), Qstr2.toUtf8().constData() );
To copy to clipboard, switch view to plain text mode
The temporary QByteArray object scope extends to the end of the foo() call. If you need to make multiple uses of the UTF8 string:
foo( username.constData(), password.constData() );
bar( username.constData() );
QByteArray username = QStr1.toUtf8();
QByteArray password = QStr2.toUtf8();
foo( username.constData(), password.constData() );
bar( username.constData() );
To copy to clipboard, switch view to plain text mode
and let QByteArray worry about NUL termination, memory allocation, and necessary size.
Bookmarks