By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,960 Members | 2,232 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,960 IT Pros & Developers. It's quick & easy.

unsafe bug ?

P: n/a
Hi,

Using VS2005 I'm trying out some unsave code but have some strange results.
My english is not well so I give example:

private void testToolStripMenuItem2_Click(object sender, EventArgs e)
{
char[] ttt = "5".ToCharArray();
test(ttt);
}

unsafe void test(char[] _a)
{
fixed (char* a = _a) {
double t = *a - 0x30;
t /= 10;
double tt = (*a - 0x30) / 10;
}
}

In the procedure test, t is 0.5 in second line witch is correct. But tt is
0. Wy ? It is exacly same code unless I'm missing somting...

--
rgds, Wilfried
http://www.mestdagh.biz
Sep 21 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Wilfried Mestdagh <Wi**************@discussions.microsoft.comwrote :
Using VS2005 I'm trying out some unsave code but have some strange results.
My english is not well so I give example:

private void testToolStripMenuItem2_Click(object sender, EventArgs e)
{
char[] ttt = "5".ToCharArray();
test(ttt);
}

unsafe void test(char[] _a)
{
fixed (char* a = _a) {
double t = *a - 0x30;
t /= 10;
double tt = (*a - 0x30) / 10;
}
}

In the procedure test, t is 0.5 in second line witch is correct. But tt is
0. Wy ? It is exacly same code unless I'm missing somting...
No, it's not the same code. The expression t /= 10 is performing a
division of a double by an integer - so the integer is promoted to a
double and the calculation is performed in floating point.

Compare this with (*a-0x30) / 10, which is performing a division of a
char by an integer, so the char is promoted to an int and the
calculation is performed in integer arithmetic.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 21 '06 #2

P: n/a
Hi,

thank you!

--
rgds, Wilfried
http://www.mestdagh.biz
Sep 22 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.