我有一个烦人的错误
rq->tickets[rq->usedContracts] = toAdd;
如果我这样做: rq->tickets[0] = toAdd
程序崩溃
如果我这样做的 rq->tickets[1] = toAdd;
话
瓦尔格林 说
==19501== 使用大小为 8 的未初始化值
和
==19501== 大小为 8 的无效写入
对于这条线。怎么了?
struct TS_element {
int travels;
int originalTravels;
int cost;
char** dates;
int moneyLeft;
} TicketSet;
struct RQ_element {
int usedContracts;
struct TS_element* tickets;
} RabQav;
TicketSetStatus tsCreate(TicketSet* t, int n, int c) {
if (n <= 0)
return TS_ILLEGAL_PARAMETER;
TicketSet* myTicketSet = (TicketSet*) malloc(sizeof(TicketSet));
if (myTicketSet == NULL) {
return TS_CANNOT_CREATE;
}
myTicketSet->usedTravels = 0;
myTicketSet->originalTravels = n;
myTicketSet->cost = c;
myTicketSet->moneyLeft = n * c;
char** dates = malloc(sizeof(char**)* (n)); //todo maybe c99 allows dynamic arrays?
for (int i = 0; i < n; i++) {
dates[i] = malloc(sizeof(char)*GOOD_LENGTH+1);
if (dates[i] == NULL) {
free(dates);
free(t);
return TS_CANNOT_CREATE;
}
}
myTicketSet->dates = dates;
*t = *myTicketSet;
return TS_SUCCESS;
}
static void copyTicketSets(TicketSet* dest, const TicketSet* source) {
dest->usedTravels = source->usedTravels;
dest->originalTravels = source->originalTravels;
dest->cost = source->cost;
dest->moneyLeft = source->moneyLeft;
for (int i = 0; i < source->originalTravels; i++) {
if (NULL != source->dates[i]) {
free(dest->dates[i]);
dest->dates[i] = malloc(sizeof(char) * GOOD_LENGTH + 1);
if (dest->dates[i] == NULL) {
free(dest->dates); //todo free dates 0...i-1
free(dest);
return;
}
strcpy(dest->dates[i], source->dates[i]);
}
}
}
RabQavStatus rqLoadTS(RabQav* rq, TicketSet t, DateTime dt) {
TicketSet toAdd;
TicketSetStatus res = tsCreate(&toAdd, t.originalTravels, t.cost);
if (res != TS_SUCCESS) {
return RQ_FAIL;
}
copyTicketSets(&toAdd, &t);
rq->tickets[rq->usedContracts] = toAdd;
rq->usedContracts++;
return RQ_SUCCESS;
}