输入int32_t用于循环变量声明

时间:2014-12-09 21:16:03

标签: c++

我有一个for循环声明一个int32_t类型的变量,我不确定为什么使用它而不是只输入int。我理解两者之间的区别,但我不确定这里是否需要int32_t。

  void AlarmProcess::AlarmProcess_alarm_Timer_1Sec()
{
    //Timer function called every 50ms
    static AlarmParams alarm_Params;
    //Update LED and buzzer
    //Buzzer is on for 1 second and off for 10 seconds then repeats.
    //Why define index as int32_t?
    for(int32_t i32_Index = 0;i32_Index < AlarmProcess_getInstance()->alarm_Parameter_List.Count; i32_Index++)
    {
        //Check for non latching mode
        if(AlarmProcess_getInstance()->alarm_Parameter_List.alarm_Parameter[i32_Index].Latching == false)
        {
            //Get channel number
            int32_t i32_Channel_Num = AlarmProcess_getInstance()->alarm_Parameter_List.alarm_Parameter[i32_Index].Channel_Number;
            //Get alarm detection type
            int32_t i32_Detection = AlarmProcess_getInstance()->alarm_Parameter_List.alarm_Parameter[i32_Index].Detection;
            //Check detection type and channel number mapping
            if((i32_Detection == HIGH && AlarmProcess_getInstance()->m_mapHighAlarmRaised[i32_Channel_Num] == true) ||
                (i32_Detection == LOW && AlarmProcess_getInstance()->m_mapLowAlarmRaised[i32_Channel_Num] == true) ||
                (i32_Detection == WINDOW_IN && AlarmProcess_getInstance()->m_mapWindowInAlarmRaised[i32_Channel_Num] == true) ||
                (i32_Detection == WINDOW_OUT && AlarmProcess_getInstance()->m_mapWindowOutAlarmRaised[i32_Channel_Num] == true))
            {
                // check for buzzer on or off
                if(s_bIsActivate == true)
                {
                    //Check for one second elapse
                    if((s_i32alarmTimerCount[i32_Channel_Num]) >= ONE_SECOND)
                    {
                        RETAILMSG(DEBUG_ALARM_PROCESS,(_T("Buzzer off:%d\r\n"),s_i32alarmTimerCount));
                        //Update counter variable
                        s_i32alarmTimerCount[i32_Channel_Num] = 0;

                        //Get alarm settings
                        AlarmProcess_getInstance()->b_AlarmProcess_GetAlarmsettingDetails(i32_Index,alarm_Params);

                        // Turn off the the buzzer
                        b_AlarmProcess_control_Buzzer(false,ZERO);
                        // update the flag
                        s_bIsActivate = false;
                    }
                    //Increment timer count variable
                    s_i32alarmTimerCount[i32_Channel_Num] += GPT_TIMER_RESOLUTION;
                }
                else
                {
                    //Check for 10 second elapse
                    if((s_i32alarmTimerCount[i32_Channel_Num]) >= TEN_SECOND)   
                    {
                        // Reinitialize the counter variable
                        s_i32alarmTimerCount[i32_Channel_Num] = 0;
                        RETAILMSG(DEBUG_ALARM_PROCESS,(_T("Buzzer ON:%d\r\n"),s_i32alarmTimerCount));

                        //Check if buzzer need to be enabled
                        if(s_bIsBuzzer == true)
                        {
                            //Enable buzzer
                            b_AlarmProcess_control_Buzzer(true,i32_Detection);

                        }

                        // update the flag
                        s_bIsActivate = true;
                    }
                    //increment timer count variable
                    s_i32alarmTimerCount[i32_Channel_Num] += GPT_TIMER_RESOLUTION;
                }
            }
        }
    }

并且Alarm_Parameter_List类如下:

class Alarm_Parameter_List
    {
    public :
        Alarm_Parameters alarm_Parameter[50]; 
        int Count; 
        Alarm_Parameter_List(){ Count = 0;} 
    };

    }

我只是不明白为什么使用int32_t而不是int。

2 个答案:

答案 0 :(得分:1)

在我看来,这只是一个糟糕的代码。应使用类型说明符Count声明数据成员int32_t,或者应使用类型int32_Index声明int(并具有其他名称)。否则,代码只会让读者感到困惑。

至于我,那么我会声明Count为类型size_t,前提是它可能不存储负数。

答案 1 :(得分:-1)

不要假设所有C平台都使用32位整数。此代码看起来可能是闹钟中的嵌入式控制器。嵌入式控制器更可能具有8位或可能16位处理器,具有较短的原生int类型。