19.new和delete用于数组

程序1:

//2022年9月20日22:06:27
#include <iostream>
#pragma warning(disable:4996)
using namespace std;

//1.new创建基础类型的数组
void test01()
{
    //申请基础数据类型的数组
    int * pInt = new int [10] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};//不推荐
    for (int i = 0; i < 10; i++)
    {
        pInt[i] = i + 1;
    }

    for (int i = 0; i < 10; i++)
    {
        cout << pInt[i] << " ";
    }
    cout << endl;

    char * pChar = new char[64];
    memset(pChar, 0, 64);
    strcpy(pChar, "小花");
    cout << pChar << endl;

    //注意:如果new时有中括号,那么delete时也要有中括号
    delete [] pInt;
    delete [] pChar;
}
int main()
{
    test01();
    system("pause");
    return EXIT_SUCCESS;
}

输出结果:

1 2 3 4 5 6 7 8 9 10
小花
请按任意键继续. . .
posted @   CodeMagicianT  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示