new/delete, malloc/free的用法及其例证

new/delete关键字用法
与malloc,free的区别:
malloc,free是函数,需要包含头文件<malloc.h>、<cstdlib>/<stdlib.h>,而new/delete是操作符,不需要包含任何头文件;


int *t_p1 = (int *)malloc(sizeof(int));
*t_p1 = 100;
cout << "*t_p1 : " << *t_P1 << endl;
free(t_p1);
t_p1 = NULL;

      int  *t_p3  =   new   int( 88);         //分配4个字节的空间同时赋值
      int  *t_p4  =   new   int[ 10];     //分配40个字节的空间

#include <iostream>
#include <cstdlib>                  //包含malloc头文件或者malloc.h
using namespace std;
 
  
int main()
{
#if 0
    int i, n;
    char *buffer;
 
  
    cout << "How long do you want the string ?\n" ;
    cin >> i;
 
  
    buffer = (char *) malloc (i + 1);
    if (buffer == NULL )
        exit(EXIT_SUCCESS);
    for (n = 0; n < i; n++)
    {
        buffer[n] = rand()%26+'a';
    }
    buffer[i] = '\0';
 
  
    cout << "Random string: " << buffer << endl;
    free(buffer);
#endif
 
  
    int *t_p1 = (int *)malloc(sizeof(int));
    *t_p1 = 100;
 
  
    cout << "*t_p1 = " << *t_p1 << "\t t_p1 = " << t_p1 << endl;
    free(t_p1);
     t_p1 = NULL;
     cout << "===========================================\n";
 
  
     int *t_p2 = new int;
     *t_p2 = 20;
     cout << "*t_p2 = " << *t_p2 << "\t t_p2 = " << t_p2 << endl;
     delete(t_p2);
     t_p2 = NULL;
     cout << "===========================================\n";
 
  
     int *t_p3 = new int(88);       //赋值
 
  
     cout << "*t_p3 = " << *t_p3 << "\t t_p3 = " << t_p3 << endl;
     delete t_p3;
     t_p3 = NULL;
 
  
     cout << "sizeof(int(88)) = " << sizeof(int(88)) << endl;
 
  
     cout << "===========================================\n";
     int *t_p4 = new int[10];   //分配10个int 空间 40 bytes
 
  
     for (int i = 0; i < 10; i++)
     {
         t_p4[i] = i + 1;
         cout << " " << t_p4[i];
     }
     cout << endl;
     cout << "t_p4 = " << t_p4 << endl;
     cout << "sizeof(int[10]) = " <<sizeof(int[10]) << endl;
     delete [] t_p4;
     t_p4 = NULL;
 
  
     cout << "================pointer array1===========================\n";
     int *t_array1[3];
 
  
     for (int i = 0; i < 3; i++)
     {
         t_array1[i] = new int [4];
 
  
         for (int j = 0; j < 4; j++)
         {
             t_array1[i][j] = i + j + 1;
         }
     }
 
  
     for (int i = 0; i < 3; i++)
     {
         for (int j = 0; j < 4; j++)
         {
             cout << t_array1[i][j] << " ";
         }
         cout << endl;
     }
 
  
     //释放资源
     for (int i = 0; i < 3; i++)
     {
         delete [] t_array1[i];
         t_array1[i] = NULL;
     }
     cout << sizeof(t_array1) << "\t"<< t_array1 <<endl;
 
  
     cout << "=================pointer array2=======================\n";
     char *t_array2[5];
 
  
     for (int i = 0; i < 5; i++)
     {
         t_array2[i] = new char[6];
 
  
         for (int j = 0; j < 6; j++)
         {
             t_array2[i][j] = i + j + 'a';
         }
     }
 
  
     for (int i = 0; i < 5; i++)
     {
         for (int j = 0; j < 6; j++)
         {
             cout << t_array2[i][j] << " ";
         }
         cout << endl;
     }
 
  
     for (int i = 0; i < 5; i++)
     {
         delete [] t_array2[i];
         t_array2[i] = NULL;
     }
 
  
     cout << "=================array pointer=======================\n";
     int (*t_arrar3)[3] = new int [4][3];
 
  
     for (int i = 0; i < 4; i++)
     {
         for (int j = 0; j < 3; j++)
         {
             t_arrar3[i][j] = i + j + 1;
         }
     }
 
  
     for (int i = 0; i < 4; i++)
     {
         for (int j = 0; j < 3; j++)
         {
             cout << "t_array[" << i << "][" << j << "] = " << t_arrar3[i][j] << " ";
         }
         cout << endl;
     }
 
  
     //释放资源
     delete [] t_arrar3;
     t_arrar3 = NULL;
 
  
 
  
    cout << "Hello World!" << endl;
    return 0;
}
 
  
 
  
运行结果如下:



