C# と VB.NET の質問掲示板

わんくま同盟

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト


(過去ログ 78 を表示中)
■46246 / )  new演算子の使用
□投稿者/ ★★★ (1回)-(2010/01/28(Thu) 21:06:18)

分類:[C/C++] 

初歩の質問で大変申し訳ないのですが、どうしてもよくわからないので質問させていただきます。
C++でのnew演算子の使用に対しての質問です。
パターン1でもnewで連続してメモリ確保を行っているので、パターン2でも問題ないのではと思って
ためしてみたのですがパターン1は問題ないのですがパターン2では異常終了してしまいます。
この違いは一体なんななのでしょうか?
それと、そもそもnewを連続して使用するのは可能なのでしょうか?

// パターン1
int main( int argc, char *argv )
{
char *str = "This is a test\n";
char *temp = new char('\0');

temp = new char[strlen(str)+1];
strcpy( temp, str );
cout << temp;

delete[] temp;
}
// パターン2
int main( int argc, char *argv )
{
char *str1 = "This is a test ";
char *str2 = "C++ Programing\n"
char *temp = new char[strlen(str)+1];

strcpy( temp, str1 );
temp = new char[strlen(str2)+1];
strcat( temp, str2 );
cout << temp;

delete[] temp;
}

返信 編集キー/


管理者用

- Child Tree -