我编写了以下示例程序,但由于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;}我不知道如何使它工作。有什么想法甚至可以使用malloc()和std::strings吗?
谢谢,博达·西多(Boda Cydo)。
慕村9548890
相关分类