﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-David's Blog-文章分类-C++ 学习</title><link>http://www.cppblog.com/qiuxing009/category/5693.html</link><description>C++学习心得, 感想!</description><language>zh-cn</language><lastBuildDate>Thu, 22 May 2008 05:37:28 GMT</lastBuildDate><pubDate>Thu, 22 May 2008 05:37:28 GMT</pubDate><ttl>60</ttl><item><title>[转]GlobalAlloc返回Null的可能性</title><link>http://www.cppblog.com/qiuxing009/articles/37753.html</link><dc:creator>Xingfu.Qiu</dc:creator><author>Xingfu.Qiu</author><pubDate>Tue, 04 Dec 2007 01:40:00 GMT</pubDate><guid>http://www.cppblog.com/qiuxing009/articles/37753.html</guid><wfw:comment>http://www.cppblog.com/qiuxing009/comments/37753.html</wfw:comment><comments>http://www.cppblog.com/qiuxing009/articles/37753.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/qiuxing009/comments/commentRss/37753.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qiuxing009/services/trackbacks/37753.html</trackback:ping><description><![CDATA[<p>最初发现这个问题是在VChelp.net的论坛上, 想到这个也许对我有用,所以做个备份.<br><br>原贴的问题描述如下:</p>
<p>&nbsp;&nbsp; 原贴地址: <a href="http://www.vchelp.net/cndevforum/subject_view.asp?subject_id=178091&amp;forum_id">http://www.vchelp.net/cndevforum/subject_view.asp?subject_id=178091&amp;forum_id</a>=</p>
<blockquote dir=ltr style="MARGIN-RIGHT: 0px">
<p>&nbsp;&nbsp;&nbsp; 对它只知道皮毛,GlobalAlloc(GNN, 175)返回NULL<br>&nbsp;&nbsp;&nbsp; 现象如下:<br>&nbsp;&nbsp;&nbsp; 1. 内存还多呢! 不明白比尔的任务管理器对它好使否?<br>&nbsp;&nbsp;&nbsp; 2. 据说 它只能分配6000个HGLOBAL, 会不会和它有关系!? //我如何知道是否超过这个数了呢?<br>&nbsp;&nbsp;&nbsp; 3. 我们软件有一个工具, 来监测和它相关的函数, 发现没有泄漏//那个工具应该不会有问题吧!</p>
</blockquote>
<p>&nbsp;&nbsp; 我从来没有用过这东西, 流行这东西的时候, 我还不知道什么叫cpp呢, 有经验的人来帮忙啦~~~~~~~</p>
<p>&nbsp;&nbsp;&nbsp; 针对上述描述1和描述2,&nbsp; 我写了一段代码测试了一下, 还真的会出现返回为NULL的情况. 相关代码如下:</p>
<p>&nbsp;&nbsp;&nbsp; HGLOBAL g[80000];<br>&nbsp;&nbsp;&nbsp; memset(g, 0, sizeof(g));<br>&nbsp;&nbsp;&nbsp; int m = 0;</p>
<p>&nbsp;&nbsp;&nbsp; for(unsigned long i = 0; i &lt; 80000; i++)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; g[i] = ::GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT, 175);</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(g[i] == NULL)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m++; // 这里加个断点<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; }</p>
<p>&nbsp;&nbsp; 请测试一下这段代码,&nbsp; 我发现 i = 65535 以后, 申请的都是NULL. 也就是说, GlobalAlloc最多只能成功申请65535次, 其它的申请操作将返回NULL.</p>
<img src ="http://www.cppblog.com/qiuxing009/aggbug/37753.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qiuxing009/" target="_blank">Xingfu.Qiu</a> 2007-12-04 09:40 <a href="http://www.cppblog.com/qiuxing009/articles/37753.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>