动态分配/撤销内存的运算符new和delete

例:用new开辟空间存放一个结构体变量,delete运算符撤销所开辟的空间

#include<iostream>
#include<string.h>
using namespace std;

struct Student
{
    char name[10];
    int num;
    char sex;
};

int main(void)
{
    Student *p;
    p = new Student;
    strcpy(p-> name, "Wang Yun");
    p-> num = 10123;
    p-> sex = 'M';
    cout << p-> name << " " << p-> num << " " << p-> sex << endl;
    delete p;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Eider1998/article/details/88376393