• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
我是张洪铭我是熊博士
时光静好,与君同;细水长流,与君语
博客园    首页    新随笔    联系   管理    订阅  订阅

C++ new delete 一维数组 二维数组 三维数组

h-----------------------------
#include "newandmalloc.h"
#include <iostream>
using namespace std;

newAndMalloc::newAndMalloc()
{
    cout<<"construct newAndMalloc..."<<endl;
}

newAndMalloc::~newAndMalloc()
{
    cout<<"destruct newAndMalloc..."<<endl;
    deletePInt();
    deletePPInt();
    deletePPPInt();
}

void newAndMalloc::newPInt()
{
    cout<<"new m_pInt..."<<endl;

    m_pInt = new int[m_size]; //开辟空间

    for (int i = 0; i < m_size; i++)  //赋值
    {
        m_pInt[i] = m_size - i;
    }
}

void newAndMalloc::newPPInt()
{
    cout<<"new m_ppInt..."<<endl;

    m_ppInt = new int*[m_size]; //开辟一维的空间
    for (int i = 0; i < m_size; i++)
    {
        m_ppInt[i] = new int[m_size]; //开辟二维的控件
    }

    for (int i = 0; i < m_size; i++)  //赋值
    {
        for (int j = 0; j < m_size; j++)
        {
            m_ppInt[i][j] = i + j;
        }
    }

}

void newAndMalloc::newPPPInt()
{
    cout<<"new m_pppInt..."<<endl;

    m_pppInt = new int**[m_size];  //开辟一维的空间
    for(int i = 0; i < m_size; i++)
    {
        m_pppInt[i] = new int*[m_size];  //开辟二维的空间
        for (int j = 0; j < m_size; j++)
        {
            m_pppInt[i][j] = new int[m_size];  //开辟三维的空间
        }
    }

    for (int i = 0; i < m_size; i++) //赋值
    {
        for (int j = 0; j < m_size; j++)
        {
            for (int h = 0; h < m_size; h++)
            {
                m_pppInt[i][j][h] = i + j + h;
            }
        }
    }
}

