爱问知识人 爱问教育 医院库

在C中,calloc free malloc realloc各自是什么意思?语法是什么?有什么作用?

首页

在C中,calloc free malloc realloc各自是什么意思?语法是什么?有什么作用?

最好还能举几个例子以及每行的说明~
谢谢啦

提交回答
好评回答
  • 2005-05-30 21:22:27
      函数名: calloc 
    功 能: 分配主存储器 
    用 法: void *calloc(size_t nelem, size_t elsize); 
    程序例: 
    #include  
    #include  
    int main(void) 
    { 
    char *str = NULL; 
    /* allocate memory for string */ 
    str = calloc(10, sizeof(char)); 
    /* copy "Hello" into string */ 
    strcpy(str, "Hello"); 
    /* display string */ 
    printf("String is %s\n", str); 
    /* free memory */ 
    free(str); 
    return 0; 
    } 
    函数名: free 
    功 能: 释放已分配的块 
    用 法: void free(void *ptr); 
    程序例: 
    #include  
    #include  
    #include  
    int main(void) 
    { 
    char *str; 
    /* allocate memory for string */ 
    str = malloc(10); 
    /* copy "Hello" to string */ 
    strcpy(str, "Hello"); 
    /* display string */ 
    printf("String is %s\n", str); 
    /* free memory */ 
    free(str); 
    return 0; 
    } 
    函数名: malloc  
    功  能: 内存分配函数  
    用  法: void *malloc(unsigned size);  
    程序例:  
    #include   
    #include   
    #include   
    #include   
    int main(void)  
    {  
       char *str;  
       /* allocate memory for string */  
       /* This will generate an error when compiling */  
       /* with C++, use the new operator instead。
       */ if ((str = malloc(10)) == NULL) { printf("Not enough memory to allocate buffer\n"); exit(1); /* terminate program if out of memory */ } /* copy "Hello" into string */ strcpy(str, "Hello"); /* display string */ printf("String is %s\n", str); /* free memory */ free(str); return 0; } 函数名: realloc 功 能: 重新分配主存 用 法: void *realloc(void *ptr, unsigned newsize); 程序例: #include #include #include int main(void) { char *str; /* allocate memory for string */ str = malloc(10); /* copy "Hello" into string */ strcpy(str, "Hello"); printf("String is %s\n Address is %p\n", str, str); str = realloc(str, 20); printf("String is %s\n New address is %p\n", str, str); /* free memory */ free(str); return 0; } 。
      

    a***

    2005-05-30 21:22:27

其他答案

    2005-05-30 13:41:35
  • malloc()和free()对应,用了malloc申请的记得要用free释放掉

    e***

    2005-05-30 13:41:35

  • 2005-05-29 23:19:06
  • void * malloc(unsigned size); 动态分配size字节空间,返回它的指针
    void * calloc(unsigned n,unsigned size);动态分配n个连续的size字节空间,返回第一个指针,好比一个数组
    void free(void *) ; 释放上述两函数分配的空间;
    void * realloc(void *p,unsigned n);将p指向的内存空间扩展成n个,把它放在最后解释因为偶不建议你使用~~确实要用也要考虑多种情况

    邓***

    2005-05-29 23:19:06

  • 2005-05-29 20:54:55
  • 老大,也太节约了,教育是要投资的
    买本C的书看不是更有把握些

    江***

    2005-05-29 20:54:55

类似问题

换一换
  • C/C++ 相关知识

  • 电脑网络技术
  • 电脑网络

相关推荐

正在加载...
最新问答 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 169-188
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):