C内存访问错误结果

C memory access wrong results

我对结构数组中的数组有疑问。我的问题是,在分配数组后,数据可以毫无问题地读取,但一段时间后我得到了错误的结果。 这是我的代码:

struct time_on_off {
    struct tm time_on;
    struct tm time_off;
};

struct device {
    int mysqlid;
    int port;
    char name[12];
    struct tm time_on;
    struct tm time_off;
    struct time_on_off *time_on_off;
    int automatic;
    int state;
    struct tm timer;
    int man;
    int numtimers;
};

struct device devices[8];
char sqlquery[150];


int update(){

   MYSQL_RES *res;
   MYSQL_ROW row;

   day=currenttime->tm_wday;
   day=(day==0)?7:day;

   printf("Updating ... %d \n", day);
   res=connectsql("SELECT * FROM  `devices` AS A INNER JOIN manual AS B ON A.id = B.id");

   for (int i=0;(row = mysql_fetch_row(res)) != NULL;i++) {

      devices[i].mysqlid=atoi(row[0]);
      devices[i].port=atoi(row[3]);
      devices[i].automatic=1;
      devices[i].man=0;
      devices[i].timer.tm_min=0;
      devices[i].timer.tm_hour=0;
      strcpy(devices[i].name,row[1]);
      devices[i].state=atoi(row[5]);
      devices[i].numtimers=0;

      if(row[7]!=NULL){

        strptime(row[7],"%H:%M:%S", &devices[i].timer);
        devices[i].automatic=0;
        devices[i].man=atoi(row[6]);

      }
   }

  mysql_close(conn);

  for (int i=0; i<(sizeof(devices)/sizeof(struct device)); i++){

    sprintf(sqlquery,"SELECT * FROM  `timer` WHERE  `dayid` =  '%i' AND  `deviceid` =  '%i'",day,devices[i].mysqlid);
    res=connectsql(sqlquery);

    for (int o=0;(row = mysql_fetch_row(res))!= NULL;o++) {

      devices[i].time_on_off = (struct time_on_off *)  realloc(devices[i].time_on_off, devices[i].numtimers+1);
      strptime(row[2],"%H:%M:%S", &devices[i].time_on_off[o].time_on);
      strptime(row[3],"%H:%M:%S", &devices[i].time_on_off[o].time_off);
      printf("On: %i:%i \n", devices[i].time_on_off[o].time_on.tm_hour,devices[i].time_on_off[o].time_on.tm_min);
      printf("Off: %i:%i \n", devices[i].time_on_off[o].time_off.tm_hour,devices[i].time_on_off[o].time_off.tm_min);
     devices[i].numtimers++;
     devices[i].automatic=1;

     }
    mysql_close(conn);
  }

  for (int i=0; i<(sizeof(devices)/sizeof(struct device)); i++){

      printf("Name: %s \n", devices[i].name);
      printf("Port: %i \n", devices[i].port);
      printf("Automatic: %i \n", devices[i].automatic);
      printf("man: %i \n", devices[i].man);
      printf("Timer: %i:%i \n", devices[i].timer.tm_hour,devices[i].timer.tm_min);

      for (int o=0; o<devices[i].numtimers; o++){

           printf("On: %i:%i \n", devices[i].time_on_off[o].time_on.tm_hour,devices[i].time_on_off[o].time_on.tm_min);
           printf("Off: %i:%i \n", devices[i].time_on_off[o].time_off.tm_hour,devices[i].time_on_off[o].time_off.tm_min);
      }
  }
return 0;
}

输出是:

Updating ... 2 
On: 14:0 
Off: 16:0 <- thats the correct value
On: 16:0 
Off: 18:0 
On: 17:0 
Off: 19:0 
On: 15:8 
Off: 16:9 
Name: Poolpumpe 
Port: 0 
Automatic: 1 
man: 0 
Timer: 0:0 
On: 14:0 
Off: 1936681068:1633906540 <- here seems to be the error
Name: Poollicht 
Port: 1 
...

有人对我有想法吗?谢谢

你的(重新)分配是错误的,你没有为 x 条目分配 space,你只分配了 x 字节:

devices[i].time_on_off = realloc(devices[i].time_on_off, devices[i].numtimers+1);

您的意图可能是为 o+1 个条目提供足够的 space(在您的情况下为 o == devices[i].numtimers)。所以它应该是这样的:

devices[i].time_on_off = realloc(devices[i].time_on_off, sizeof(struct time_on_off) * (o+1));

或:

devices[i].numtimers++;
devices[i].time_on_off = realloc(devices[i].time_on_off, sizeof(struct time_on_off) * devices[i].numtimers);

另外,don't cast the result pointer from malloc and realloc.