A
a
By James Kuyper insightful suggestion, I identified my program error sources
from calloc.
I just don't know why for the following small program, I try different
JUDGEMENTDAY from 1,2,... and when it is >= 13, segmentation fault appears
if I don't force the program to exit by setting k to <= 11 in the last but 4
line. I guess the program is accessing something invalid but just don't know
why this happens.
#define SIZE 12
#define JUDGEMENTDAY 13
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv) {
int i=0;
int **chair_assg;
// int chair_assg[JUDGEMENTDAY+1][SIZE];
//int chair_assg[JUDGEMENTDAY][SIZE];
int t=0,k,kk,j,jj,mid,did;
double cost;
///*
i = JUDGEMENTDAY + 1;
printf("i: %d",i);
//return;
chair_assg = calloc(i,sizeof(int*));
if(chair_assg == NULL)
return;
//chair_assg = calloc((JUDGEMENTDAY),sizeof(int*));
for(i=0;i<SIZE;i++) {
chair_assg = calloc(SIZE,sizeof(int));
}
for(k=0;k<JUDGEMENTDAY;k++) {
printf("k: %d", k);
for(i=0;i<SIZE;i++) {
chair_assg[k] = 0;
}
if (k==11) // <-------------- HERE
return;
}
return 0;
}
from calloc.
I just don't know why for the following small program, I try different
JUDGEMENTDAY from 1,2,... and when it is >= 13, segmentation fault appears
if I don't force the program to exit by setting k to <= 11 in the last but 4
line. I guess the program is accessing something invalid but just don't know
why this happens.
#define SIZE 12
#define JUDGEMENTDAY 13
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv) {
int i=0;
int **chair_assg;
// int chair_assg[JUDGEMENTDAY+1][SIZE];
//int chair_assg[JUDGEMENTDAY][SIZE];
int t=0,k,kk,j,jj,mid,did;
double cost;
///*
i = JUDGEMENTDAY + 1;
printf("i: %d",i);
//return;
chair_assg = calloc(i,sizeof(int*));
if(chair_assg == NULL)
return;
//chair_assg = calloc((JUDGEMENTDAY),sizeof(int*));
for(i=0;i<SIZE;i++) {
chair_assg = calloc(SIZE,sizeof(int));
}
for(k=0;k<JUDGEMENTDAY;k++) {
printf("k: %d", k);
for(i=0;i<SIZE;i++) {
chair_assg[k] = 0;
}
if (k==11) // <-------------- HERE
return;
}
return 0;
}