I wrote some c code on codeblocks. It works perfectly fine on Windows but gave segmantation fault on Linux. Why ?
我在codeblocks上写了一些c代码。它在Windows上完美运行但在Linux上出现了分段错误。为什么?
This is main. I used 3 libraries and opencells method calls a recursive method.
这是主要的。我使用3个库和opencells方法调用一个递归方法。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc , char *argv[])
{
srand(time(NULL));
int size=atoi(argv[1]),trial=atoi(argv[2]); //! program basladıgında gelen argumanlar
int i,j; //! dongu degişkenleri
int **matrix = (int **)malloc(size * sizeof(int)); //!matriksin 1 boyutunu dinamik olarak yarattık
int *counters = (int *)malloc(trial * sizeof(int)); //! buda counterları tutcagımız array
for (i=0; i<size; i++)
matrix[i] = (int *)malloc(size * sizeof(int)); //! 2. boyutada yarattık
for(i=0;i<size;i++)
for(j=0;j<size;j++) //! matrixsi sıfırla saçma sapan degerler geliyo yoksa
matrix[i][j]=0;
for(i=0;i<trial;i++)
{
counters[i]=opencells(matrix,size); //!Random kapı açan ve bunun sayısını donduren fonksyon
}
printboard(matrix,size,trial,counters); //!Output.txtye yazdır
for (i=0; i<size; i++)
free(matrix[i]); //! ramden aldıgımız yerleri sal gitsin
free(matrix); //! bosu bosuna makinayı zorlamayalım
return 0;
}
3 个解决方案
#1
3
int **matrix = (int **)malloc(size * sizeof(int));
Change: (int) to (int*)
更改:(int)到(int *)
int **matrix = (int **)malloc(size * sizeof(int*));
Because sizeof(int) and sizeof(int*) maybe different, so your program maybe crashed when access the not allocated memory.
因为sizeof(int)和sizeof(int *)可能不同,所以当访问未分配的内存时,您的程序可能会崩溃。
#2
1
int **matrix = (int **)malloc(size * sizeof(int));
Change: (int)
to (int*)
更改:(int)到(int *)
int **matrix = (int **)malloc(size * sizeof(int*));
And while freeing the memory then use
在释放内存的同时使用
free(matrix[i]);
free(counters);
#3
0
int **matrix = (int **)malloc(size * sizeof(int));
You are allocating the wrong amount of memory here. You want sizeof(int*)
but you do sizeof(int)
.
你在这里分配错误的内存量。你想要sizeof(int *),但你做sizeof(int)。
On windows, int and int* are both 32 bit big. On linux, this might well be different, and thus causing your program to incur a segmentation fault for accessing unallocated memory.
在Windows上,int和int *都是32位大。在linux上,这可能会有所不同,从而导致程序因访问未分配的内存而导致分段错误。
#1
3
int **matrix = (int **)malloc(size * sizeof(int));
Change: (int) to (int*)
更改:(int)到(int *)
int **matrix = (int **)malloc(size * sizeof(int*));
Because sizeof(int) and sizeof(int*) maybe different, so your program maybe crashed when access the not allocated memory.
因为sizeof(int)和sizeof(int *)可能不同,所以当访问未分配的内存时,您的程序可能会崩溃。
#2
1
int **matrix = (int **)malloc(size * sizeof(int));
Change: (int)
to (int*)
更改:(int)到(int *)
int **matrix = (int **)malloc(size * sizeof(int*));
And while freeing the memory then use
在释放内存的同时使用
free(matrix[i]);
free(counters);
#3
0
int **matrix = (int **)malloc(size * sizeof(int));
You are allocating the wrong amount of memory here. You want sizeof(int*)
but you do sizeof(int)
.
你在这里分配错误的内存量。你想要sizeof(int *),但你做sizeof(int)。
On windows, int and int* are both 32 bit big. On linux, this might well be different, and thus causing your program to incur a segmentation fault for accessing unallocated memory.
在Windows上,int和int *都是32位大。在linux上,这可能会有所不同,从而导致程序因访问未分配的内存而导致分段错误。