【发布时间】:2011-03-25 14:51:04
【问题描述】:
我编写了以下示例程序,但它因段错误而崩溃。问题似乎在于在结构中使用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,有什么想法吗?
谢谢,博达·赛多。
【问题讨论】: