[過去ログ] C++相談室 part134 (1002レス)
前次1-
抽出解除 レス栞

このスレッドは過去ログ倉庫に格納されています。
次スレ検索 歴削→次スレ 栞削→次スレ 過去ログメニュー
182
(1): はちみつ餃子◆8X2XSCHEME (ワッチョイ bf6f-T3WU) [sage] 2018/02/17(土) 18:45:39 ID:uBckbaXI0(1/2) AAS
>>162
162(5): デフォルトの名無しさん (マクド FF6b-xgEu) [sage] 2018/02/17(土) 09:43:48 ID:Lqib7/b9F(1) AAS
以下のようにconst char*型の文字列でchar*型の変数を初期化したいときに
strcpyやstrlenを書かずにstd::stringを利用して短くかけないかと思うのですが
msvcだとエラーにならず、clang, gccだとdeleteでセグメンテーション違反になります。
どうすればより良い感じになるでしょうか?const_castはできれば使いたくないです。

#include <string.h>
#include <string>
#include <iostream>
int main()
{
const char* asdf = "asdf";
// char* psz2 = new char[strlen(asdf)+1];
// strcpy(psz2, asdf);
std::string *psz = new std::string(asdf);
char* psz2 = &(*psz)[0];

std::cout << psz2 << std::endl;
delete psz2;
return 0;
}
ぶっちゃけた話、 strcpy が一番短いと思う。
183
(1): デフォルトの名無しさん [] 2018/02/17(土) 18:59:50 AAS
>>182
strdup という答えが出てるのに何言ってるの?
前次1-
スレ情報 赤レス抽出 画像レス抽出 歴の未読スレ AAサムネイル

ぬこの手 ぬこTOP 0.054s