• C++ realloc()用法及代码示例


    C++ realloc()用法及代码示例

    C++ 中的realloc() 函数重新分配先前分配但尚未释放的内存块。

    realloc() 函数重新分配先前使用 malloc() 、 calloc() 或 realloc() 函数分配但尚未使用 free() 函数释放的内存。

    如果新大小为零,则返回的值取决于库的实现。它可能会也可能不会返回空指针。


    realloc()原型

    void* realloc(void* ptr, size_t new_size);


    该函数在 头文件中定义。
    参数:

    • ptr :指向要重新分配的内存块的指针。
    • new_size :一个无符号整数值,表示内存块的新大小(以字节为单位)。


    返回:

    realloc() 函数返回:
    • 指向重新分配的内存块开头的指针。
    • 如果分配失败,则为空指针。
    在重新分配内存时,如果内存不足,则不释放旧内存块并返回空指针。
    如果旧指针(即 ptr)为空,则调用 realloc() 与调用 malloc() 函数相同,并将新大小作为其参数。
    有两种可能的重新分配内存的方法。
    • 扩展或收缩同一个块:如果可能,旧指针(即 ptr)指向的内存块被扩展或收缩。内存块的内容保持不变,直到新旧大小中的较小者。如果该区域被扩展,新分配的块的内容是未定义的。
    • 搬到新位置: 分配一个大小为new_size 字节的新内存块。在这种情况下,内存块的内容也保持不变,直到新旧大小中的较小者,如果内存扩大,新分配的块的内容是未定义的。


    示例 1:realloc() 函数如何工作?
     

      1. #include
      2. #include
      3. using namespace std;
      4. int main()
      5. {
      6.     float *ptr, *new_ptr;
      7.     ptr = (float*) malloc(5*sizeof(float));
      8.     if(ptr==NULL)
      9.     {
      10.         cout << "Memory Allocation Failed";
      11.         exit(1);
      12.     }
      13.     /* Initializing memory block */
      14.     for (int i=0; i<5; i++)
      15.     {
      16.         ptr[i] = i*1.5;
      17.     }
      18.     /* reallocating memory */
      19.     new_ptr = (float*) realloc(ptr, 10*sizeof(float));
      20.     if(new_ptr==NULL)
      21.     {
      22.         cout << "Memory Re-allocation Failed";
      23.         exit(1);
      24.     }
      25.    
      26.     /* Initializing re-allocated memory block */
      27.     for (int i=5; i<10; i++)
      28.     {
      29.         new_ptr[i] = i*2.5;
      30.     }
      31.     cout << "Printing Values" << endl;
      32.    
      33.     for (int i=0; i<10; i++)
      34.     {
      35.         cout << new_ptr[i] << endl;
      36.     }
      37.     free(new_ptr);
      38.    
      39.     return 0;
      40. }

    运行程序时,输出将是:

      1. Printing Values
      2. 0
      3. 1.5
      4. 3
      5. 4.5
      6. 6
      7. 12.5
      8. 15
      9. 17.5
      10. 20
      11. 22.5


    示例 2:realloc() 函数,new_size 为零

      1. #include
      2. #include
      3. using namespace std;
      4. int main()
      5. {
      6.     int *ptr, *new_ptr;
      7.     ptr = (int*) malloc(5*sizeof(int));
      8.    
      9.     if(ptr==NULL)
      10.     {
      11.         cout << "Memory Allocation Failed";
      12.         exit(1);
      13.     }
      14.     /* Initializing memory block */
      15.     for (int i=0; i<5; i++)
      16.     {
      17.         ptr[i] = i;
      18.     }
      19.     /* re-allocating memory with size 0 */
      20.     new_ptr = (int*) realloc(ptr, 0);
      21.     if(new_ptr==NULL)
      22.     {
      23.         cout << "Null Pointer";
      24.     }
      25.     else
      26.     {
      27.         cout << "Not a Null Pointer";
      28.     }
      29.     return 0;
      30. }

    运行程序时,输出将是:

    Null Pointer
    示例 3:当 ptr 为 NULL 时的 realloc() 函数

      1. #include
      2. #include
      3. #include
      4. using namespace std;
      5. int main()
      6. {
      7.     char *ptr=NULL, *new_ptr;
      8.     /* reallocating memory, behaves same as malloc(20*sizeof(char)) */
      9.     new_ptr = (char*) realloc(ptr, 50*sizeof(char));
      10.     strcpy(new_ptr, "Welcome to Net188.com");
      11.     cout << new_ptr;
      12.     free(new_ptr);
      13.     return 0;
      14. }

    运行程序时,输出将是:

    Welcome to Net188.com

  • 相关阅读:
    java计算机毕业设计响应式交友网站MyBatis+系统+LW文档+源码+调试部署
    第十九课、QString、string、char *、char[] 之间的转换方法
    全局路线规划导读
    双机热备与数据备份的关系说明一二
    HTTP协议
    ssm+共享图书管理系统 毕业设计-附源码151121
    SQLSmith: Databend 如何利用随机化测试检测 Bug
    JetPack之LifeCycle设计模式与解耦艺术的极致运用
    Selenium WebDriver类的常用属性和方法汇总
    Communication-Efficient Learning of Deep Networks from Decentralized Data
  • 原文地址:https://blog.csdn.net/airen3339/article/details/133788243