It is All about Traffic, and Programming
0 visitors online now
0 guests, 0 members


Who touches my “for-loop” condition?

A for loop is a common control structure that allows repetitions of certain statements for a specific number of times. The syntax of a for-loop in C++ or C# is

for (initialization; condition; increment) {

As simple as ABC. As easy as a no-brainer. As happy as Clam.

Now, look at the following C++ code. How many times would the the loop execute?

int main( )
   int N = 10;

   for ( int i = 0; i < N; i++ ) {
     std::cout << "i = " << i 
               << ";"
               << "N= "  << N 
               << std::endl;

   return 0;

As the output shown below – The loop executes 5 times, even though we set N = 10.  Obviously, modifying N inside the loop body, i.e., N–, causes the termination of the loop prematurely.

Generally speaking, it is a bad practice to modify loop termination condition inside a for-loop. Bad, Bad, Bad!

Now, the same question for a similar Delphi code:


  I, N: Integer;

    N := 10;

    for I := 0 to N do
      WriteLn( Format('I=%d, N=%d',[I, N]) );
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);

The output is not so much unsurprising – unlike the C++ counterpart, it does loop 11 times:

Interesting. It seems modifying N inside loop body doesn’t impact the number of intended loop execution times.

Let’s check the disassembled C++ binary:

As seen from the highlighted lines, N is referenced by address (i.e., ptr [N]) in loop body. This means, any change to N’s value will eventually impact the termination condition evaluation. Address [ebp-14h] is the local variable i stored in Stack, which is incremented with every repetition of the loop, before comparing to ptr[N] that is decremented with each loop.

Delphi compiler generates something different:

N is addressed at [0x004258a8]. At the beginning of the loop, N’s value is copied to [$ebp-$14]. Each loop just checks [$ebp-$14] for termination. Modifying the original Nwouldn’t impact the loop termination condition evaluation.

So, though it is generally not a good practice to change for-loop termination condition inside the loop,  Delphi obviously has some compiler caveat that may slip the mind of a careless programmer. Be warned!

Lane Closure Utility Updated for VISSIM 9

Lane Closure Utility was initially developed in 2006 as an amateur/hobby work by a then graduate student.  Surprisingly,  it had been found useful,  and has been used by quite a few industry engineers, graduate students,  and academic researchers in their work.  I would like to thank them again for giving me feedback and inputs.

I just updated this free little utility to work with VISSIM latest 9.x version.

PTV made a LOT, really a LOT of changes to VISSIM COM interfaces – which is annoying but understandable given their grand plan of overhauling and refactoring the system’s application framework inside out –  that however made this upgrade quite eventful trying back and forth to figure out a lot of undocumented changes. Anyway,  I managed to just upgrade the code to match the latest VISSIM version COM interfaces.

It can be downloaded here: vissim9-dist-x86-x64  Feel free to download and distribute.

For those interested – my previous post about this little utility is here, so you can learn how to use it (easy and simple!)  Another big surprise to me as I found today is that – the original post has been read for almost 5000 times!  Wow.  I would be even happier  if you’d left me some comments,  good or bad.

Lane Closure Utility for VISSIM 5.40 (32bit and 64bit)


Midtown In Motion Covers More Intersections and Wins National Innovation Award

New York City Department of Transportation today announced the expansion of Midtown in Motion, the innovative congestion management system deployed in Manhattan since last year.

The first phase of Midtown in Motion has resulted in an overall 10% speeds improvement as suggested by the E-ZPass travel time data and taxi GPS data.   The initial deployment for real time congestion control included 100 microwave sensors, 32 traffic video cameras and E-ZPass readers at 23 intersections to measure in real time traffic speeds,  travel times,  traffic counts and occupancies.

This new expansion of Midtown in Motion will have  a total of 210 microwave sensors, 56 traffic cameras and E-ZPass readers at 59 intersections, covering the Midtown Manhattan area from 1st to 9th avenue and from 42nd to 57th street,  more than 270 square blocks  (more than 300 controlled intersections) .  The collected real time data are fed to sophisticated algorithms and signals are updated accordingly (operator-in-loop or autonomously).

Midtown In Motion has won ITS America Smart Solution Spotlight Award , in recognition of New York City’s commitment to “smart” technologies that allow engineers to respond to traffic conditions in real time.

 “From cameras to microwave sensors and EZ-pass readers to Advanced Solid State Traffic Controllers, Midtown in Motion is a showcase of the most sophisticated intelligent transportation solutions available to public agencies. ITS America is excited to recognize this comprehensive use of technology and real time data which has resulted in a highly successful deployment that can be replicated in cities throughout the country.”  – Scott Belcher, President and CEO of ITS America.

Details of the Press Release are available at the NYCDOT website and the ITS America website