• realloc函数实现数组动态增长


    realloc函数实现数组动态增长
    realloc函数有两种机制:

    #include<stdio.h>
    #include<stdlib.h>

    int main(){
           
           int *p = ( int*)calloc(10, sizeof( int)); 
           printf( "%p",p);
           for ( int i = 0; i < 10; i++)
           {
                  *(p + i) = i;
           }
           
           p = ( int*)realloc(p,60); // 在原来的内存后面补增60个字节
           for ( int i = 10; i < 25; i++)
           {
                  *(p + i) = i;
           }

           system( "pause");
           return 0;
    }

    1. 直接在原来的内存后面补接内存(在内存足够时)


    2.当原内存后面的内存不足的时候,就重新找到一块内存,将原内存中的数据复制过去,原内存清空

    #include<stdio.h>
    #include<stdlib.h>

    int main(){
           
           int *p = ( int*)calloc(10, sizeof( int)); //堆上分配内存,返回内存地址
           printf( "%p ",p);
           for ( int i = 0; i < 10; i++)
           {
                  *(p + i) = i;
           }
           
           p = ( int*)realloc(p,10000); //在原来的内存后面补增60个字节
           printf( "%p", p);
           for ( int i = 10; i < 10000; i++)
           {
                  *(p + i) = i;
           }

           system( "pause");
           return 0;
    }










  • 相关阅读:
    max key length is 1000 bytes
    205 Reset Content
    The Path Attribute
    track message forwards, avoiding request loops, and identifying the protocol capabilities of all senders along the request/response chain
    test hypertext links for validity, accessibility, and recent modification
    ES6 will change the way you write JS code.
    ECMAScript Web APIs node.js
    Symbols
    HTML/Elements/base
    frag General URL components
  • 原文地址:https://www.cnblogs.com/ZhangJinkun/p/4531374.html
Copyright © 2020-2023  润新知