麒麟子

~~

导航

<2008年12月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

统计

常用链接

留言簿(12)

随笔分类

随笔档案

Friends

WebSites

积分与排名

最新随笔

最新评论

阅读排行榜

评论排行榜

#

冒泡排序与选择排序的不同、快速排序与选择排序的结合

     摘要: 目前广为使用的快速排序和选择排序联合使用,也会有意想不到的提升!
众所周知,当用快速排序法排序时,划分到很细的时候,明显很亏。 比如:两三个数排序却要划分成两堆,这样很划不来。所以,我们可以设定一个阀值,当快速排序划分到一定粒度的时候,便采用选择排序。 至于这个阀值,可以通过performace来测试,以得到一个“最优值”

  阅读全文

posted @ 2010-05-04 23:44 麒麟子 阅读(2169) | 评论 (3)编辑 收藏

某内存池中的指针用法

内存池实现有许多种,各有不同的优缺点。

这里不是主要说内存池,只是觉得这个内存池中的指针用得很飘逸!

 

 1template <class T,int AllocSize = 50>   
 2class MemPool   
 3{   
 4public:   
 5    static void* operator new(size_t allocLength)   
 6    {   
 7        if(!mStartPotinter)   
 8        {   
 9            MyAlloc();   
10        }
   
11        //将当前指向空闲内存起始地址作为反回地址   
12        unsigned char* p = mStartPotinter;   
13        //取出空闲区域前4字节的值,赋值给空闲地址   
14        //因为前四字节中存放了下一个BLOCK的地址   
15        mStartPotinter = *(unsigned char**)mStartPotinter;   
16        return p;   
17    }
   
18  
19    static void operator delete(void* deleteP)   
20    {   
21//      assert(deletePointer);   
22        *(unsigned char**)deleteP = mStartPotinter;   
23        mStartPotinter = (unsigned char*)deleteP;   
24    }
   
25  
26    static void MyAlloc()   
27    {   
28        //预分配内存   
29        mStartPotinter = new unsigned char[sizeof(T)*AllocSize];   
30        //构造BLOCK之间的关系    
31        //每个BLOCK的前4BYTE存放了下一个BLOCK的地址   
32        unsigned char** next = (unsigned char**)mStartPotinter;   
33        unsigned char* p = mStartPotinter;   
34  
35        for(int i = 0; i< AllocSize;++i)   
36        {   
37            p +=sizeof(T);//步进   
38            *next = p;//赋值   
39            next = (unsigned char**)p;//步进   
40        }
   
41        *next = NULL;   
42    }
   
43  
44    static unsigned char* mStartPotinter;   
45}
;   
46  
47template <class T,int AllocSize>   
48unsigned char* MemPool<T,AllocSize>::mStartPotinter = NULL;  
49
50
51本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wqjqepr/archive/2010/05/03/5552322.aspx

 

 

简单提示一下: unsigned char** next = (unsigned char**)mStartPotinter;

mStartPotinter作为二维指针的时候,相当于是一系列的unsigned char* [].

对于第一个 *next 相当于(unsigned char*)mStartPointer[0].

第二个相当于(unsigned char*)mStartPointer[sizeof(T)*1];

第三个相当于(unsigned char*)mStartPointer[sizeof(T)*2];

所以,构造BLOCK之间关系的时候,也可以写成

 

1for(int i = 0; i< AllocSize;++i)   
2{   
3 p +=sizeof(T);//步进   
4 unsigned char* pp = (unsigned char*)(p[sizeof(T)*i]);   
5 pp = p;//赋值   
6}
 

 

 

不想多解释了,累。估计多看几分种啥都明白了!

 

 

posted @ 2010-05-03 18:33 麒麟子 阅读(1929) | 评论 (11)编辑 收藏

函数中分配内存的问题

     摘要: 只是为了能够让自己讲清一些事!

大家都知道,如果想要在在C++的函数中分配内存,那么就只得使用指针的引用传递,或是二级指针

如:void MyNew(int** p)
{
*p = new int;
}
void MyNew(int*& p)
{
p = new int;
}


  阅读全文

posted @ 2010-04-27 22:52 麒麟子 阅读(1743) | 评论 (11)编辑 收藏

[转]C++ 对象的内存布局

     摘要: 这是陈皓继《C++虚函数表解析》之后的又一大作。
讲述了C++对象在以下情况的内存布局。值得一看!
1)有成员变量的情况。

2)有重复继承的情况。

3)有虚拟继承的情况。

4)有钻石型虚拟继承的情况。

还有,对于前几天的贴子表示报歉,今天是直接贴到这里的。 没乱码,还好!!!  阅读全文

posted @ 2010-04-27 20:56 麒麟子 阅读(2215) | 评论 (5)编辑 收藏

[原]译:一个游戏引擎所应具有的元素

     摘要: 3D游戏引擎设计是一项巨大的软件工程。一个人也能写出一个游戏,但这不只是熬一两个晚上便能搞定的,你很可能会出写出几兆的源代码量。如果你没有持久的信念与激情,你早晚会放弃。 当然,别指望你的第一次尝试就能写出完整的引擎,选择一个对引擎需求较小的项目。努力,你就能成功。  阅读全文

posted @ 2010-04-26 23:34 麒麟子 阅读(2127) | 评论 (11)编辑 收藏

仅列出标题
共38页: First 14 15 16 17 18 19 20 21 22 Last