着重理解int *a[3]和int (*a)[3]区别:前者是确定一维,后者是确定二维;

===================================================================================================
在类中的用法:


#ifndef STUDENT_H
#define STUDENT_H
 
 
#include <iostream>
#include <string>
 
 
using namespace std;
 
 
class student
{
public:
    student()
    {
        cout << "student()" << endl;
        m_name = "Jack";
        m_ID = "001";
        m_age = 20;
    }
 
 
    student(const string &t_name, const string &t_ID, int t_age)
        :m_name(t_name), m_ID(t_ID), m_age(t_age)
    {
        cout << "student(...)" << endl;
    }
 
 
    ~student()
    {
        cout << "~student()" << endl;
    }
 
 
    void print()
    {
        cout << "student name : " << m_name << " ID : " << m_ID << " age : " << m_age << endl;
    }
 
 
private:
 
 
    string m_name;
    string m_ID;
    int m_age;
};
 
 
#endif // STUDENT_H
 
 

#include <iostream>
#include "student.h"
 
 
using namespace std;
 
 
int main()
{
    //new一个类的对象,会自动调用该类的相应的构造函数
    student *t_student1 = new student("Mack","003",30);
    student  t_student3;
 
 
    t_student1->print();
    t_student3.print();
 
 
 
 
    //delete一个类的对象,会自动调用该类的析构函数
    delete t_student1;
    t_student1 = NULL;
 
 
    cout << "111111111111111111" << endl;
 
 
    student *t_student2 = new student[3]{student("Jim","005", 20), student("Lucy","006",30)}; //-std = c++11
 
 
    for(int i = 0; i < 3; i++)
    {
        t_student2[i].print();
        cout << endl;
    }
 
 
    delete [] t_student2;
    t_student2 = NULL;
 
 
    cout << "2222222222222222222" << endl;
 
 
 
 
    student *t_array1[3];
 
 
    for(int i = 0; i < 3; i++)      //分配六个,调用六次构造函数
    {
        t_array1[i] = new student[2];
    }
 
 
    //释放资源
    for(int i = 0; i < 3; i++)
    {
        delete [] t_array1[i];
        t_array1[i] = NULL;
    }
 
 
    cout << "3333333333333333333333" << endl;
 
 
    student (*t_array2)[3] = new student[2][3];
 
 
    //释放资源
    delete [] t_array2;
    t_array2 = NULL;
 
 
 
 
    cout << "Hello World!" << endl;
    return 0;
}

 
 
 
 

New/delete与类的构造函数,析构函数的关系;

//new 一个类的对象,会自动调用该类的相应的构造函数
Person *t_p1 = new Person;
t_p1->print();
//delete一个类的对象,会自动调用该类的相应的析构函数
delete t_p1;
t_p1 = NULL;

Person *t_p2 = new Person[2];     //无参
Person *t_p2 = new Person[2]{student(..),student(..)};     //有参std=c11

Person (*t_p3)[ 3] = new Person[2][ 3];  //数组指针 
for(int i = 0; i < 2; i++)
{
     for (int j = 0; j < 3; j++)
     {
        cout << "t_p3["  << i << "]["<< j << "] = " << t_p3[i][j];
     }
     cout << endl;
}
delete [ .] t_p3;
t_p3 =NULL;

Person *t_p4[3];     //指针数组
for(int i = 0; i < 3; i++)
{
    t_p4[i] = new Person[2];
}

for(int i = 0; i < 3; i++)     
{
delete [.] t_p4[i];
t_p4[i] = NULL;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值