|
|
|
|
 请教:calloc - kelvin_tang [ 2005-10-29 20:52 | 450 byte(s)]
 Re: 请教:calloc - ddd [ 2005-11-06 10:19 | 999 byte(s)]
 Re: 请教:calloc - kelvin_tang [ 2005-11-07 15:43 | 19 byte(s)]
 Re: 请教:calloc - whenwhentea [ 2005-10-31 16:08 | 307 byte(s)]
 Re: 请教:calloc - kelvin_tang [ 2005-10-31 19:15 | 38 byte(s)]
|
|
|
|
[Original]
[Print]
[Top]
|
请先看我从msdn上摘抄的一段文字:
calloc calls malloc 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.
我想请问的是什么叫做new handler mode, new handler mode routine?
请版里的朋友帮忙!
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
我想大概是这个意思,可能不对!请其他大虾指教.
calloc在申请内存时,它也是通过调用malloc来申请内存, 但它通过_set_new_handler设置了一个new handler mode routine,这个routine就是用来在处理malloc申请内存失败时所做的过程,而malloc在申请内存失败后是不会调用这个routine的,与malloc相比,calloc就叫new handler mode.
|
|
|
----
吃自助的最高境界: 扶墙进,扶墙出.
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
解释这个问题还真有点麻烦。
1 new handle
比如int *n=new int[10000000000];
内存不足,程序有可能立刻崩溃,为了避免这种情况,允许你自定义一个函数,假设这个函数的名字为nh,new执行失败了,那么转入nh进行处理。这个nh就是new handle。nh执行的那些语句就是new handle route,这里概念说的有点绕,理解大意就行了,我历来对概念精确把握就很差。
以上是c++标准,下面是按照你贴出来的msdn的意思说的。
2 malloc默认行为是失败了不调用new handle route,就是不调用你那个nh。
3 calloc失败则可以调用你那个nh,虽然calloc通过调用malloc实现分配内存的。
4 你先想象成_set_new_mode设置了一个全局变量(设为bbb),bbb缺省是false,而calloc通过_set_new_mode将bbb=true,然后调用malloc,malloc一看到bbb==true,就分配内存失败的时候调用nh了。
从malloc正常返回后calloc将bbb=false,那么下次直接调用malloc的时候分配失败还是不掉用nh。
4的实现过程纯属猜测,但结果应该是这么回事。
new handle mode就是分配失败是否调用你的new handle route。
|
|
|
[Original]
[Print]
[Top]
|
|
|