pp = new char  只分配一个字节,你说够用吗?改成pp = new char[num],num指定

解决方案 »

  1.   

    strcpy()要求事先给pp分配足够的空间,pp = new char只是给pp分配了一个字符的空间,当执行完strcpy后,你的内存分配已经出错了。改为pp = new char[10];
      

  2.   

    typedef struct d
     {
      char * g;  
      d()
     {
      g=new char [100];
     }
     ~d()
     {
      delete g;
     } } dd; dd *tt=new dd[1];
      strcpy(tt[0].g,"dsfg");
     
      delete []tt;
    我这样可不可 完全释放内存,连 tt[0].g 也 释放
      

  3.   

    把        delete g  写在 ~g(){} 中好,
    还是 把   delete []tt[0].g;
    写在最后的 delete []tt;前面好。
      

  4.   

    asdmusic8
    这个问题我来回答
    你所需要的空间是要向“堆栈空间”申请的。 {heap)
    //////////////////////
    char *pp;
    int arrSize;
    cin>>arrSize;
    pp = new char[arrSize];
    assert(pp);
    strcpy(pp,"asdf"); 
    //do something
    delete []pp;这样就可以,当然用同样方法可以构造动态数组。 (字符串不就是个数组嘛)给分,给分!
      

  5.   

    char *pp;
      pp=new char;
      strcpy(pp,"asdf");
      delete pp;//因为我是new 的,所以我加上了delete ,出错。
      

  6.   

    对不起,前面按错了  char *pp;
      pp=new char[strlen("asdf")];
      strcpy(pp,"asdf");
      delete pp;
    关键是要分配足够的字符数组空间
      

  7.   

    又错了
      char *pp;
      pp=new char[strlen("asdf")+1];
      strcpy(pp,"asdf");
      delete[] pp;
      

  8.   

    typedef struct d
    {
      char * g; 
      d()
    {
      g=new char [100];
    }
    ~d()
    {
      delete g;
    }} dd;dd *tt=new dd[1];
      strcpy(tt[0].g,"dsfg");  delete []tt;
    我这样可不可 完全释放内存,连 tt[0].g 也 释放 把        delete g  写在 ~g(){} 中好,
    还是 把  delete []tt[0].g;
    写在最后的 delete []tt;前面好。