473,394 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

unsafe bug ?

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
2 1109
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
Hi,

thank you!

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Elidel | last post by:
Is the use of a COM object in a c# program considered 'unsafe' code ? Does the method that calls the COM object need the 'unsafe' keyword ? Is code that calls a COM object considered unmanaged?...
3
by: Andre | last post by:
if I have an unsafe method that takes in two arrays, performs some operations and returns; I know that the garbage collector will not collect objects allocated inside the unsafe method. However, if...
8
by: | last post by:
Wel, I am rebuilding the VC# 2002 project that I have deployment problems with the 2003 version, hoping this solves the problems, but now I encounter this wierd bug??? If I have the project, and...
4
by: Jon Milner | last post by:
How do I declare that my code is unsafe? Sorry the help files at my University have not been installed!
17
by: Bradley1234 | last post by:
Sorry if this is obvious, but Ill ask... Is there a new way of using pointer operations in C# ? Ive got a Deitel book on C# that neither mentions the word "pointer" nor "unsafe" in the index....
4
by: CodeTyro | last post by:
My native language being C++, I've got a few questions that a couple of hours of searching on msdn didn't answer. First, when using unsafe code and pointers, what is the C# equivalent to the C++...
1
by: Z | last post by:
Hello, In my C# program I have a function that uses pointers. Is there a way to tell the compiler to compile only this function with the unsafe switch and compile the remaining project as safe...
2
by: NickP | last post by:
Hi there, I am currently moving my API declarations into relevant classes for Safe and Unsafe methods. My understanding is that unsafe methods are ones that require elevated security...
0
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I have two piece of unsafe code. In the first one I'm getting the byte* pointer of a Bitmap data, and the second one in inside a loop that uses that byte* pointer to set the data in the Bitmap. ...
2
by: wmhnq | last post by:
private void button1_Click(object sender, EventArgs e) { unsafe { string str = "abcde"; fixed (char* pStr = str) { A a = new A(pStr); a.change(); MessageBox.Show(str.ToString());
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.