首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

怎么提高二维数组的动态内存配置与回收的速度

2012-02-12 
如何提高二维数组的动态内存配置与回收的速度我这样定义了一个二维指针数组char**A_segmentnewchar*[2000

如何提高二维数组的动态内存配置与回收的速度
我这样定义了一个二维指针数组
char   **A_segment   =new   char*[2000000];
for(int   i=0;i <2000000;i++)
{
    A_segment[i]=new   char[15];
}

[解决办法]
const int SIZE = 2000000;
const int STEP = 15;
char* mem = new char[SIZE * sizeof(char*) + SIZE*STEP];
char** A_segment = reinterpret_cast <char**> (mem);
char* data = mem + SIZE * sizeof(char*);
for(int i=0;i <SIZE;i++)
{
A_segment[i] = data;
data += STEP;
}

//... do something
strcpy(A_segment[SIZE-1], "hello! ");
printf( "%s\n ", A_segment[SIZE-1]);

//release
delete mem;

热点排行