【发布时间】:2015-09-14 19:31:45
【问题描述】:
下面的代码应该创建两个具有连续内存位置的二维数组(posa 和 posb),然后使用 memmove 函数将 posa 复制到 posb 上。令人惊讶的是,它适用于“小”数组大小(例如:size1=size2=100),但对于较大的数组(例如:size1=size2=300),我会遇到分段错误(核心转储)。 当我使用动态内存分配时,我认为这不是像here 这样的堆栈溢出问题... 有人可以解释我做错了什么吗?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int ** Allocation_array_x2(int size1, int size2){
int **ptr;
int i;
ptr = malloc(size1 * sizeof(*ptr));
if(ptr == NULL) {
fprintf(stderr,"Allocation error");
exit(1);
}
ptr[0] = malloc(size1 * size2 * sizeof(**ptr));
for(i=1 ; i < size1 ; i++){
ptr[i]=ptr[0]+(i * size2);
if( ptr[i] == NULL) {
fprintf(stderr,"Allocation error");
exit(1);
}
}
return ptr;
}
void Free_array_x2(int **ptr){
free(ptr);
ptr=NULL;
}
int main(){
int size1=300, size2=300;
int **posa, **posb;
posa=Allocation_array_x2(size1,size2);
posb=Allocation_array_x2(size1,size2);
/* array_filling */
for( int j = 0 ; j<size1 ; j++ ) {
for( int k = 0 ; k<size2 ; k++ ) {
posa[j][k] = 2*j+3*k+1;
posb[j][k] = 1000;
}
}
memmove(posb,posa,size1*size2*sizeof(int));
for(int i = 0 ; i<size1 ; i++ ) {
for(int j = 0 ; j<size2 ; j++ ) {
printf("%d\t%d\n",posa[i][j],posb[i][j]);
}
}
Free_array_x2(posa);
Free_array_x2(posb);
}
【问题讨论】:
-
细分到底发生在哪里?
-
第二个
malloc的检查必须在ptr[0]上。如果不是NULL,则所有后续指针都不是NULL。你的free方法应该是freeptr[0]和ptr。 -
是的,这是内存泄漏,您只需对两个 malloc 进行一次免费调用
-
如果要将所有需要的内存分配给
ptr[0],然后将其分配给其他数组元素(为了保持连续性),我觉得处理它会更好作为一维数组并使用row*size2+col来操作索引。