C++(7)动态数组

动态数组


序言:

   数组类型变量有三个限制:

    1)数组长度固定不变

    2)在编译时必须知道其长度

    3)数组只能在定义他的语句块内存在

   与数组变量不同:动态分配的数组将一直存在直到程序显示的释放他!


正文:

1、每个程序在执行时都占用一块可用的内存空间,用于存放动态分配的对象,此内存空间称为程序的自由存储区,或堆(heap),在C++中使用new和delete在自由存储区中分配存储空间。

   new表达式返回指向新分配数组的第一个元素的指针:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //数组的维数可以是任意复杂的表达式  
  2. int *pia = new int[10];  

2、在自由存储区中创建的数组对象是没有名字的,只能通过其地址间接访问堆中的对象。


3、动态分配数组时,如果数组类型具有类类型,将调用该类的默认构造函数实现初始化;如果数组元素是内置类型,则无初始化:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. string *psa = new string[10];  
  2. int *pia = new int[10];  

4、可以跟在数组长度后面的一对空圆括号,对数组元素进行值初始化。

   但是对于动态分配的数组,只能初始化为元素的默认值,不能像数组变量一样...

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. int *pi = new int[10]();  

5、const对象的动态数组

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. const int *pci_bad = new const int[10]; //ERROR  
  2. const int *pci_ok = new const int[10]();//OK  

在实际应用中其实没有太大用处...


6、动态空间的释放

    动态分配的内存到最后必须进行释放,否则,内存最终将会耗尽。如果不再需要使用动态创建的数组,则必须显式的将所占用的存储空间返还给程序的自由存储空间!

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. delete []pia;   //不要忘记了[],即使忘记了,编译器也不会发现...  

7、动态数组的使用

    通常就是在编译时无法知道数组的维数,所以才使用动态数组!

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1.    const char *errno = "success";  
  2.    const char *errinfo = "Error: a function declaration must "  
  3.                          "specify a function return type!";  
  4.   
  5.    const char *errTxt;  
  6.    if (errFound)  
  7.     errTxt = errinfo;  
  8. else  
  9.     errTxt = errno;  
  10.   
  11. /* 
  12. *在获得字符串的长度上,必须+1,以便在动态分配内存时 
  13. *预留出存储结束符的空间 
  14. */  
  15. int dimension = strlen(errTxt) + 1;  
  16. char *errMsg = new char[dimension];  
  17. strncpy(errMsg,errTxt,dimension);  

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //P120 习题4.28  
  2. int main()  
  3. {  
  4.     vector<int> ivec;  
  5.     int value;  
  6.   
  7.     while (cin >> value)  
  8.     {  
  9.         ivec.push_back(value);  
  10.     }  
  11.   
  12.     int *arr = new int[ivec.size()];  
  13.     for (size_t index = 0; index != ivec.size(); ++index)  
  14.     {  
  15.         arr[index] = ivec[index];  
  16.         cout << arr[index] << ' ';  
  17.     }  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="white-space:pre">  </span>delete []arr;  
  2.     cout << endl;  
  3. }  

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //4.29测试下面两段程序的执行时间  
  2.     const char *pc = "very long literal string";  
  3.     const size_t len = strlen(pc);  
  4.   
  5.     for (size_t ix = 0; ix != 1000000; ++ix)  
  6.     {  
  7.         char *pc2 = new char[len + 1];  
  8.         strcpy(pc2,pc);  
  9.         if (strcmp(pc2,pc))  
  10.             ;  
  11.         delete []pc2;  
  12.     }  
  13. //execution time : 0.121 s  
  14.   
  15.     const string obj("very long literal string");  
  16.   
  17.     for (size_t sz = 0; sz != 1000000; ++sz)  
  18.     {  
  19.         string str = obj;  
  20.         if (str != obj)  
  21.             ;  
  22.     }  
  23. //execution time : 0.071 s   

可以看出使用string类型的程序执行速度要比用C风格字符串快很多!!!



[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //习题4.30  
  2. //(1)  
  3. int main()  
  4. {  
  5.     const char *str1 = "hello";  
  6.     const char *str2 = "world";  
  7.     const size_t sz = strlen(str1) + strlen(str2) + 2;  
  8.   
  9.     char *strLarg = new char[sz];  
  10.     strncpy(strLarg,str1,strlen(str1) + 1);  
  11.     strncat(strLarg," ",2);  
  12.     strncat(strLarg,str2,sizeof(str2) + 1);  
  13.   
  14.     cout << strLarg << endl;  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1.     delete []strLarg;  
  2.     //execution time : 0.002 s  
  3. }  

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //(2)  
  2. int main()  
  3. {  
  4.     string str1("hello");  
  5.     string str2("world");  
  6.     string strLarg = str1 + " " + str2;  
  7.   
  8.     cout << strLarg << endl;  
  9.     //execution time : 0.002 s  
  10. }  

转载于:https://my.oschina.net/sfsimon/blog/659289

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值