malloc是什么意思呢?怎么用?

[复制链接]
查看11 | 回复2 | 2015-10-8 18:16:58 | 显示全部楼层 |阅读模式
回复

使用道具 举报

千问 | 2015-10-8 18:16:58 | 显示全部楼层
C#语言里一个分配内存的函数吧原型:extern void *malloc(unsigned int num_bytes);
用法:#include
或#include
功能:分配长度为num_bytes字节的内存块
说明:如果分配成功则返回指向被分配内存的指针,否则返回空指针NULL。
当内存不再使用时,应使用free()函数将内存块释放。malloc的语法是:指针名=(数据类型*)malloc(长度),(数据类型*)表示指针.
举例:
// malloc.c
#include
#include
main()
{
char *p;
clrscr();
// clear screen
p=(char *)malloc(100);
if(p)
printf("Memory Allocated at: %x",p);
else
printf("Not Enough Memory!\n");
free(p);
getchar();
return 0;
}参考资料:http://baike.baidu.com/view/736228.htm?fr=topic


已赞过已踩过<
回复

使用道具 举报

千问 | 2015-10-8 18:16:58 | 显示全部楼层
函数名: 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; }
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行