Why does stackalloc initialization have inconsistent behavior?












9














The following code initializes two stackalloc arrays with non-zero values. While array A is properly initialized, array B remains filled with zeroes, contrary to what is expected.



By disassembling the compiled executable, one can see that no initialization code is generated for array B. Why is that?



using System;
namespace ConsoleApp1
{
class Program
{
static unsafe void Main(string args)
{
double a1 = 1;
double* A = stackalloc double { a1, 0, 0, a1, a1 }; // results in 1 0 0 1 1
double* B = stackalloc double { a1, 0, 0, 0, 0}; // results in 0 0 0 0 0

for (int i = 0; i < 5; i++) Console.Write($"{A[i]} ");
Console.WriteLine();
for (int i = 0; i < 5; i++) Console.Write($"{B[i]} ");
}
}
}


Expected results:



1 0 0 1 1



1 0 0 0 0



Actual results:



1 0 0 1 1



0 0 0 0 0










share|improve this question




















  • 1




    @ChrisF why would you expect replacing i with j would make a difference? (it doesn't, but curious)
    – Kirk Woll
    3 hours ago






  • 1




    This seems really peculiar to me. (i.e. I'm ready to learn something ;) ) but replacing a1 with the literal 1 in the second one produces entirely different results (it works).
    – Kirk Woll
    3 hours ago








  • 2




    @KirkWoll - I wasn't really expecting anything, but wanted to rule out any possible strangeness with reusing the variable. I've had cases where despite scoping indicating otherwise, variables and their values have persisted outside loops. In this case I wanted to check that the code was actually writing out each element of B and not just ,e.g., B[4]
    – ChrisF
    3 hours ago






  • 2




    Looks like a bug in the compiler. As far as I can see, it triggered if you have 3 or more zero constant expressions, and no non-zero constant expressions.
    – PetSerAl
    3 hours ago






  • 2




    I've attempted a 🦇 signal to @jaredpar. We'll see if he's ready to come to our aid. ;)
    – Kirk Woll
    3 hours ago
















9














The following code initializes two stackalloc arrays with non-zero values. While array A is properly initialized, array B remains filled with zeroes, contrary to what is expected.



By disassembling the compiled executable, one can see that no initialization code is generated for array B. Why is that?



using System;
namespace ConsoleApp1
{
class Program
{
static unsafe void Main(string args)
{
double a1 = 1;
double* A = stackalloc double { a1, 0, 0, a1, a1 }; // results in 1 0 0 1 1
double* B = stackalloc double { a1, 0, 0, 0, 0}; // results in 0 0 0 0 0

for (int i = 0; i < 5; i++) Console.Write($"{A[i]} ");
Console.WriteLine();
for (int i = 0; i < 5; i++) Console.Write($"{B[i]} ");
}
}
}


Expected results:



1 0 0 1 1



1 0 0 0 0



Actual results:



1 0 0 1 1



0 0 0 0 0










share|improve this question




















  • 1




    @ChrisF why would you expect replacing i with j would make a difference? (it doesn't, but curious)
    – Kirk Woll
    3 hours ago






  • 1




    This seems really peculiar to me. (i.e. I'm ready to learn something ;) ) but replacing a1 with the literal 1 in the second one produces entirely different results (it works).
    – Kirk Woll
    3 hours ago








  • 2




    @KirkWoll - I wasn't really expecting anything, but wanted to rule out any possible strangeness with reusing the variable. I've had cases where despite scoping indicating otherwise, variables and their values have persisted outside loops. In this case I wanted to check that the code was actually writing out each element of B and not just ,e.g., B[4]
    – ChrisF
    3 hours ago






  • 2




    Looks like a bug in the compiler. As far as I can see, it triggered if you have 3 or more zero constant expressions, and no non-zero constant expressions.
    – PetSerAl
    3 hours ago






  • 2




    I've attempted a 🦇 signal to @jaredpar. We'll see if he's ready to come to our aid. ;)
    – Kirk Woll
    3 hours ago














9












9








9


4





The following code initializes two stackalloc arrays with non-zero values. While array A is properly initialized, array B remains filled with zeroes, contrary to what is expected.



By disassembling the compiled executable, one can see that no initialization code is generated for array B. Why is that?



using System;
namespace ConsoleApp1
{
class Program
{
static unsafe void Main(string args)
{
double a1 = 1;
double* A = stackalloc double { a1, 0, 0, a1, a1 }; // results in 1 0 0 1 1
double* B = stackalloc double { a1, 0, 0, 0, 0}; // results in 0 0 0 0 0

for (int i = 0; i < 5; i++) Console.Write($"{A[i]} ");
Console.WriteLine();
for (int i = 0; i < 5; i++) Console.Write($"{B[i]} ");
}
}
}


Expected results:



1 0 0 1 1



1 0 0 0 0



Actual results:



1 0 0 1 1



0 0 0 0 0










share|improve this question















The following code initializes two stackalloc arrays with non-zero values. While array A is properly initialized, array B remains filled with zeroes, contrary to what is expected.



By disassembling the compiled executable, one can see that no initialization code is generated for array B. Why is that?



using System;
namespace ConsoleApp1
{
class Program
{
static unsafe void Main(string args)
{
double a1 = 1;
double* A = stackalloc double { a1, 0, 0, a1, a1 }; // results in 1 0 0 1 1
double* B = stackalloc double { a1, 0, 0, 0, 0}; // results in 0 0 0 0 0

for (int i = 0; i < 5; i++) Console.Write($"{A[i]} ");
Console.WriteLine();
for (int i = 0; i < 5; i++) Console.Write($"{B[i]} ");
}
}
}


Expected results:



1 0 0 1 1



1 0 0 0 0



Actual results:



1 0 0 1 1



0 0 0 0 0







c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 3 hours ago









Kirk Woll

60.6k16158172




60.6k16158172










asked 4 hours ago









Igor Gribanov

562




562








  • 1




    @ChrisF why would you expect replacing i with j would make a difference? (it doesn't, but curious)
    – Kirk Woll
    3 hours ago






  • 1




    This seems really peculiar to me. (i.e. I'm ready to learn something ;) ) but replacing a1 with the literal 1 in the second one produces entirely different results (it works).
    – Kirk Woll
    3 hours ago








  • 2




    @KirkWoll - I wasn't really expecting anything, but wanted to rule out any possible strangeness with reusing the variable. I've had cases where despite scoping indicating otherwise, variables and their values have persisted outside loops. In this case I wanted to check that the code was actually writing out each element of B and not just ,e.g., B[4]
    – ChrisF
    3 hours ago






  • 2




    Looks like a bug in the compiler. As far as I can see, it triggered if you have 3 or more zero constant expressions, and no non-zero constant expressions.
    – PetSerAl
    3 hours ago






  • 2




    I've attempted a 🦇 signal to @jaredpar. We'll see if he's ready to come to our aid. ;)
    – Kirk Woll
    3 hours ago














  • 1




    @ChrisF why would you expect replacing i with j would make a difference? (it doesn't, but curious)
    – Kirk Woll
    3 hours ago






  • 1




    This seems really peculiar to me. (i.e. I'm ready to learn something ;) ) but replacing a1 with the literal 1 in the second one produces entirely different results (it works).
    – Kirk Woll
    3 hours ago








  • 2




    @KirkWoll - I wasn't really expecting anything, but wanted to rule out any possible strangeness with reusing the variable. I've had cases where despite scoping indicating otherwise, variables and their values have persisted outside loops. In this case I wanted to check that the code was actually writing out each element of B and not just ,e.g., B[4]
    – ChrisF
    3 hours ago






  • 2




    Looks like a bug in the compiler. As far as I can see, it triggered if you have 3 or more zero constant expressions, and no non-zero constant expressions.
    – PetSerAl
    3 hours ago






  • 2




    I've attempted a 🦇 signal to @jaredpar. We'll see if he's ready to come to our aid. ;)
    – Kirk Woll
    3 hours ago








1




1




@ChrisF why would you expect replacing i with j would make a difference? (it doesn't, but curious)
– Kirk Woll
3 hours ago




@ChrisF why would you expect replacing i with j would make a difference? (it doesn't, but curious)
– Kirk Woll
3 hours ago




1




1




This seems really peculiar to me. (i.e. I'm ready to learn something ;) ) but replacing a1 with the literal 1 in the second one produces entirely different results (it works).
– Kirk Woll
3 hours ago






This seems really peculiar to me. (i.e. I'm ready to learn something ;) ) but replacing a1 with the literal 1 in the second one produces entirely different results (it works).
– Kirk Woll
3 hours ago






2




2




@KirkWoll - I wasn't really expecting anything, but wanted to rule out any possible strangeness with reusing the variable. I've had cases where despite scoping indicating otherwise, variables and their values have persisted outside loops. In this case I wanted to check that the code was actually writing out each element of B and not just ,e.g., B[4]
– ChrisF
3 hours ago




@KirkWoll - I wasn't really expecting anything, but wanted to rule out any possible strangeness with reusing the variable. I've had cases where despite scoping indicating otherwise, variables and their values have persisted outside loops. In this case I wanted to check that the code was actually writing out each element of B and not just ,e.g., B[4]
– ChrisF
3 hours ago




2




2




Looks like a bug in the compiler. As far as I can see, it triggered if you have 3 or more zero constant expressions, and no non-zero constant expressions.
– PetSerAl
3 hours ago




Looks like a bug in the compiler. As far as I can see, it triggered if you have 3 or more zero constant expressions, and no non-zero constant expressions.
– PetSerAl
3 hours ago




2




2




I've attempted a 🦇 signal to @jaredpar. We'll see if he's ready to come to our aid. ;)
– Kirk Woll
3 hours ago




I've attempted a 🦇 signal to @jaredpar. We'll see if he's ready to come to our aid. ;)
– Kirk Woll
3 hours ago












2 Answers
2






active

oldest

votes


















5














Thanks for writing up a nice repro here! This appears to be a duplicate of issue 29092. The repro is a bit different but at a quick glance it's hitting the same problem and should also be fixed. The fix for this will be included in Dev16.






share|improve this answer





















  • Stupid question: if you fixed it in September, why was it not shipped with all versions of Roslyn since?
    – Ian Kemp
    3 hours ago



















0














As it is stated by @JaredPar, It is a bug that is needed to be fixed.



As a workarround, I found two ways to avoid this problem.



one is to use const varible



const double a1 = 1;
double* A = stackalloc double[5] { a1, 0, 0, 0, a1 }; // output 1 0 0 0 1


or



double a1 = 1;
double a0 = 0;
double* A = stackalloc double[5] { a1, a0, a0, a0, a1 }; // output 1 0 0 0 1





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53981915%2fwhy-does-stackalloc-initialization-have-inconsistent-behavior%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    Thanks for writing up a nice repro here! This appears to be a duplicate of issue 29092. The repro is a bit different but at a quick glance it's hitting the same problem and should also be fixed. The fix for this will be included in Dev16.






    share|improve this answer





















    • Stupid question: if you fixed it in September, why was it not shipped with all versions of Roslyn since?
      – Ian Kemp
      3 hours ago
















    5














    Thanks for writing up a nice repro here! This appears to be a duplicate of issue 29092. The repro is a bit different but at a quick glance it's hitting the same problem and should also be fixed. The fix for this will be included in Dev16.






    share|improve this answer





















    • Stupid question: if you fixed it in September, why was it not shipped with all versions of Roslyn since?
      – Ian Kemp
      3 hours ago














    5












    5








    5






    Thanks for writing up a nice repro here! This appears to be a duplicate of issue 29092. The repro is a bit different but at a quick glance it's hitting the same problem and should also be fixed. The fix for this will be included in Dev16.






    share|improve this answer












    Thanks for writing up a nice repro here! This appears to be a duplicate of issue 29092. The repro is a bit different but at a quick glance it's hitting the same problem and should also be fixed. The fix for this will be included in Dev16.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 3 hours ago









    JaredPar

    568k11610571342




    568k11610571342












    • Stupid question: if you fixed it in September, why was it not shipped with all versions of Roslyn since?
      – Ian Kemp
      3 hours ago


















    • Stupid question: if you fixed it in September, why was it not shipped with all versions of Roslyn since?
      – Ian Kemp
      3 hours ago
















    Stupid question: if you fixed it in September, why was it not shipped with all versions of Roslyn since?
    – Ian Kemp
    3 hours ago




    Stupid question: if you fixed it in September, why was it not shipped with all versions of Roslyn since?
    – Ian Kemp
    3 hours ago













    0














    As it is stated by @JaredPar, It is a bug that is needed to be fixed.



    As a workarround, I found two ways to avoid this problem.



    one is to use const varible



    const double a1 = 1;
    double* A = stackalloc double[5] { a1, 0, 0, 0, a1 }; // output 1 0 0 0 1


    or



    double a1 = 1;
    double a0 = 0;
    double* A = stackalloc double[5] { a1, a0, a0, a0, a1 }; // output 1 0 0 0 1





    share|improve this answer


























      0














      As it is stated by @JaredPar, It is a bug that is needed to be fixed.



      As a workarround, I found two ways to avoid this problem.



      one is to use const varible



      const double a1 = 1;
      double* A = stackalloc double[5] { a1, 0, 0, 0, a1 }; // output 1 0 0 0 1


      or



      double a1 = 1;
      double a0 = 0;
      double* A = stackalloc double[5] { a1, a0, a0, a0, a1 }; // output 1 0 0 0 1





      share|improve this answer
























        0












        0








        0






        As it is stated by @JaredPar, It is a bug that is needed to be fixed.



        As a workarround, I found two ways to avoid this problem.



        one is to use const varible



        const double a1 = 1;
        double* A = stackalloc double[5] { a1, 0, 0, 0, a1 }; // output 1 0 0 0 1


        or



        double a1 = 1;
        double a0 = 0;
        double* A = stackalloc double[5] { a1, a0, a0, a0, a1 }; // output 1 0 0 0 1





        share|improve this answer












        As it is stated by @JaredPar, It is a bug that is needed to be fixed.



        As a workarround, I found two ways to avoid this problem.



        one is to use const varible



        const double a1 = 1;
        double* A = stackalloc double[5] { a1, 0, 0, 0, a1 }; // output 1 0 0 0 1


        or



        double a1 = 1;
        double a0 = 0;
        double* A = stackalloc double[5] { a1, a0, a0, a0, a1 }; // output 1 0 0 0 1






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 3 hours ago









        Simonare

        4,87811434




        4,87811434






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53981915%2fwhy-does-stackalloc-initialization-have-inconsistent-behavior%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            How to ignore python UserWarning in pytest?

            Alexandru Averescu