如果我使用
const char * str = "Hello";
在运行时不需要进行内存分配/释放
如果我使用
const std::string str = "Hello";
在string类中是否会使用new/malloc进行分配?我在汇编中找到了它,但我不擅长阅读。
如果答案是“是的,会有malloc/new”,为什么?为什么只能通过内部const char指针传递并在需要编辑字符串时进行实际内存分配?
char str[] = "Hello";
或者const std::string str = "Hello";
甚至是static const std::string str = "Hello"
。 - Neil Kirk