【问题标题】:wrong memory allocation new char [n]错误的内存分配 new char [n]
【发布时间】:2013-09-07 14:57:12
【问题描述】:

这个程序有什么问题?

#include<iostream>
using namespace std;
void main()
{

    int n = 5;
    char* p = new char [n];
    int i;
    for(i=0;i<n;i++)
    {
        p[i] = 'A'+i;
    }
    cout<<p<<endl;
}

为什么我得到的是 "ABCDExxxx" 而不是 "ABCDE" ? 内存分配有什么问题?

【问题讨论】:

  • 请在 C++ 中使用std::string。如果您需要char*,只需调用c_str() 方法即可。

标签: c++ memory allocation


【解决方案1】:

内存分配没有问题,只是内存永远不会释放。在main 返回之前不要忘记delete [] p;

输出的问题是p 指向的字符串没有终止'\0'。通常,您应该为一个数组分配至少一个比您要放入数组中的字符多的空间,并在最后一个字符之后放置一个'\0'。当然,更好的解决方案是使用std::string,它会为您处理所有这些。

【讨论】:

    【解决方案2】:

    C 字符串需要以空值结尾。再添加一个包含 0 的字节。

    【讨论】:

      【解决方案3】:

      你可以使用newchar分配存储空间,这样就可以了。但是,如果您稍后要使用与空终止字符相关的函数(例如 strlen 或打印出来),那么在为 char* 分配存储时,您需要分配字符数 + 1 更多存储\0。 C 字符串需要以 null 结尾。

      为什么我得到的是 "ABCDExxxx" 而不是 "ABCDE" ?出什么问题了 内存分配?

      您的数据不是以空值结尾的(末尾不包含'\0',因此您正在打印垃圾,直到在其他地方找到字符'\0')。要使其按预期工作,您可以这样做:

      int n = 5;
      char* p = new char [n+1];
      p[n]='\0';
      
      for(i=0;i<n;i++)
      {
          p[i] = 'A'+i;
               ^
              side note: this is OK, however if your p has been pointing to a string 
              literal, i.e. if it was defined as  char*p = "string literal\n";
              then according to section 2.14.5 paragraph 11 of the C++ standard,
              it would invoke undefined behavior:
              The effect of attempting to modify a string literal is undefined.
              so be aware :p !
      
      }
      cout<<p<<endl;
      

      记得用

      释放存储空间

      delete [] p;

      正如其他人评论的那样,改用std::string 可能是一个更好的主意。

      【讨论】:

        【解决方案4】:

        首先,当你已经在使用 C++ 时不要使用 C 风格

        改用std::string

        它有一个成员函数c_str(),有助于使用 C api/functions


        #include<iostream>
        using namespace std;
        int main()
        ^^ main should return int
        {
        
            int n = 5;
           //C string needs to be null terminated, so an extra
            char* p = new char [n+1];
            int i;
            for(i=0;i<n;i++)
            {
                p[i] = 'A'+i;
            }
            p[i] = '\0'; //Insert the null character
            cout<<p<<endl;
        }
        

        【讨论】:

          【解决方案5】:

          您根本没有放置空字符。使用此代码:

          #include<iostream>
          using namespace std;
          void main()
          {
          
              int n = 5;
              char* p = new char [n];
              int i;
              for(i=0;i<n;i++)
              {
                  p[i] = 'A'+i;
              }
              cout<<p<<endl;
          }
          

          当您使用 c++ 时,我推荐使用 std::string

          #include<iostream>
          #include<string>
                  using namespace std;
                  void main()
                  {
          
                      //int n = 5;
                      //char* p = new char [n];
                      string s;
                      int i;
                      for(i=0;i<n;i++)
                      {
                          s.append("/*whatever string you want to append*/");
                      }
                      cout<<s<<endl;
                  }
          

          【讨论】:

          • "/*whatever you want append*/" 格式可能是“whatever you want append”我认为
          【解决方案6】:

          当 endl 遇到 '\0' 时,它会返回,所以如果 char[] 中没有 '\0',直到找到它,它会继续读取内存。

          【讨论】:

          • endl与此无关
          猜你喜欢
          • 2018-08-19
          • 2013-05-14
          • 2012-11-29
          • 2012-05-06
          • 2011-06-08
          • 1970-01-01
          • 1970-01-01
          • 2016-07-31
          • 1970-01-01
          相关资源
          最近更新 更多