• C和指针 第11章 动态内存分配 11.3 calloc和realloc


    11.3 calloc和realloc
        另外还有两个内存分配函数:calloc和realloc。它们的原型如下所示:
        void *calloc( size_t num_elements, size_t element_size );
        void realloc( void *ptr, size_t new_size );
        calloc也用于分配内存。malloc和calloc之间的主要区别是后者在返回指向内存的指针之前把它初始化为0。这个初始化常常能带来方便,但如果你的程序只是想把一些值存储到数组中,那么这个初始化过程纯属浪费时间。calloc和malloc之间另一个较小的区别是它们请求内存数量的方式不同。calloc的参数包括所需元素的数量和每个元素的字节数。根据这些值,它能够计算出总共需要分配的内存。
        realloc函数用于修改一个原先已经分配的内存块的大小。使用这个函数,可以使一块内存扩大或缩小。如果它用于扩大一个内存块,那么这块内存原先的内容依然保留,新增的内存添加到原先内存的后面,新内存并未以任何方式进行初始化。如果它用于缩小一个内存块,该内存块尾部的部分内存便被拿掉,剩余部分内存的原先内容依然保留。
        如果原先的内存块无法改变大小,realloc将分配另一块正确大小的内存,并把原来的那块内存的内容复制到新的块上。因此,在使用realloc之后,就不能再使用指向旧内存的指针,而是应该改用realloc所返回的新指针。
        如果,如果realloc函数的第一个参数是NULL,那么它的行为就和malloc一模一样。 

    /*
    ** calloc和realloc。 
    */
    #include <stdio.h>
    #include <stdlib.h>

    int main( void ){
        int *pm, *pm2, *pmt;
        int *pc, *pc2, *pct;
        int *pr, *pr2, *prt;
        int *p, *p2, *p3, *p4;
        int size;
        
        size = 8;
        /*
        ** allocate size * sizeof(int) bytes memory by using malloc function.
        ** the allocated memory doesn't be initialized.
        */
        pm = (int *)malloc( size * sizeof(int) );
        pmt = pm;
        pm2 = pm + size;
        printf( "print the elements by using malloc function:\n" );
        while( pm < pm2 ){
            printf( "%d ", *pm );
            ++pm;
        }
        printf( "\n" );
        /*
        ** free dynamic memory.
        */
        /*
        free( pmt );
        */
        /*
        ** allocate size * sizeof(int) bytes memory by using calloc function.
        ** the allocated memory can be initialized to zero.
        */
        pc = (int *)calloc( size, sizeof(int) );
        pct = pc;
        pc2 = pc + size;
        printf( "print the elements by using calloc function:\n" ); 
        while( pc < pc2 ){
            printf( "%d ", *pc );
            ++pc;
        }
        printf( "\n" );
        /*
        ** free dynamic memory.
        */
        /*
        free( pct );
        */
        
        /*
        ** allocate size * sizeof(int) bytes memory by using realloc function.
        ** the allocated memory doesn't be initialized.
        */
        pr = (int *)realloc( NULL, size * sizeof(int) );
        prt = pr;
        pr2 = pr + size;
        printf( "print the elements by using realloc function:\n" );
        while( pr < pr2 ){
            printf( "%d ", *pr );
            ++pr;
        }
        printf( "\n" );
        printf( "pmt = %p, pct = %p, prt = %p\n", pmt, pct, prt );
        printf( "enlarge the memory pointed to by pmt:\n" );
        p = (int *)realloc( pmt , 100 * sizeof(int) );
        printf( "after p = realloc( pmt , 100 * sizeof(int) ), p = %p, pmt = %p\n", p, pmt );
        printf( "print the preceding 20 elements by using realloc function to enlarge memory:\n" );
        p2 = p + 20;
        while( p < p2 ){
            printf( "%d ", *p );
            ++p;
        }
        printf( "\n" );
        printf( "lessen the memory pointed to by pct:\n" );
        p3 = (int *)realloc( pct , 4 * sizeof(int) );
        printf( "after p = realloc( pct , 100 * sizeof(int) ), p3 = %p, pct = %p\n", p3, pct );
        printf( "print the preceding 20 elements by using realloc function to lessen memory:\n" );
        p4 = p3 + 4;
        while( p3 < p4 ){
            printf( "%d ", *p3 );
            ++p3;
        }
        printf( "\n" );
        /*
        ** free dynamic memory.
        */
        free( prt );
        free( p );
        free( p3 );
        
        return EXIT_SUCCESS;
    }
    /* 输出:

    */ 

  • 相关阅读:
    Oracle数据库:刷题错题本,数据库的各种概念
    Windows 7 联合 eNSP模拟器 配置Snmp
    Pandas中的iloc函数; 查看.pt文件内容
    1 Supervised Machine Learning Regression and Classification
    【笔记】关于寄存器的一些理解
    opencv dnn模块 示例(19) 目标检测 object_detection 之 yolox
    基于TCP与UDP网络编程
    typescript之神奇的infer - 推断任何你想知道的类型
    谷歌评价:从服务器检索信息时出错。DF-DFERH-01
    【python自动化】03. pynput库自动操作键鼠(保姆级代码注释)
  • 原文地址:https://blog.csdn.net/weixin_40186813/article/details/125463228