I have a program that I built for RHEL5 32 bit and ubuntu10 64 bit (c++ qt4.6). When I run the program on ubuntu, all the variables are initialized without me needing to code this initialization. But When I run the program on RHEL, some of the variables are not initialized, I have noticed that they are mostly integer type and the typicial values are around 154280152. The funny thing is that it just happens in a few classes. How can this be?
update: here is a snippet of code, it is the header of one of the classes where this is happening(sorry for the layout I am looking into that right now):
#ifndef FCP_CONFIG_H
#define FCP_CONFIG_H
#include "ui_fcpConfig.h"
#include
#include "fpsengine.h"
#include "fcp_cfg_delegate.h"
#define SET_COL 3
#define GLOBAL_KEY_COL 2
#define LOCAL_KEY_COL 1
#define ENABLE_COL 0
namespace Ui
{
class fcpConfig;
}
class fcpConfig : public QWidget
{
Q_OBJECT
public:
fcpConfig(QWidget *parent, FPSengine * FPS);
Ui::fcpConfigForm ui;
void setupFcpCfg();
private:
QWidget * myParent;
FPSengine * myFPS;
fcpCfgDelegate delegate;
QList<QSpinBox*>failOrderList;
QList<QRadioButton*>primaryList;
int numFCP;
QList<int>numFcpInEachSet;
int currentSet;
void updateSets();
void refreshFailorderDuringUserEdit(int fcpPos);
QSignalMapper * signalMapper;
QMutex mutex;
void sendSysStatusMsgAndPopup(QString msg);
int curSet; //the connected Fcp's Set
private slots:
void updateFcpFailOrderSpinBox(int absPos);
void on_twFCP_cellClicked( int row, int column );
void on_buttonBox_clicked(QAbstractButton* button);
private:
template <class T>
void buildObjList(QObject * location,QList<T> *cmdEleList,QString objName, int numObj){
T pCmdEle;
cmdEleList->clear();
for(int i=0;i<numObj;i++){
pCmdEle = location->findChild<T>(objName+QString("%1").arg(i+1));
cmdEleList->append(pCmdEle);
}
}
//used to send SysStatus and popuMsg when number of active Fcps in Set not 1
QString activeList; //build a string representing Fcp numbers that are active.
int iNumActive;
};
#endif // FCP_CONFIG_H
-
Different compilers do different things. The standard doesn't state that all variables should be initialized automatically, so many compilers don't. This means they are typically filled with garbage. Sometimes you luck out and get a block of zeros, but it's rare. Don't count on it.
John Gaughan : Always, always, always initialize variables. Then, even if your code is incorrect, at least it is also consistent.karlphillip : Initialize variables! Your life may depend on it.PigBen : @yan The standard doesn't state that it must be random either.Throwback1986 : @yan: It's a good practice to initialize all variables from the start.TheUndeadFish : @yan What kind of abnormal behavior do you imagine an uninitialized variable could actually help you detect? I can't think of a case where using the value of an uninitialized variable wouldn't be a problem in its own right and in need of being solved before anything else useful could be accomplished with it.yan bellavance : @TheUndeadFish this is exactly the reason why I ended up asking this question. For example: if(varMIN) {assign your variable} else {do not assign your variable but you still should} otherwise I would of used an initialized value that is not necessarily the value I want. JoshD : @yan bellavance: I didn't say anything about random events. I said some compilers will initialize some things, others won't. Don't be misled by that block of zeros comment.
0 comments:
Post a Comment