Why does stackalloc initialization have inconsistent behavior?
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#
|
show 7 more comments
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#
1
@ChrisF why would you expect replacingi
withj
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 replacinga1
with the literal1
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 ofB
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
|
show 7 more comments
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#
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#
c#
edited 3 hours ago
Kirk Woll
60.6k16158172
60.6k16158172
asked 4 hours ago
Igor Gribanov
562
562
1
@ChrisF why would you expect replacingi
withj
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 replacinga1
with the literal1
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 ofB
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
|
show 7 more comments
1
@ChrisF why would you expect replacingi
withj
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 replacinga1
with the literal1
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 ofB
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
|
show 7 more comments
2 Answers
2
active
oldest
votes
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.
Stupid question: if you fixed it in September, why was it not shipped with all versions of Roslyn since?
– Ian Kemp
3 hours ago
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Stupid question: if you fixed it in September, why was it not shipped with all versions of Roslyn since?
– Ian Kemp
3 hours ago
add a comment |
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.
Stupid question: if you fixed it in September, why was it not shipped with all versions of Roslyn since?
– Ian Kemp
3 hours ago
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered 3 hours ago
Simonare
4,87811434
4,87811434
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
@ChrisF why would you expect replacing
i
withj
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 literal1
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