2015-05-08 180 views
0

我正在嘗試運行從2015年1月1日到當前日期的循環並填充數據庫中每一天的值。我正在使用Julian天計數器作爲循環變量。沒有哪裏在循環內我正在改變計數器的值,但循環2次後,計數器的值突然改變爲垃圾值。可能是什麼原因?循環變量的值突然變化

for (julian_day_counter = 2457024; julian_day_counter < curr_day; \ 
       julian_day_counter++) 
    { 
     for (slot_counter = 1; slot_counter < 24; slot_counter++) 
     { 
      consumption = (rand() % (CONSUMPTION_MAX_VAL - CONSUMPTION_MIN_VAL + 1)) \ 
        + CONSUMPTION_MIN_VAL; 

      /* Form current slot value column string based on current slot value */ 
      addr_port_strncpy((addr_u8bit_t *)slot_value_col, (addr_u8bit_t *)"slot_", \ 
       addr_port_strlen((addr_c8bit_t *)"slot_")); 
      ADDR_SPRINTF(temp_str, "%d", slot_counter); 
      addr_port_strncat((addr_s8bit_t *)slot_value_col, (addr_c8bit_t *)temp_str, \ 
       ADDR_SHORT_STR_LEN); 

      /* Comma separated Column names */ 
      addr_port_strncat((addr_s8bit_t *)col_name,(addr_c8bit_t *)slot_value_col, 
       ADDR_SHORT_STR_LEN); 
      addr_port_strncat((addr_s8bit_t *)col_name, \ 
       (addr_c8bit_t *)" ,",ADDR_SHORT_STR_LEN); 

      /* Comma separated Column values */ 
      ADDR_SPRINTF(temp_str, "%f ,", consumption); 
      addr_port_strncat((addr_s8bit_t *)col_value, 
      (addr_c8bit_t *)temp_str, ADDR_SHORT_STR_LEN); 
     } 
     addr_port_strncat((addr_s8bit_t *)col_name, \ 
       (addr_c8bit_t *)"julian_day",ADDR_SHORT_STR_LEN); 

     ADDR_SPRINTF(temp_str, "%d", julian_day_counter); 
     addr_port_strncat((addr_s8bit_t *)col_value, \ 
      (addr_c8bit_t *)temp_str, ADDR_SHORT_STR_LEN); 

     /* Insert row for consumption profile information in DB*/  
     if(ADDR_FAILURE == addr_db_local_insert(p_glb_pdb, 
       ADDR_APPLIANCE_CONSUMPTION_PROFILE_TABLE, (addr_c8bit_t *)col_name, 
       (addr_c8bit_t *)col_value)) 
     { 
      ret_val = ADDR_FAILURE; 
     } 
    } 

下面是gdb的片段:

Breakpoint 1, addr_db_update_dummy_appliance_consumption_profile (
    p_glb_pdb=0x812a4b0, curr_day=2457151) 
    at addr_db_app.c:13184 
13184  addr_s8bit_t   temp_str[ADDR_SHORT_STR_LEN] = {'\0'}; 
(gdb) watch julian_day_counter 
Hardware watchpoint 2: julian_day_counter 
(gdb) c 
Continuing. 
Hardware watchpoint 2: julian_day_counter 

Old value = 0 
New value = 2457024 
0x08088d55 in addr_db_update_dummy_appliance_consumption_profile (
    p_glb_pdb=0x812a4b0, curr_day=2457151) 
    at addr_db_app.c:13208 
13208  for (julian_day_counter = 2457024; julian_day_counter < curr_day; \ 
(gdb) c 
Continuing. 
Hardware watchpoint 2: julian_day_counter 

Old value = 2457024 
New value = 2457025 
0x08088f2d in addr_db_update_dummy_appliance_consumption_profile (
    p_glb_pdb=0x812a4b0, curr_day=2457151) 
    at addr_db_app.c:13208 
13208  for (julian_day_counter = 2457024; julian_day_counter < curr_day; \ 
(gdb) c 
Continuing. 
Hardware watchpoint 2: julian_day_counter 

Old value = 2457025 
New value = 2456880 
0x00b6a33b in strncat() from /lib/libc.so.6 
+2

'julian_day_calendar'是什麼類型? – Levi

+2

一旦這些strncats可能覆蓋它。 – cnicutar

+1

是的,你可以讓代碼更復雜,請。謝謝。 – trojanfoe

回答

0

真正的線索這裏:

0x00b6a33b在strncat函數()從/lib/libc.so.6

您的值在某些其他函數調用中被修改,導致strncat。但是,現在這個問題的代碼並不是問題所在。 :)

+0

那麼解決方案是什麼? (這非常簡單),並且很好地完成了@undur_gongor原來的指出。 – trojanfoe

+0

對不起,但仍然無法得到.. julian_day_counter甚至沒有在strncat函數中使用。 –

+0

@undur_gongor這些顯然是某種封裝函數。它們很可能是問題所在,但是在調用strncat之前和之後,如果沒有看到包裝函數在傳入的參數中做了什麼,就無法說出導致行爲的原因。 –

1

做得很好@undur_gongor指出這個問題在你的問題的評論;罪魁禍首是strncat()。它正在覆蓋緩衝區並破壞其他變量。你有一個緩衝區溢出。

您似乎沒有正確傳遞第三個參數; from this question

#define BUFFER_SIZE 64 
char buff[BUFFER_SIZE]; 

//Use strncpy 
strncpy(buff, "String 1", BUFFER_SIZE - 1); 
buff[BUFFER_SIZE - 1] = '\0'; 

strncat(buff, "String 2", BUFFER_SIZE - strlen(buff) - 1); 

strncat(buff, "String 3", BUFFER_SIZE - strlen(buff) - 1);