Skip to content

[ZH] Fix loop index comparison warnings #804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void PartitionSolver::solve(void)

Int minSizeForAllData = 0;
Int slotsAllotted = 0;
Int i, j;
size_t i, j;

// first, determine whether there is an actual solution, or we're going to have to fudge it.
for (i = 0; i < m_data.size(); ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,7 @@ Bool AcademyStats::calculateAcademyAdvice( AcademyAdviceInfo *info )
info->numTips = 0;

//Build the header for each string.
for( Int i = 0; i < maxAdviceTips; i++ )
for( UnsignedInt i = 0; i < maxAdviceTips; i++ )
{
info->advice[ i ].format( UnicodeString( L"\n\n" ) );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void ProductionPrerequisite::init()
//=============================================================================
void ProductionPrerequisite::resolveNames()
{
for (Int i = 0; i < m_prereqUnits.size(); i++)
for (size_t i = 0; i < m_prereqUnits.size(); i++)
{

//
Expand Down Expand Up @@ -121,7 +121,7 @@ Int ProductionPrerequisite::calcNumPrereqUnitsOwned(const Player *player, Int co
Int ProductionPrerequisite::getAllPossibleBuildFacilityTemplates(const ThingTemplate* tmpls[], Int maxtmpls) const
{
Int count = 0;
for (int i = 0; i < m_prereqUnits.size(); i++)
for (size_t i = 0; i < m_prereqUnits.size(); i++)
{
if (i > 0 && !(m_prereqUnits[i].flags & UNIT_OR_WITH_PREV))
break;
Expand Down Expand Up @@ -216,7 +216,7 @@ void ProductionPrerequisite::addUnitPrereq( AsciiString unit, Bool orUnitWithPre
void ProductionPrerequisite::addUnitPrereq( const std::vector<AsciiString>& units )
{
Bool orWithPrevious = false;
for (int i = 0; i < units.size(); ++i)
for (size_t i = 0; i < units.size(); ++i)
{
addUnitPrereq(units[i], orWithPrevious);
orWithPrevious = true;
Expand Down
6 changes: 3 additions & 3 deletions GeneralsMD/Code/GameEngine/Source/Common/RTS/SpecialPower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ SpecialPowerStore::~SpecialPowerStore( void )
{

// delete all templates
for( Int i = 0; i < m_specialPowerTemplates.size(); ++i )
for( size_t i = 0; i < m_specialPowerTemplates.size(); ++i )
m_specialPowerTemplates[ i ]->deleteInstance();

// erase the list
Expand All @@ -272,7 +272,7 @@ SpecialPowerTemplate* SpecialPowerStore::findSpecialPowerTemplatePrivate( AsciiS
{

// search the template list for matching name
for( Int i = 0; i < m_specialPowerTemplates.size(); ++i )
for( size_t i = 0; i < m_specialPowerTemplates.size(); ++i )
if( m_specialPowerTemplates[ i ]->getName() == name )
return m_specialPowerTemplates[ i ];

Expand All @@ -287,7 +287,7 @@ const SpecialPowerTemplate *SpecialPowerStore::findSpecialPowerTemplateByID( Uns
{

// search the template list for matching name
for( Int i = 0; i < m_specialPowerTemplates.size(); ++i )
for( size_t i = 0; i < m_specialPowerTemplates.size(); ++i )
if( m_specialPowerTemplates[ i ]->getID() == id )
return m_specialPowerTemplates[ i ];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ static void findHighFileNumber( AsciiString filename, void *userData )

// strip off the extension at the end of the filename
AsciiString nameOnly = filename;
for( Int count = 0; count < strlen( SAVE_GAME_EXTENSION ); count++ )
for( size_t count = 0; count < strlen( SAVE_GAME_EXTENSION ); count++ )
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this will call strlen on every iteration...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah i think the VC6 version may do that but modern MSVC would likely compile it out as a constant.

nameOnly.removeLastChar();

// convert filename (which is only numbers) to a number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3371,7 +3371,7 @@ void ControlBar::populateSpecialPowerShortcut( Player *player)
//button specifying a vector of sciences in the command button.
Int bestIndex = -1;
ScienceType science;
for( Int scienceIndex = 0; scienceIndex < commandButton->getScienceVec().size(); ++scienceIndex )
for( size_t scienceIndex = 0; scienceIndex < commandButton->getScienceVec().size(); ++scienceIndex )
{
science = commandButton->getScienceVec()[ scienceIndex ];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ void ControlBar::populateCommand( Object *obj )
//button specifying a vector of sciences in the command button.
Int bestIndex = -1;
ScienceType science;
for( Int scienceIndex = 0; scienceIndex < commandButton->getScienceVec().size(); ++scienceIndex )
for( size_t scienceIndex = 0; scienceIndex < commandButton->getScienceVec().size(); ++scienceIndex )
{
science = commandButton->getScienceVec()[ scienceIndex ];

Expand Down Expand Up @@ -1237,7 +1237,7 @@ CommandAvailability ControlBar::getCommandAvailability( const CommandButton *com
if( TheUpgradeCenter->canAffordUpgrade( player, command->getUpgradeTemplate() ) == FALSE )
return COMMAND_RESTRICTED;//COMMAND_CANT_AFFORD;

for( Int i = 0; i < command->getScienceVec().size(); i++ )
for( size_t i = 0; i < command->getScienceVec().size(); i++ )
{
ScienceType st = command->getScienceVec()[ i ];
if( !player->hasScience( st ) )
Expand Down Expand Up @@ -1273,7 +1273,7 @@ CommandAvailability ControlBar::getCommandAvailability( const CommandButton *com
if( TheUpgradeCenter->canAffordUpgrade( player, command->getUpgradeTemplate() ) == FALSE )
return COMMAND_RESTRICTED;//COMMAND_CANT_AFFORD;

for( Int i = 0; i < command->getScienceVec().size(); i++ )
for( size_t i = 0; i < command->getScienceVec().size(); i++ )
{
ScienceType st = command->getScienceVec()[ i ];
if( !player->hasScience( st ) )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ CBCommandStatus ControlBar::processCommandUI( GameWindow *control,

ScienceType st = SCIENCE_INVALID;
Player *player = ThePlayerList->getLocalPlayer();
for(Int i = 0; i < commandButton->getScienceVec().size(); ++i)
for(size_t i = 0; i < commandButton->getScienceVec().size(); ++i)
{
st = commandButton->getScienceVec()[ i ];
if(!player->hasScience(st) && TheScienceStore->playerHasPrereqsForScience(player, st) && TheScienceStore->getSciencePurchaseCost(st) <= player->getSciencePurchasePoints())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ void ControlBar::populateBuildTooltipLayout( const CommandButton *commandButton,
{
if( commandButton->getScienceVec().size() > 1 )
{
for(Int j = 0; j < commandButton->getScienceVec().size(); ++j)
for(size_t j = 0; j < commandButton->getScienceVec().size(); ++j)
{
st = commandButton->getScienceVec()[ j ];

Expand Down Expand Up @@ -493,7 +493,7 @@ void ControlBar::populateBuildTooltipLayout( const CommandButton *commandButton,
{

//Do we have a prerequisite science?
for( Int i = 0; i < commandButton->getScienceVec().size(); i++ )
for( size_t i = 0; i < commandButton->getScienceVec().size(); i++ )
{
ScienceType st = commandButton->getScienceVec()[ i ];
if( !player->hasScience( st ) )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1511,7 +1511,7 @@ void populatePlayerInfo( Player *player, Int pos)
AcademyAdviceInfo info;
if( player->getAcademyStats()->calculateAcademyAdvice( &info ) )
{
for( Int i = 0; i < info.numTips; i++ )
for( UnsignedInt i = 0; i < info.numTips; i++ )
{
GadgetListBoxAddEntryText( listboxAcademyWindowScoreScreen, info.advice[ i ], GameSpyColor[GSCOLOR_DEFAULT], -1 );
}
Expand Down
4 changes: 2 additions & 2 deletions GeneralsMD/Code/GameEngine/Source/GameClient/GameClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ GameClient::~GameClient()
TheVideoPlayer = NULL;

// destroy all translators
for( Int i = 0; i < m_numTranslators; i++ )
for( UnsignedInt i = 0; i < m_numTranslators; i++ )
TheMessageStream->removeTranslator( m_translators[ i ] );
m_numTranslators = 0;
m_commandTranslator = NULL;
Expand Down Expand Up @@ -1124,7 +1124,7 @@ void GameClient::preloadAssets( TimeOfDay timeOfDay )

GlobalMemoryStatus(&before);
extern std::vector<AsciiString> debrisModelNamesGlobalHack;
Int i=0;
size_t i=0;
for (; i<debrisModelNamesGlobalHack.size(); ++i)
{
TheDisplay->preloadModelAssets(debrisModelNamesGlobalHack[i]);
Expand Down
4 changes: 2 additions & 2 deletions GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3491,7 +3491,7 @@ void InGameUI::postDraw( void )
UnsignedByte r, g, b, a;
GameGetColorComponents( m_militarySubtitle->color, &r, &g, &b, &a );
dropColor = GameMakeColor( 0, 0, 0, a );
for(Int i = 0; i <= m_militarySubtitle->currentDisplayString; i++)
for(UnsignedInt i = 0; i <= m_militarySubtitle->currentDisplayString; i++)
{
m_militarySubtitle->displayStrings[i]->draw(pos.x,pos.y, m_militarySubtitle->color,dropColor );
Int height;
Expand Down Expand Up @@ -4123,7 +4123,7 @@ void InGameUI::removeMilitarySubtitle( void )
TheInGameUI->clearTooltipsDisabled();

// loop through and free up the display strings
for(Int i = 0; i <= m_militarySubtitle->currentDisplayString; i ++)
for(UnsignedInt i = 0; i <= m_militarySubtitle->currentDisplayString; i ++)
{
TheDisplayStringManager->freeDisplayString(m_militarySubtitle->displayStrings[i]);
m_militarySubtitle->displayStrings[i] = NULL;
Expand Down
2 changes: 1 addition & 1 deletion GeneralsMD/Code/GameEngine/Source/GameClient/MapUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ Bool MapCache::loadUserMaps()
{
AsciiString endingStr;
AsciiString fname = s+1;
for (Int i=0; i<strlen(mapExtension); ++i)
for (size_t i=0; i<strlen(mapExtension); ++i)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another strlen in condition...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah... i didn't want to change too much.

fname.removeLastChar();

endingStr.format("%s\\%s%s", fname.str(), fname.str(), mapExtension);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4676,7 +4676,7 @@ GameMessageDisposition CommandTranslator::translateGameMessage(const GameMessage
{
// cheese festival: do NOT imitate this code. it is for debug purposes only.
std::vector<AsciiString> v = TheScienceStore->friend_getScienceNames();
for (int i = 0; i < v.size(); ++i)
for (size_t i = 0; i < v.size(); ++i)
{
ScienceType st = TheScienceStore->getScienceFromInternalName(v[i]);
if (st != SCIENCE_INVALID && TheScienceStore->isScienceGrantable(st))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ void CountermeasuresBehavior::launchVolley()
Object *obj = getObject();

Real volleySize = (Real)data->m_volleySize;
for( int i = 0; i < data->m_volleySize; i++ )
for( UnsignedInt i = 0; i < data->m_volleySize; i++ )
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope this will produce the same float value below.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory it should as the raw binary values of positive signed int and unsigned int values are the same up to 2.7 billion at least.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes 34% of assembler in CountermeasuresBehaviour::launchVolley.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting how it changes that much when the index is only used in one place.

{
//Each flare in a volley will calculate a different vector to fly out. We have a +/- angle to
//spread out equally. With only one flare, it'll come straight out the back. Two flares will
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ UpdateSleepTime DumbProjectileBehavior::update()
void DumbProjectileBehavior::displayFlightPath()
{
extern void addIcon(const Coord3D *pos, Real width, Int numFramesDuration, RGBColor color);
for( Int pointIndex = 0; pointIndex < m_flightPath.size(); ++pointIndex )
for( size_t pointIndex = 0; pointIndex < m_flightPath.size(); ++pointIndex )
{
addIcon(&m_flightPath[pointIndex], TheGlobalData->m_debugProjectileTileWidth,
TheGlobalData->m_debugProjectileTileDuration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ void InstantDeathBehavior::onDie( const DamageInfo *damageInfo )

const InstantDeathBehaviorModuleData* d = getInstantDeathBehaviorModuleData();

Int idx, listSize;
size_t idx, listSize;

listSize = d->m_fx.size();
if (listSize > 0)
{
idx = GameLogicRandomValue(0, listSize-1);
idx = (size_t)GameLogicRandomValue(0, listSize-1);
const FXListVec& v = d->m_fx;
DEBUG_ASSERTCRASH(idx>=0&&idx<v.size(),("bad idx"));
const FXList* fxl = v[idx];
Expand All @@ -151,7 +151,7 @@ void InstantDeathBehavior::onDie( const DamageInfo *damageInfo )
listSize = d->m_ocls.size();
if (listSize > 0)
{
idx = GameLogicRandomValue(0, listSize-1);
idx = (size_t)GameLogicRandomValue(0, listSize-1);
const OCLVec& v = d->m_ocls;
DEBUG_ASSERTCRASH(idx>=0&&idx<v.size(),("bad idx"));
const ObjectCreationList* ocl = v[idx];
Expand All @@ -161,7 +161,7 @@ void InstantDeathBehavior::onDie( const DamageInfo *damageInfo )
listSize = d->m_weapons.size();
if (listSize > 0)
{
idx = GameLogicRandomValue(0, listSize-1);
idx = (size_t)GameLogicRandomValue(0, listSize-1);
const WeaponTemplateVec& v = d->m_weapons;
DEBUG_ASSERTCRASH(idx>=0&&idx<v.size(),("bad idx"));
const WeaponTemplate* wt = v[idx];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Bool UnitCrateCollide::executeCrateBehavior( Object *other )
return FALSE;
}

for( Int unitIndex = 0; unitIndex < unitCount; unitIndex++ )
for( UnsignedInt unitIndex = 0; unitIndex < unitCount; unitIndex++ )
{
Team *creationTeam = other->getControllingPlayer()->getDefaultTeam();
Object *newObj = TheThingFactory->newObject( unitType, creationTeam );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2745,7 +2745,7 @@ void LocomotorSet::xferSelfAndCurLocoPtr(Xfer *xfer, Locomotor** loco)
}
else
{
for (int i = 0; i < m_locomotors.size(); ++i)
for (size_t i = 0; i < m_locomotors.size(); ++i)
{
if (m_locomotors[i]->getTemplateName() == name)
{
Expand All @@ -2763,7 +2763,7 @@ void LocomotorSet::xferSelfAndCurLocoPtr(Xfer *xfer, Locomotor** loco)
//-------------------------------------------------------------------------------------------------
void LocomotorSet::clear()
{
for (int i = 0; i < m_locomotors.size(); ++i)
for (size_t i = 0; i < m_locomotors.size(); ++i)
{
if (m_locomotors[i])
m_locomotors[i]->deleteInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ class DeliverPayloadNugget : public ObjectCreationNugget
}

Object *firstTransport = NULL;
for( Int formationIndex = 0; formationIndex < m_formationSize; formationIndex++ )
for( UnsignedInt formationIndex = 0; formationIndex < m_formationSize; formationIndex++ )
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This innocent looking edit changes 13% of the assembler inside DeliverPayloadNugget::create.

image

{
Coord3D offset;
offset.zero();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ Bool AIUpdateInterface::chooseLocomotorSetExplicit(LocomotorSetType wst)
{
m_locomotorSet.clear();
m_curLocomotor = NULL;
for (Int i = 0; i < set->size(); ++i)
for (size_t i = 0; i < set->size(); ++i)
{
const LocomotorTemplate* lt = set->at(i);
if (lt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ class ChinookCombatDropState : public State
}

UnsignedInt now = TheGameLogic->getFrame();
for (Int i = 0; i < m_ropes.size(); ++i)
for (size_t i = 0; i < m_ropes.size(); ++i)
{
if (m_ropes[i].ropeDrawable)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ DockUpdate::DockUpdate( Thing *thing, const ModuleData* moduleData ) : UpdateMod
m_approachPositionReached.resize(DEFAULT_APPROACH_VECTOR_SIZE);
}

for( Int vectorIndex = 0; vectorIndex < m_approachPositions.size(); ++vectorIndex )
for( size_t vectorIndex = 0; vectorIndex < m_approachPositions.size(); ++vectorIndex )
{
// Whatever size we are, init everything.
m_approachPositions[vectorIndex].zero();
Expand All @@ -118,7 +118,7 @@ Bool DockUpdate::isClearToApproach( Object const* docker ) const

ObjectID dockerID = docker->getID();

for( Int positionIndex = 0; positionIndex < m_approachPositionOwners.size(); ++positionIndex )
for( size_t positionIndex = 0; positionIndex < m_approachPositionOwners.size(); ++positionIndex )
{
if( m_approachPositionOwners[positionIndex] == INVALID_ID )
{
Expand Down Expand Up @@ -315,7 +315,7 @@ void DockUpdate::getExitPosition( Object* docker, Coord3D *position )
void DockUpdate::onApproachReached( Object* docker )
{
ObjectID dockerID = docker->getID();
for( Int positionIndex = 0; positionIndex < m_approachPositionOwners.size(); ++positionIndex )
for( size_t positionIndex = 0; positionIndex < m_approachPositionOwners.size(); ++positionIndex )
{
if( m_approachPositionOwners[positionIndex] == dockerID )
{
Expand All @@ -335,7 +335,7 @@ void DockUpdate::onEnterReached( Object* docker )
m_dockerInside = TRUE;

ObjectID dockerID = docker->getID();
for( Int positionIndex = 0; positionIndex < m_approachPositionOwners.size(); ++positionIndex )
for( size_t positionIndex = 0; positionIndex < m_approachPositionOwners.size(); ++positionIndex )
{
if( m_approachPositionOwners[positionIndex] == dockerID )
{
Expand Down Expand Up @@ -381,7 +381,7 @@ void DockUpdate::onExitReached( Object* docker )
void DockUpdate::cancelDock( Object* docker )
{
ObjectID dockerID = docker->getID();
for( Int positionIndex = 0; positionIndex < m_approachPositionOwners.size(); ++positionIndex )
for( size_t positionIndex = 0; positionIndex < m_approachPositionOwners.size(); ++positionIndex )
{
if( m_approachPositionOwners[positionIndex] == dockerID )
{
Expand Down Expand Up @@ -418,7 +418,7 @@ UpdateSleepTime DockUpdate::update()
if( m_activeDocker == INVALID_ID && !m_dockCrippled )
{
// if setDockCrippled has been called, I will never give enterance permission.
for( Int positionIndex = 0; positionIndex < m_approachPositionReached.size(); ++positionIndex )
for( size_t positionIndex = 0; positionIndex < m_approachPositionReached.size(); ++positionIndex )
{
if( m_approachPositionReached[positionIndex] )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ void EMPUpdate::doDisableAttack( void )

UnsignedInt emitterCount = MAX(15, REAL_TO_INT_CEIL(data->m_sparksPerCubicFoot * victimVolume));

for (Int e = 0 ; e < emitterCount; ++e)
for (UnsignedInt e = 0 ; e < emitterCount; ++e)
{

ParticleSystem *sys = TheParticleSystemManager->createParticleSystem(tmp);
Expand Down
Loading
Loading