手机版
你好,游客 登录 注册
背景:
阅读新闻

C/C++之动态内存分配比较

[日期:2011-11-15] 来源:Linux社区  作者:wallwind [字体:大 中 小]

3、C++ 动态内存分配:

 

使用 "new" 和 "delete"

如下示例:文件: AllocNewTest.cpp

  1. // memory.cpp : 定义控制台应用程序的入口点。   
  2. //2011/10/13 by wallwind   
  3.   
  4. #include "stdafx.h"   
  5. #include <iostream>   
  6. using namespace std;  
  7.   
  8. class CCC  
  9.   
  10. {  
  11. public:  
  12.     CCC(){cout<<"CCC()"<<endl;};  
  13.     ~CCC(){cout<<"~CCC()"<<endl;};  
  14.     CCC(int);  
  15.     CCC(int, double);  
  16.     int ii;  
  17.     double dd;  
  18. };  
  19. CCC::CCC(int _ii)  
  20. : ii(_ii)  
  21. {  
  22. };  
  23.   
  24. CCC::CCC(int _ii, double _dd)  
  25. : ii(_ii), dd(_dd)  
  26. {  
  27. };  
  28.   
  29.   
  30.   
  31. int _tmain(int argc, _TCHAR* argv[])  
  32. {  
  33.     CCC *cc1 = new CCC(4, 5.5);   // Pointer. Contructor called.   
  34.     CCC *cc2 = new CCC[5];        // Pointer to an array of objects.   
  35.     CCC &cc3 = *new CCC;          // Reference   
  36.     CCC **c4 = new CCC * [5];     // Array of pointers to pointers   
  37.     cc1->ii   = 5;  
  38.     cc2[3].ii = 6;  
  39.   
  40.     cc3.ii    = 7;  
  41.     c4[0]     = new CCC(8);  
  42.     c4[1]     = new CCC(9);  
  43.   
  44.     cout << cc1->ii   << endl;  
  45.     cout << cc2[3].ii << endl;  
  46.     cout << cc3.ii    << endl;  
  47.     cout << c4[0]->ii << endl;  
  48.     cout << c4[1]->ii << endl;  
  49.     delete cc1;  
  50.     delete [] cc2;  
  51.     delete & cc3;  
  52.   
  53.     delete [] c4[0];   // First delete pointer content   
  54.     delete [] c4[1];  
  55.     delete [] c4;      // then delete array of pointers   
  56.     return 0;  
  57.   
  58. }  

注意:

  •  new CCC(3) 为integer 成员的单个对象分配的内存并且设置integer 成员成3.
  •  new CCC[3] 创建三个对象,并没有初始化变量

结果如图:

图四

4、C 函数返回一个指针vs C++函数返回的是一个副本:

C 函数返回一个指针 

  1. // memory.cpp : 定义控制台应用程序的入口点。   
  2. //2011/10/13 by wallwind   
  3.   
  4. #include "stdafx.h"   
  5. #include <stdio.h>   
  6. #include <stdlib.h>   
  7. #include <iostream>   
  8. using namespace std;  
  9.   
  10. char *fnConvert(int _ii)  
  11. {  
  12.     char *str =(char*)malloc(10);  /* Return 10 character string */  
  13.     if(str == NULL)  
  14.         fprintf(stderr,"Error: Memory allocation failed.\n");  
  15.     sprintf(str, "%d", _ii);  
  16.     return str;  
  17. }  
  18.   
  19. int _tmain(int argc, _TCHAR* argv[])  
  20. {  
  21.     char *s1=fnConvert( 34567 );  
  22.     printf("%s\n", s1);  
  23.     free(s1);  
  24.     return 0;  
  25.   
  26. }  

运行结果:

图五

C++ 函数返回的是一个副本:

  1. // memory.cpp : 定义控制台应用程序的入口点。   
  2. //2011/10/13 by wallwind   
  3.   
  4. #include "stdafx.h"   
  5. #include <iostream>   
  6. #include <sstream>   
  7. #include <string>   
  8. using namespace std;  
  9.   
  10. string fnConvert(int _ii)  
  11.   
  12. {  
  13.     ostringstream ost;  
  14.     ost << _ii;  
  15.     return ost.str();  
  16. }  
  17.   
  18.   
  19. int _tmain(int argc, _TCHAR* argv[])  
  20. {  
  21.      cout << fnConvert( 34567 ) << endl;  
  22.     return 0;  
  23.   
  24. }  

运行结果

图六

注意:c++的STL string类的赋值构造函数是被用来返回一个值的副本,也就是当我们离开了函数的时候,变量 "ost",不在作用域了,但是所赋复制的内容是有效的。不要返回"return ost.str().c_str()" 作为他的指针,他是不在作用域的内一旦程序离开了函数,并且数据会丢失。

linux
相关资讯       C++ 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款