1个回答
展开全部
函数名: calloc 功能: 分配主存储器 用法: void *calloc(size_t nelem, size_t elsize); 程序例: #include <stdio.h> #include <alloc.h> 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 <string.h> #include <stdio.h> #include <alloc.h> 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 <stdio.h> #include <string.h> #include <alloc.h> #include <process.h> 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 <stdio.h> #include <alloc.h> #include <string.h> 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; }
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询