我正在嘗試運行從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
'julian_day_calendar'是什麼類型? – Levi
一旦這些strncats可能覆蓋它。 – cnicutar
是的,你可以讓代碼更復雜,請。謝謝。 – trojanfoe