我编写了以下示例程序,但它与segfault崩溃.问题似乎是在结构中使用malloc和std::strings.
#include <iostream>
#include <string>
#include <cstdlib>
struct example {
std::string data;
};
int main() {
example *ex = (example *)malloc(sizeof(*ex));
ex->data = "hello world";
std::cout << ex->data << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何让它发挥作用.任何想法,如果它甚至可以使用malloc()和std::strings?
谢谢,Boda Cydo.