求内存申请和初始化一并完成的函数

背着书包去打工 2013-12-11 09:18:48
如题
本人比较懒,想找个可以在我申请内存的时候就能自动帮我清空内存的函数,不用我每次memset一下
求精,求辟
...全文
174 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-12-11
  • 打赏
  • 举报
回复
calloc Allocates an array in memory with elements initialized to 0. void *calloc( size_t num, size_t size ); Routine Required Header Compatibility calloc <stdlib.h> and <malloc.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value calloc returns a pointer to the allocated space. The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object. To get a pointer to a type other than void, use a type cast on the return value. Parameters num Number of elements size Length in bytes of each element Remarks The calloc function allocates storage space for an array of num elements, each of length size bytes. Each element is initialized to 0. calloc calls malloc in order to use the C++ _set_new_mode function to set the new handler mode. The new handler mode indicates whether, on failure, malloc is to call the new handler routine as set by _set_new_handler. By default, malloc does not call the new handler routine on failure to allocate memory. You can override this default behavior so that, when calloc fails to allocate memory, malloc calls the new handler routine in the same way that the new operator does when it fails for the same reason. To override the default, call _set_new_mode(1) early in your program, or link with NEWMODE.OBJ. When the application is linked with a debug version of the C run-time libraries, calloc resolves to _calloc_dbg. For more information about how the heap is managed during the debugging process, see Using C Run-Time Library Debugging Support. Example /* CALLOC.C: This program uses calloc to allocate space for * 40 long integers. It initializes each element to zero. */ #include <stdio.h> #include <malloc.h> void main( void ) { long *buffer; buffer = (long *)calloc( 40, sizeof( long ) ); if( buffer != NULL ) printf( "Allocated 40 long integers\n" ); else printf( "Can't allocate memory\n" ); free( buffer ); } Output Allocated 40 long integers Memory Allocation Routines See Also free, malloc, realloc
lee_鹿游原 2013-12-11
  • 打赏
  • 举报
回复
楼主还是自己手动初始化吧..
  • 打赏
  • 举报
回复
上面的回答都太敷衍了,比我还懒
版主大哥 2013-12-11
  • 打赏
  • 举报
回复
懒懒的吉他手 2013-12-11
  • 打赏
  • 举报
回复
自己写个dll一并完成即可
ztenv 版主 2013-12-11
  • 打赏
  • 举报
回复
重载全局的new运算符,申请成功后就memset

64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