小编Lyn*_*ing的帖子

C++中动态分配的错误

#include <iostream>
using namespace std;

char *CopyOf(const char *str);

void main(void)
{
    char *hello = CopyOf("Hello\n");
    cout << hello;
    delete [] hello;
    system("pause");
}

char *CopyOf(const char *str)
{
    char *copy = new char(strlen(str) + 1);
    strcpy(copy, str);
    return copy;
}
Run Code Online (Sandbox Code Playgroud)

程序运行到delete语句时发生我的错误.有什么建议吗?非常感谢你.

c++ memory-management

-7
推荐指数
1
解决办法
73
查看次数

标签 统计

c++ ×1

memory-management ×1