奇怪的malloc错误访问
我在Xcode中使用malloc分配内存时遇到问题 当我使用较小的块大小(256)时,代码没有问题 如果我使用更大的块大小(65536),Xcode将停止在“state1[t]=(int*)malloc(sizeof(int)*4)”,并告诉我访问错误。如何解决这个问题 谢谢奇怪的malloc错误访问,c,malloc,exc-bad-access,C,Malloc,Exc Bad Access,我在Xcode中使用malloc分配内存时遇到问题 当我使用较小的块大小(256)时,代码没有问题 如果我使用更大的块大小(65536),Xcode将停止在“state1[t]=(int*)malloc(sizeof(int)*4)”,并告诉我访问错误。如何解决这个问题 谢谢 int main(int argc, const char * argv[]) { // insert code here... int **state1; int t = 0; int
int main(int argc, const char * argv[]) {
// insert code here...
int **state1;
int t = 0;
int Block_size = 65535;
state1 = (int **)malloc(sizeof(int) * Block_size);
printf("%d",Block_size);
for (t=0 ; t < Block_size-1 ; t++) {
state1[t] = (int*) malloc(sizeof(int) * 4);
}
printf("end");
return 0;
}
int main(int argc,const char*argv[]{
//在这里插入代码。。。
国际**国家1;
int t=0;
int Block_size=65535;
state1=(int**)malloc(sizeof(int)*块大小);
printf(“%d”,块大小);
对于(t=0;t
第一个malloc应该是
state1 = malloc(sizeof(int *) * Block_size);
因为您分配了一个指针数组。在64位平台上,这会带来不同!
有些人喜欢写作
state1 = malloc(sizeof(*state1) * Block_size);
为了避免这种错误
备注:在C中,您不需要强制转换
malloc()的返回值
state1 = malloc(sizeof(int *) * Block_size);
因为您分配了一个指针数组。在64位平台上,这会带来不同!
有些人喜欢写作
state1 = malloc(sizeof(*state1) * Block_size);
为了避免这种错误
备注:在C中,您不需要强制转换malloc()
的返回值