笔者在处理程序奔溃问题的时候,遇到栈溢出的情况,栈溢出最常见的情况是:迭代调用和数组过大。数组占用占空间,所以改为了malloc方式放在堆上。想想,就想整理一下关于对多维数组的动态分配问题。
一,堆和栈的先关问题
首先,必须了解一下堆和栈的问题,可参考这篇文章:http://blog.csdn.net/hairetz/article/details/4141043 ,现在稍微总结一下:
栈区(stack),由编译器自动分配释放,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的栈;堆区(heap),一般由程序员分配释放,若程序员不释放,程序结束时可能由OS回收。注意它与数据结构中的堆是两回事,分配方式倒是类似于链表;全局区(静态区)(static),全局变量和静态变量的存储是放在一块的,初始化的全局变量和静态变量在一块区域,未初始化的全局变量和未初始化的静态变量在相邻的另一块区域,程序结束后由系统释放;文字常量区,常量字符串就是放在这里的,程序结束后由系统释放 ;程序代码区,存放函数体的二进制代码。
二,多维数组的malloc内存动态分配
对于一些需要放在堆上的数组,或者维数未知的数组,我们可以直接定义指针,在进行对其内存分配。
- char (*a)[N];//指向数组的指针
- a = (char (*)[N])malloc(sizeof(char) * N * m);
- printf("%d\n", sizeof(a));//4,指针
- printf("%d\n", sizeof(a[0]));//N,一维数组
- free(a);
(2)已知第一维
- char* a[M];//指针的数组
- int i;
- for(i=0; i<M; i++)
- a[i] = (char *)malloc(sizeof(char) * n);
- printf("%d\n", sizeof(a));//4*M,指针数组
- printf("%d\n", sizeof(a[0]));//4,指针
- for(i=0; i<M; i++)
- free(a[i]);
(3)已知第一维,一次分配内存(保证内存的连续性)
- char* a[M];//指针的数组
- int i;
- a[0] = (char *)malloc(sizeof(char) * M * n);
- for(i=1; i<M; i++)
- a[i] = a[i-1] + n;
- printf("%d\n", sizeof(a));//4*M,指针数组
- printf("%d\n", sizeof(a[0]));//4,指针
- free(a[0]);
(4)两维都未知
- char **a;
- int i;
- a = (char **)malloc(sizeof(char *) * m);//分配指针数组
- for(i=0; i<m; i++)
- {
- a[i] = (char *)malloc(sizeof(char) * n);//分配每个指针所指向的数组
- }
- printf("%d\n", sizeof(a));//4,指针
- printf("%d\n", sizeof(a[0]));//4,指针
- for(i=0; i<m; i++)
- {
- free(a[i]);
- }
- free(a);
(5)两维都未知,一次分配内存(保证内存的连续性)
- char **a;
- int i;
- a = (char **)malloc(sizeof(char *) * m);//分配指针数组
- a[0] = (char *)malloc(sizeof(char) * m * n);//一次性分配所有空间
- for(i=1; i<m; i++)
- {
- a[i] = a[i-1] + n;
- }
- printf("%d\n", sizeof(a));//4,指针
- printf("%d\n", sizeof(a[0]));//4,指针
- free(a[0]);
- free(a);
- char (*a)[N];//指向数组的指针
- a = new char[m][N];
- printf("%d\n", sizeof(a));//4,指针
- printf("%d\n", sizeof(a[0]));//N,一维数组
- delete[] a;
(2)已知第一维
- char* a[M];//指针的数组
- for(int i=0; i<M; i++)
- a[i] = new char[n];
- printf("%d\n", sizeof(a));//4*M,指针数组
- printf("%d\n", sizeof(a[0]));//4,指针
- for(i=0; i<M; i++)
- delete[] a[i];
(3)已知第一维,一次分配内存(保证内存的连续性)
- char* a[M];//指针的数组
- a[0] = new char[M*n];
- for(int i=1; i<M; i++)
- a[i] = a[i-1] + n;
- printf("%d\n", sizeof(a));//4*M,指针数组
- printf("%d\n", sizeof(a[0]));//4,指针
- delete[] a[0];
(4)两维都未知
- char **a;
- a = new char* [m];//分配指针数组
- for(int i=0; i<m; i++)
- {
- a[i] = new char[n];//分配每个指针所指向的数组
- }
- printf("%d\n", sizeof(a));//4,指针
- printf("%d\n", sizeof(a[0]));//4,指针
- for(i=0; i<m; i++)
- delete[] a[i];
- delete[] a;
(5)两维都未知,一次分配内存(保证内存的连续性)
- char **a;
- a = new char* [m];
- a[0] = new char[m * n];//一次性分配所有空间
- for(int i=1; i<m; i++)
- {
- a[i] = a[i-1] + n;//分配每个指针所指向的数组
- }
- printf("%d\n", sizeof(a));//4,指针
- printf("%d\n", sizeof(a[0]));//4,指针
- delete[] a[0];
- delete[] a;
- void func(int a[][N])
- {
- printf("%d\n", a[1][2]);
- }
(2)不给定第二维长度
- void func(int* a)
- {
- printf("%d\n", a[1 * N + 2]);//计算元素位置
- }
以上来源:http://blog.csdn.net/xiajun07061225/article/details/6827643
三,关于数组与指针相互memcpy的问题
笔者只举个例子:
unsigned char Buffer1[5][8]= { {1,2,3,4,5,6,7,8}, {1,2,3,4,5,6,7,8}, {1,2,3,4,5,6,7,8}, {1,2,3,4,5,6,7,8}, {1,2,3,4,5,6,7,8}, }; unsigned char *Buffer2 = NULL; int bufer_index = 0; Buffer2 = (unsigned char *)malloc(sizeof(unsigned char) * 5 * 8); memset(Buffer2, 0, sizeof(Buffer2)); for (bufer_index = 0; bufer_index < 5; bufer_index++) { memcpy(Buffer2 + (bufer_index * 8), Buffer1[bufer_index], 8 * sizeof(unsigned char)); }
memcpy回来:
for (bufer_index = 0; bufer_index < 5; bufer_index++) { memcpy(Buffer[bufer_index], Buffer2 + (bufer_index * 8), 8 * sizeof(unsigned char)); }