void newAndMalloc::printPInt()
{
    cout<<"print m_pInt..."<<endl;

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

void newAndMalloc::printPPInt()
{
    cout<<"print m_ppInt..."<<endl;

    for (int i = 0; i < m_size; i++)
    {
        for (int j = 0; j < m_size; j++)
        {
            cout<<m_ppInt[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<endl;
}

void newAndMalloc::printPPPInt()
{
    cout<<"print m_pppInt..."<<endl;

    for (int i = 0; i < m_size; i++)
    {
        for (int j = 0; j < m_size; j++)
        {
            for (int h = 0; h < m_size; h++)
            {
                cout<<m_pppInt[i][j][h]<<" ";
            }
            cout<<endl;
        }
        cout<<endl;
    }
    cout<<endl;
}

void newAndMalloc::deletePInt()
{
    cout<<"delete m_pInt..."<<endl;
    delete[] m_pInt;
}

void newAndMalloc::deletePPInt()
{
    cout<<"delete m_ppInt..."<<endl;
    for (int i = 0; i < m_size; i++)
    {
        delete[] m_ppInt[i];
    }
    delete[] m_ppInt;
}

void newAndMalloc::deletePPPInt()
{
    cout<<"delete m_pppInt..."<<endl;
    for (int i = 0; i < m_size; i++)
    {
        for (int j = 0; j < m_size; j++)
        {
            delete[] m_pppInt[i][j];
        }
        delete[] m_pppInt[i];
    }
    delete[] m_pppInt;
}

testClass::testClass()
{
    cout<<"construct testClass..."<<endl;
}

testClass::~testClass()
{
    cout<<"destruct testClass..."<<endl;
}

cpp--------------------------------
#include "newandmalloc.h"
#include <iostream>
using namespace std;

newAndMalloc::newAndMalloc()
{
    cout<<"construct newAndMalloc..."<<endl;
}

newAndMalloc::~newAndMalloc()
{
    cout<<"destruct newAndMalloc..."<<endl;
    deletePInt();
    deletePPInt();
    deletePPPInt();
}

void newAndMalloc::newPInt()
{
    cout<<"new m_pInt..."<<endl;

    m_pInt = new int[m_size]; //开辟空间

    for (int i = 0; i < m_size; i++)  //赋值
    {
        m_pInt[i] = m_size - i;
    }
}

void newAndMalloc::newPPInt()
{
    cout<<"new m_ppInt..."<<endl;

    m_ppInt = new int*[m_size]; //开辟一维的空间
    for (int i = 0; i < m_size; i++)
    {
        m_ppInt[i] = new int[m_size]; //开辟二维的控件
    }

    for (int i = 0; i < m_size; i++)  //赋值
    {
        for (int j = 0; j < m_size; j++)
        {
            m_ppInt[i][j] = i + j;
        }
    }

}

void newAndMalloc::newPPPInt()
{
    cout<<"new m_pppInt..."<<endl;

    m_pppInt = new int**[m_size];  //开辟一维的空间
    for(int i = 0; i < m_size; i++)
    {
        m_pppInt[i] = new int*[m_size];  //开辟二维的空间
        for (int j = 0; j < m_size; j++)
        {
            m_pppInt[i][j] = new int[m_size];  //开辟三维的空间
        }
    }

    for (int i = 0; i < m_size; i++) //赋值
    {
        for (int j = 0; j < m_size; j++)
        {
            for (int h = 0; h < m_size; h++)
            {
                m_pppInt[i][j][h] = i + j + h;
            }
        }
    }
}

void newAndMalloc::printPInt()
{
    cout<<"print m_pInt..."<<endl;

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

void newAndMalloc::printPPInt()
{
    cout<<"print m_ppInt..."<<endl;

    for (int i = 0; i < m_size; i++)
    {
        for (int j = 0; j < m_size; j++)
        {
            cout<<m_ppInt[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<endl;
}

void newAndMalloc::printPPPInt()
{
    cout<<"print m_pppInt..."<<endl;

    for (int i = 0; i < m_size; i++)
    {
        for (int j = 0; j < m_size; j++)
        {
            for (int h = 0; h < m_size; h++)
            {
                cout<<m_pppInt[i][j][h]<<" ";
            }
            cout<<endl;
        }
        cout<<endl;
    }
    cout<<endl;
}

void newAndMalloc::deletePInt()
{
    cout<<"delete m_pInt..."<<endl;
    delete[] m_pInt;
}

void newAndMalloc::deletePPInt()
{
    cout<<"delete m_ppInt..."<<endl;
    for (int i = 0; i < m_size; i++)
    {
        delete[] m_ppInt[i];
    }
    delete[] m_ppInt;
}

void newAndMalloc::deletePPPInt()
{
    cout<<"delete m_pppInt..."<<endl;
    for (int i = 0; i < m_size; i++)
    {
        for (int j = 0; j < m_size; j++)
        {
            delete[] m_pppInt[i][j];
        }
        delete[] m_pppInt[i];
    }
    delete[] m_pppInt;
}

testClass::testClass()
{
    cout<<"construct testClass..."<<endl;
}

testClass::~testClass()
{
    cout<<"destruct testClass..."<<endl;
}


main-------------------------------
#include <iostream>
#include <stdio.h>
#include "newandmalloc.h"

int main(int argc, char *argv[])
{
    newAndMalloc *nam = nullptr;
    nam = new newAndMalloc();
    nam->newPInt();
    nam->newPPInt();
    nam->newPPPInt();

    nam->printPInt();
    nam->printPPInt();
    nam->printPPPInt();

    delete nam;
    return 0;
}


打印输出------------------------
construct newAndMalloc...
new m_pInt...
new m_ppInt...
new m_pppInt...
print m_pInt...
10 9 8 7 6 5 4 3 2 1 
print m_ppInt...
0 1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 10 
2 3 4 5 6 7 8 9 10 11 
3 4 5 6 7 8 9 10 11 12 
4 5 6 7 8 9 10 11 12 13 
5 6 7 8 9 10 11 12 13 14 
6 7 8 9 10 11 12 13 14 15 
7 8 9 10 11 12 13 14 15 16 
8 9 10 11 12 13 14 15 16 17 
9 10 11 12 13 14 15 16 17 18 

print m_pppInt...
0 1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 10 
2 3 4 5 6 7 8 9 10 11 
3 4 5 6 7 8 9 10 11 12 
4 5 6 7 8 9 10 11 12 13 
5 6 7 8 9 10 11 12 13 14 
6 7 8 9 10 11 12 13 14 15 
7 8 9 10 11 12 13 14 15 16 
8 9 10 11 12 13 14 15 16 17 
9 10 11 12 13 14 15 16 17 18 

1 2 3 4 5 6 7 8 9 10 
2 3 4 5 6 7 8 9 10 11 
3 4 5 6 7 8 9 10 11 12 
4 5 6 7 8 9 10 11 12 13 
5 6 7 8 9 10 11 12 13 14 
6 7 8 9 10 11 12 13 14 15 
7 8 9 10 11 12 13 14 15 16 
8 9 10 11 12 13 14 15 16 17 
9 10 11 12 13 14 15 16 17 18 
10 11 12 13 14 15 16 17 18 19 

2 3 4 5 6 7 8 9 10 11 
3 4 5 6 7 8 9 10 11 12 
4 5 6 7 8 9 10 11 12 13 
5 6 7 8 9 10 11 12 13 14 
6 7 8 9 10 11 12 13 14 15 
7 8 9 10 11 12 13 14 15 16 
8 9 10 11 12 13 14 15 16 17 
9 10 11 12 13 14 15 16 17 18 
10 11 12 13 14 15 16 17 18 19 
11 12 13 14 15 16 17 18 19 20 

3 4 5 6 7 8 9 10 11 12 
4 5 6 7 8 9 10 11 12 13 
5 6 7 8 9 10 11 12 13 14 
6 7 8 9 10 11 12 13 14 15 
7 8 9 10 11 12 13 14 15 16 
8 9 10 11 12 13 14 15 16 17 
9 10 11 12 13 14 15 16 17 18 
10 11 12 13 14 15 16 17 18 19 
11 12 13 14 15 16 17 18 19 20 
12 13 14 15 16 17 18 19 20 21 

4 5 6 7 8 9 10 11 12 13 
5 6 7 8 9 10 11 12 13 14 
6 7 8 9 10 11 12 13 14 15 
7 8 9 10 11 12 13 14 15 16 
8 9 10 11 12 13 14 15 16 17 
9 10 11 12 13 14 15 16 17 18 
10 11 12 13 14 15 16 17 18 19 
11 12 13 14 15 16 17 18 19 20 
12 13 14 15 16 17 18 19 20 21 
13 14 15 16 17 18 19 20 21 22 

5 6 7 8 9 10 11 12 13 14 
6 7 8 9 10 11 12 13 14 15 
7 8 9 10 11 12 13 14 15 16 
8 9 10 11 12 13 14 15 16 17 
9 10 11 12 13 14 15 16 17 18 
10 11 12 13 14 15 16 17 18 19 
11 12 13 14 15 16 17 18 19 20 
12 13 14 15 16 17 18 19 20 21 
13 14 15 16 17 18 19 20 21 22 
14 15 16 17 18 19 20 21 22 23 

6 7 8 9 10 11 12 13 14 15 
7 8 9 10 11 12 13 14 15 16 
8 9 10 11 12 13 14 15 16 17 
9 10 11 12 13 14 15 16 17 18 
10 11 12 13 14 15 16 17 18 19 
11 12 13 14 15 16 17 18 19 20 
12 13 14 15 16 17 18 19 20 21 
13 14 15 16 17 18 19 20 21 22 
14 15 16 17 18 19 20 21 22 23 
15 16 17 18 19 20 21 22 23 24 

7 8 9 10 11 12 13 14 15 16 
8 9 10 11 12 13 14 15 16 17 
9 10 11 12 13 14 15 16 17 18 
10 11 12 13 14 15 16 17 18 19 
11 12 13 14 15 16 17 18 19 20 
12 13 14 15 16 17 18 19 20 21 
13 14 15 16 17 18 19 20 21 22 
14 15 16 17 18 19 20 21 22 23 
15 16 17 18 19 20 21 22 23 24 
16 17 18 19 20 21 22 23 24 25 

8 9 10 11 12 13 14 15 16 17 
9 10 11 12 13 14 15 16 17 18 
10 11 12 13 14 15 16 17 18 19 
11 12 13 14 15 16 17 18 19 20 
12 13 14 15 16 17 18 19 20 21 
13 14 15 16 17 18 19 20 21 22 
14 15 16 17 18 19 20 21 22 23 
15 16 17 18 19 20 21 22 23 24 
16 17 18 19 20 21 22 23 24 25 
17 18 19 20 21 22 23 24 25 26 

9 10 11 12 13 14 15 16 17 18 
10 11 12 13 14 15 16 17 18 19 
11 12 13 14 15 16 17 18 19 20 
12 13 14 15 16 17 18 19 20 21 
13 14 15 16 17 18 19 20 21 22 
14 15 16 17 18 19 20 21 22 23 
15 16 17 18 19 20 21 22 23 24 
16 17 18 19 20 21 22 23 24 25 
17 18 19 20 21 22 23 24 25 26 
18 19 20 21 22 23 24 25 26 27 


destruct newAndMalloc...
delete m_pInt...
delete m_ppInt...
delete m_pppInt...

 

posted @ 2019-06-17 18:24  我是张洪铭我是熊博士  阅读(2357)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3