0162デフォルトの名無しさん (マクド FF6b-xgEu)
2018/02/17(土) 09:43:48.64ID:Lqib7/b9Fstrcpyや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;
}