URN Logo
UNIX Resources » Linux » China Linux Forum » C/C++编程版 » 23 » 请教:calloc
announcement 声明: 本页内容为中国Linux论坛的内容镜像,文章的版权以及其他所有的相关权利属于中国Linux论坛和相应文章的作者,如果转载,请注明文章来源及相关版权信息。
Resources
China Linux Forum(finished)
Linux Forum(finished)
FreeBSD China(finished)
linuxforum.net
  业界新闻与评论
  自由软件杂谈
  IT 人生
  Linux软件快递
  翻译作坊
  Linux图书与评论
  GNU Emacs/XEmacs
  Linux 中文环境和中文化
  Linux桌面与办公软件
  Linux 多媒体与娱乐版
  自由之窗Mozilla
  笔记本电脑上的Linux
  Gentoo
  Debian 一族
  网络管理技术
  Linux 安装与入门
  WEB服务器和FTP服务器
  域名服务器和邮件服务器
  Linux防火墙和代理服务器应用
  文件及打印服务器
  技术培训与认证
  Linux内核技术
  Linux 嵌入技术
  Linux设备驱动程序
  Linux 集群技术
  LINUX平台数据库
  系统和网络安全
  CPU 与 编译器
  系统计算研究所专栏
  Linux下的GUI软件开发
  C/C++编程版
  PHP 技 术
  Java&jsp技术
  Shell编程技术
  Perl 编 程
  Python 编 程
  XML/Web Service 技术
  永远的Unix
  FreeBSD世界
   
请教:calloc
请教: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)]
 
Subject: 请教:calloc
Author: kelvin_tang    Posted: 2005-10-29 20:52    Length: 450 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]
Subject: Re: 请教:calloc
Author: whenwhentea    Posted: 2005-10-31 16:08    Length: 307 byte(s)
[Original] [Print] [Top]
我想大概是这个意思,可能不对!请其他大虾指教.
calloc在申请内存时,它也是通过调用malloc来申请内存, 但它通过_set_new_handler设置了一个new handler mode routine,这个routine就是用来在处理malloc申请内存失败时所做的过程,而malloc在申请内存失败后是不会调用这个routine的,与malloc相比,calloc就叫new handler mode.
----
吃自助的最高境界: 扶墙进,扶墙出.
[Original] [Print] [Top]
Subject: Re: 请教:calloc
Author: kelvin_tang    Posted: 2005-10-31 19:15    Length: 38 byte(s)
[Original] [Print] [Top]
恩,看了楼上的解释,我有点明白了,谢谢
[Original] [Print] [Top]
Subject: Re: 请教:calloc
Author: ddd    Posted: 2005-11-06 10:19    Length: 999 byte(s)
[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]
Subject: Re: 请教:calloc
Author: kelvin_tang    Posted: 2005-11-07 15:43    Length: 19 byte(s)
[Original] [Print] [Top]
恩 ,我明白了,谢谢
[Original] [Print] [Top]
« Previous thread
请问如何在内嵌汇编中调用C函数
C/C++编程版
23
Next thread »
LINUX汇编编程文档翻译三部曲:Linux assembly HOWTO 翻译计划
     

Copyright © 2007 UNIX Resources Network, All Rights Reserved.      About URN | Privacy & Legal | Help | Contact us
备案序号: 京ICP备05006143    webmaster: webmaster@unixresources.net
This page created on 2008-07-17 03:52:29, cost 0.038160800933838 ms.