malloc和new的区别---当malloc和string相遇时, 容易出错

 在学习C语言的时候, 我们学了malloc,(用来向系统申请内存空间),后来学习C++的时候, 又学了new(用来创建一个对象)

那么malloc和new有什么区别呢?

        首先, malloc是一个库函数, 返回值是void *形式的, 而new是一个运算符, 返回值类型与new的对象or变量的指针相同。

        其次, new和delete的实现实际上是调用了malloc/free的。

        最后, 介绍最重要的, 对于非内部类型来说, malloc是不能满足要求的, 因为malloc只是分配堆内存(不会调用构造函数),

 而new是分配且内存且在此创建一个对象(会调用构造函数)。string类型的变量在使用前是必须初始化的。


        先看代码:

[cpp]  view plain c
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. class A            
  5. {  
  6. private:  
  7.     int x;  
  8. public:  
  9.     A()  
  10.     {  
  11.         cout << "A" << endl;  
  12.     }  
  13. };  
  14.   
  15. int main()   
  16. {  
  17.     A *q = (A *)malloc(sizeof(A)); // 不会调用构造函数  
  18.   
  19.     return 0;  
  20. }  

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. class A            
  5. {  
  6. private:  
  7.     int x;  
  8. public:  
  9.     A()  
  10.     {  
  11.         cout << "A" << endl;  
  12.     }  
  13. };  
  14.   
  15. int main()   
  16. {  
  17.     A *q = new A; // 会调用构造函数  
  18.   
  19.     return 0;  
  20. }  

      

      我们再看看这个代码有什么问题:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <string>  
  3. using namespace std;  
  4.   
  5. class A            
  6. {  
  7. public:  
  8.     string str;  
  9.   
  10.     A()  
  11.     {  
  12.         cout << "A" << endl;  
  13.     }  
  14. };  
  15.   
  16. int main()   
  17. {  
  18.     A *q = (A *)malloc(sizeof(A));  
  19.     q->str = "hello"// 运行error. 没有构造函数被调用, 所以, str根本就没有被初始化(string的构造函数没有被调用), 所以不能赋值  
  20.   
  21.     return 0;  
  22. }  

      调用malloc只是分配空间而已, 下面程序可以说明这点, 确实没有产生任何对象:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4.   
  5. class I            
  6. {  
  7. public:  
  8.     int x;  
  9.   
  10.     I()  
  11.     {  
  12.         cout << "I" << endl;  
  13.     }  
  14. };  
  15.   
  16.   
  17. class A            
  18. {  
  19. public:  
  20.     I i;  
  21.   
  22.     A()  
  23.     {  
  24.         cout << "A" << endl;  
  25.     }  
  26. };  
  27.   
  28. int main()   
  29. {  
  30.     A *q = (A *)malloc(sizeof(A));  
  31.     q->i.x = 100; // 可以看到, 确实没有任何构造函数被调用, 所以没有任何对象产生  
  32.   
  33.     return 0;  
  34. }  

     下面看看new:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <string>  
  3. using namespace std;  
  4.   
  5. class A            
  6. {  
  7. public:  
  8.     string str;  
  9.   
  10.     A()  
  11.     {  
  12.         cout << "A" << endl;  
  13.     }  
  14. };  
  15.   
  16. int main()   
  17. {  
  18.     A *q = new A; // 分配了堆空间, 且A的构造函数会被调用, string的构造函数也会被调用  
  19.     q->str = "hello";  // ok  
  20.     return 0;  
  21. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值