Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable is not incrementing in C# Release x64

Tags:

c#

jit

64-bit

Could someone explain to me why this piece of code is doing well when I execute it on a x86 platform and why it fail on x64 ?

Results :

  • x86 Debug : 12345678910
  • x64 Debug : 12345678910
  • x86 Release : 12345678910
  • x64 Release : 1111111111

If I change something, like removing one of the unused variables, or if I remove the useless for-loop after p_lFirstId++, the strange behavior disappear.

I found that changing "pdb-only" to "full" in my release configuration, it's work again.

If you run the code directly from visual studio, it's doing well too.

Is this a JIT Compiler bug ?

Thank you in advance.

class Program
{
    static void Main(string[] args)
    {
        Test(null, null, null, 0, 1);            
    }

    public static void Test(
        List<string> liste, List<string> unused1,
        string unused2, int unused3, long p_lFirstId)
    {
        liste = new List<string>();

        StringBuilder sbSql = new StringBuilder();

        for (int i = 0 ; i < 10 ; i++)
        {
            sbSql.Append(p_lFirstId);
            p_lFirstId++;                

            foreach (string sColonne in liste)
            {

            }

        }

        System.Console.WriteLine(sbSql.ToString());
    } 
}
like image 664
Filimindji Avatar asked Jan 17 '11 16:01

Filimindji


People also ask

Can we increment static variable in C?

Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope.

What is an incrementing variable?

Incremental Variables. Incremental variables allow you to generate a sequence of numbers or dates to use in user scenarios. Each incremental variable has an initial value and an increment step and updates its value at certain times (for example, on each use).

How do you increment a variable by a number?

++i; --i; increment or decrement occurs before the variable's value is used in the remainder of the expression. increment or decrement occurs after the variable's value is used in the remainder of the expression.

What is incrementing in coding?

1. The process of increasing or decreasing a numeric value by another value. For example, incrementing 2 to 10 by the number 2 would be 2, 4, 6, 8, 10. 2. An increment is also a programming operator to increase the value of a numerical value.


1 Answers

This is a bug in the CLR. I would advise contacting Microsoft and asking them to correct this bug in their next release.

like image 148
bbosak Avatar answered Nov 09 '22 22:11

bbosak