我不想努力执行迪吉克勒,这是我已经制定的图表制作法。
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <math.h>
#define MAX 300
int main (int argc, char *argv[]){
int v = atoi(argv[1]);
int SIZE = v*v;
int* adjMatrix = malloc(sizeof(int)* SIZE);
graphGeneration(adjMatrix, v);
free(adjMatrix);
return 0;
}
void graphGeneration(int* adj, int numV){
int i, j, r;
for(i = 0; i< numV; i++){
for(j=0; j < numV; j++){
if(i == j){
adj[i * numV + j] = 0;
}
else{
r = rand() % MAX;
adj[i * numV + j] = r;
adj[j * numV + i] = r;
}
}
}
}
When I try a value of v in 1000 s it seems to work fine, but when I try to enter a value of v = 10,000+ I get a segfault (Specifically at 50,000 is the number I noticed). Running valgrind gets me the error in the title at this method. Reposting here for convenience:
Invalid write of size 4
at 0x400800: graphGeneration
by 0x4006E3: main
Address 0x0 is not stack d, malloc d or (recently) free d
Access not within mapped region at address 0x0
没有人会想如何回避,或者这里是否有任何明显错误?
我也注意到了在朝圣时的这个界限。
Warning: silly arg (-7179869184) to malloc()
我不敢肯定这一点,但似乎也是一种奇怪的事情。