You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++中使用new操作string对象的内存分配错误分析及相关疑问解答

问题分析:C++ std::string 内存处理与代码错误解释

Let's walk through your code step by step to break down exactly why it's failing, plus clarify what's happening with memory allocation here.

为什么代码会报错?

Your code has two critical issues that will cause compilation errors (and catastrophic undefined behavior if you somehow forced it to run):

  1. Type Mismatch & Invalid Assignment
    std::string is a C++ class built to manage string data automatically. When you write string str = new char[30];, you're trying to assign a raw char* pointer (returned by new[]) directly to a std::string object. C++ doesn't allow this implicit conversion—std::string can be constructed from a const char* (like a string literal), but not from a dynamic char* pointer. The compiler will throw an error here because no valid assignment operator or constructor exists for this combination.

  2. Incorrect Use of delete[]
    Even if you hacked around the type mismatch, delete[] str; is completely invalid. delete[] is only meant to free memory allocated by new[] via a raw pointer. str is a std::string object, not a pointer to dynamically allocated chars. Using delete[] on a non-pointer type triggers undefined behavior—this would crash your program at runtime, if it even compiled.

内存分配的实际情况

To answer your question: No, your code does NOT allocate 90 bytes for str, and it won't even compile to reach the point of runtime memory allocation. Here's the breakdown:

  • The new char[30] expression does allocate 30 bytes of heap memory, but since you're trying to assign this pointer to a std::string (which isn't allowed), you lose track of this pointer immediately. This creates a memory leak—those 30 bytes are never freed.
  • The new char[60] line would similarly allocate 60 bytes, but again, the assignment to std::string is invalid. Even if it worked, you'd leak both the 30 and 60 byte blocks, because std::string doesn't take ownership of raw pointers assigned to it. It manages its own internal memory separately.

正确的做法

If you want to work with std::string and manage its internal memory, let the class handle it for you—this is the whole point of using std::string instead of raw char* arrays:

  • To pre-allocate space for future characters (without initializing them), use reserve():
    std::string str;
    str.reserve(30); // Pre-allocates space for 30 characters internally
    
  • To create a string with a specific number of initialized characters:
    std::string str(30, '\0'); // Creates a string of 30 null characters
    

std::string will automatically handle memory allocation, reallocation, and deallocation when the object goes out of scope—you never need to use new or delete with it directly.

内容的提问来源于stack exchange,提问作者masou2312

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 23:48:11