c - 在没有树的地方双重释放或腐败
c - double free or corruption where there arent frees
好吧,我已经完成了休闲代码(基本上我想做的是:我有一个列表,其中有一些障碍物,如果 x=-1,则障碍物在整个楼层中,否则,它们是只在一个地方,所以我想每次通过代码时,障碍物都在那个位置):
void restartmatrix(parking * parklot, block * obstacles, int time) {
int x, y, z;
block * tempblock;
for (z = 0; z < parklot->depth; z++) {
for (x = 0; x < parklot->width; x++) {
for (y = 0; y < parklot->height; y++) {
(parklot - > floors[x][y][z]).percorrido = -1;
}
}
}
/*tempblock = obstacles;
while(tempblock != NULL){
if(tempblock->start = time){
if(tempblock->x != -1){
for(x=0; x < parklot->width; x++){
for(y = 0; y < parklot->height; y++){
(parklot->floors[x][y][tempblock->z]).percorrido = 1;
}
}
}else{
(parklot->floors[tempblock->x][tempblock->y][tempblock->z]).percorrido = 1;
}
}else if(tempblock->end == time && tempblock->end != 0){
if(tempblock->x != -1){
for(x=0; x < parklot->width; x++){
for(y = 0; y < parklot->height; y++){
(parklot->floors[x][y][tempblock->z]).percorrido = -1;
}
}
}else{
(parklot->floors[tempblock->x][tempblock->y][tempblock->z]).percorrido = -1;
}
}
tempblock = tempblock->next;
}*/
}
你能看到代码的某些部分被注释了吗....好吧,问题是如果取消注释,程序会给我一个双重释放或损坏错误(fasttop),但我似乎找不到为什么,因为我没有做任何免费的...
如有任何帮助,我们将不胜感激
编辑
我分配障碍列表的代码在这里,如果它可以帮助:
block * read_blocking_list(char * file){
FILE * fp;
char leitura[256];
char temporary;
block * head = NULL;
block * temp;
block * aux;
int start, end, x = 0, y = 0, z = 0, size;
fp = fopen(file, "r");
if(fp == NULL){
printf("Erro ao abrir o ficheiro de leitura\n");
exit(0);
}
while(fgets(leitura, 255, fp) != NULL){
temp = malloc(sizeof(block));
size = sscanf(leitura, "%c %d %d %d %d %d", &temporary, &start, &end, &x, &y, &z);
temp->start = start;
temp->end = end;
temp->next = NULL;
if(size < 5){
temp->x = -1;
temp->z = x;
}else{
temp->x = x;
temp->y = y;
temp->z = z;
}
if(head == NULL){
head = temp;
}else{
aux = head;
while(aux->next != NULL){
aux = aux->next;
}
aux->next = temp;
}
}
fclose(fp);
return head;
}
你测试是否 tempblock-x != -1
并在 else
块中使用 tempblock->x
这样:
(parklot->floors[tempblock->x][tempblock->y][tempblock->z]).percorrido = 1;
因此您的索引很可能越界了。检查你的逻辑。
注意 =
与 ==
:
if(tempblock->start = time){
应该是
if(tempblock->start == time){
另外,这段代码:
if(tempblock->x != -1){
for(x=0; x < parklot->width; x++){
for(y = 0; y < parklot->height; y++){
(parklot->floors[x][y][tempblock->z]).percorrido = 1;
}
}
}else{
(parklot->floors[tempblock->x][tempblock->y[tempblock>z]).percorrido = 1;
}
等于在说:
if(tempblock->x != -1){
/* Do some stuff here while tempblock->x is NOT -1. */
}else{
/* So in this block, tempblock->x IS EQUAL TO -1. */
(parklot->floors[tempblock->x][tempblock->y][tempblock->z]).percorrido = 1;
}
因此,为了清楚地说明离开我们的地方,我们有:
if(tempblock->x != -1){
/* Do some stuff here while tempblock->x is NOT -1. */
}else{
/* So in this block, tempblock->x IS EQUAL TO -1. */
(parklot->floors[-1][tempblock->y][tempblock->z]).percorrido = 1;
}
注意 parklot->floors[-1]
。你绝对是越界了。
好吧,我已经完成了休闲代码(基本上我想做的是:我有一个列表,其中有一些障碍物,如果 x=-1,则障碍物在整个楼层中,否则,它们是只在一个地方,所以我想每次通过代码时,障碍物都在那个位置):
void restartmatrix(parking * parklot, block * obstacles, int time) {
int x, y, z;
block * tempblock;
for (z = 0; z < parklot->depth; z++) {
for (x = 0; x < parklot->width; x++) {
for (y = 0; y < parklot->height; y++) {
(parklot - > floors[x][y][z]).percorrido = -1;
}
}
}
/*tempblock = obstacles;
while(tempblock != NULL){
if(tempblock->start = time){
if(tempblock->x != -1){
for(x=0; x < parklot->width; x++){
for(y = 0; y < parklot->height; y++){
(parklot->floors[x][y][tempblock->z]).percorrido = 1;
}
}
}else{
(parklot->floors[tempblock->x][tempblock->y][tempblock->z]).percorrido = 1;
}
}else if(tempblock->end == time && tempblock->end != 0){
if(tempblock->x != -1){
for(x=0; x < parklot->width; x++){
for(y = 0; y < parklot->height; y++){
(parklot->floors[x][y][tempblock->z]).percorrido = -1;
}
}
}else{
(parklot->floors[tempblock->x][tempblock->y][tempblock->z]).percorrido = -1;
}
}
tempblock = tempblock->next;
}*/
}
你能看到代码的某些部分被注释了吗....好吧,问题是如果取消注释,程序会给我一个双重释放或损坏错误(fasttop),但我似乎找不到为什么,因为我没有做任何免费的...
如有任何帮助,我们将不胜感激
编辑
我分配障碍列表的代码在这里,如果它可以帮助:
block * read_blocking_list(char * file){
FILE * fp;
char leitura[256];
char temporary;
block * head = NULL;
block * temp;
block * aux;
int start, end, x = 0, y = 0, z = 0, size;
fp = fopen(file, "r");
if(fp == NULL){
printf("Erro ao abrir o ficheiro de leitura\n");
exit(0);
}
while(fgets(leitura, 255, fp) != NULL){
temp = malloc(sizeof(block));
size = sscanf(leitura, "%c %d %d %d %d %d", &temporary, &start, &end, &x, &y, &z);
temp->start = start;
temp->end = end;
temp->next = NULL;
if(size < 5){
temp->x = -1;
temp->z = x;
}else{
temp->x = x;
temp->y = y;
temp->z = z;
}
if(head == NULL){
head = temp;
}else{
aux = head;
while(aux->next != NULL){
aux = aux->next;
}
aux->next = temp;
}
}
fclose(fp);
return head;
}
你测试是否 tempblock-x != -1
并在 else
块中使用 tempblock->x
这样:
(parklot->floors[tempblock->x][tempblock->y][tempblock->z]).percorrido = 1;
因此您的索引很可能越界了。检查你的逻辑。
注意 =
与 ==
:
if(tempblock->start = time){
应该是
if(tempblock->start == time){
另外,这段代码:
if(tempblock->x != -1){
for(x=0; x < parklot->width; x++){
for(y = 0; y < parklot->height; y++){
(parklot->floors[x][y][tempblock->z]).percorrido = 1;
}
}
}else{
(parklot->floors[tempblock->x][tempblock->y[tempblock>z]).percorrido = 1;
}
等于在说:
if(tempblock->x != -1){
/* Do some stuff here while tempblock->x is NOT -1. */
}else{
/* So in this block, tempblock->x IS EQUAL TO -1. */
(parklot->floors[tempblock->x][tempblock->y][tempblock->z]).percorrido = 1;
}
因此,为了清楚地说明离开我们的地方,我们有:
if(tempblock->x != -1){
/* Do some stuff here while tempblock->x is NOT -1. */
}else{
/* So in this block, tempblock->x IS EQUAL TO -1. */
(parklot->floors[-1][tempblock->y][tempblock->z]).percorrido = 1;
}
注意 parklot->floors[-1]
。你绝对是越界了。