首页 > 其他 > 详细

malloc 和new , free 和delete的区别

时间:2020-12-07 09:40:12      阅读:45      评论:0      收藏:0      [点我收藏+]

#include <iostream>
using namespace std;
class user
{
  public:
    int age;
    int number;
  void test()
  {
    printf("this is user test function \t");
  }
  virtual void test1()
  {
    printf("this is user test111 function \t");
  }
};
int main()
{
  //user *u=new user();
  user *u=(user *)malloc(sizeof(user));
  u->age=90;
  u->number=100;
  u->test();
  //u->test1();
  free(u);
  cout << "Hello World";
  return 0;
}

-------------------------------------------------------结果----------------------

this is user test function Hello World

 

打开注解test1

----------------------------------------------

run: line 1: 3 Segmentation fault (core dumped) LD_LIBRARY_PATH=/usr/local/gcc-9.2.0/lib64 ./a.out

Exited with error status 139

 

malloc 和new , free 和delete的区别

原文:https://www.cnblogs.com/bruce1992/p/14095471.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!