unsafe

Why doesn't *(int*)0=0 cause an access violation?

和自甴很熟 提交于 2019-12-20 16:29:27
问题 For educational purposes, I'm writing a set of methods that cause runtime exceptions in C# to understand what all the exceptions are and what causes them. Right now, I'm tinkering with programs that cause an AccessViolationException . The most obvious way (to me) to do this was to write to a protected memory location, like this: System.Runtime.InteropServices.Marshal.WriteInt32(IntPtr.Zero, 0); Just as I had hoped, this threw an AccessViolationException . I wanted to do it more concisely, so

How to use “unsafe” in JNI?

前提是你 提交于 2019-12-20 05:52:09
问题 Let's say I have a value pointed to by a (base,offset) tuple. e.g. class Data{ int x = 0; } class Accessor{ public Data data; public Object base$x; public long off$x; public static final Unsafe unsafe; public void run(){ data = new Data(); base$x = data; off$x = 12; unsafe.putInt(base$x,off$x,1); assert(data.x == 1); } static{ try { Constructor<Unsafe> unsafeConstructor = Unsafe.class.getDeclaredConstructor(); unsafeConstructor.setAccessible(true); unsafe = unsafeConstructor.newInstance(); }

fast way to create a big bitmap from an array of bitmap?

百般思念 提交于 2019-12-20 03:15:35
问题 I have this code, copy/paste in a new winform app and this will write a file on your desktop if you run it: test123abcd.png Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Dim SquareSize = 5 Dim GridX = 2500 Dim GridY = 2500 Dim SquareCount = GridX * GridY - 1 Dim sw As New Stopwatch Dim Rect(4) As Rectangle Rect(0) = New Rectangle(0, 3, 3, 1) Rect(1) = New Rectangle(3, 0, 1, 3) Rect(2) = New Rectangle(3, 3, 3, 1) Rect(3) = New Rectangle(0, 0, 1, 3)

Are there utility methods for performing unsafe arithmetic in VB.NET?

元气小坏坏 提交于 2019-12-19 10:11:19
问题 I'm overriding getting a hash code. There's a point where Hash = 1210600964 * 31 + -837896230 which causes an OverflowException . Is there a method that I can use, something like: Hash = addUnsafe(multiplyUnsafe(1210600964, 31), -837896230) Without needing to link my project to another assembly? I do not want to remove overflow checks. dllimports are OK. 回答1: You can do the maths with 64-bit numbers instead and then use And to cut off any excess: Option Infer On ' ..... Shared Function

Are there utility methods for performing unsafe arithmetic in VB.NET?

回眸只為那壹抹淺笑 提交于 2019-12-19 10:10:54
问题 I'm overriding getting a hash code. There's a point where Hash = 1210600964 * 31 + -837896230 which causes an OverflowException . Is there a method that I can use, something like: Hash = addUnsafe(multiplyUnsafe(1210600964, 31), -837896230) Without needing to link my project to another assembly? I do not want to remove overflow checks. dllimports are OK. 回答1: You can do the maths with 64-bit numbers instead and then use And to cut off any excess: Option Infer On ' ..... Shared Function

Publish web application with unsafe code

我怕爱的太早我们不能终老 提交于 2019-12-19 03:24:16
问题 I'm trying to publish a web application (with VS2012 Web) in which I need to run a vb script. That script currently doesn't run correctly probably because of the lack of permissions. I am currently trying to run it as a user and supply some credentials. The password I have to provide must be in a System.Security.SecureString which needs a char* to be created. When I run my app in debug everything works fine and as expected. But when comes the time to publish the app to the server, it says : 1

Safe vs Unsafe code

我只是一个虾纸丫 提交于 2019-12-18 21:58:43
问题 Read this question today about safe and unsafe code I then read about it in MSDN but I still don't understand it. Why would you want to use pointers in C#? Is this purely for speed? 回答1: There are three reasons to use unsafe code: APIs (as noted by John) Getting actual memory address of data (e.g. access memory-mapped hardware) Most efficient way to access and modify data (time-critical performance requirements) 回答2: Sometimes you'll need pointers to interface your C# to the underlying

Error lnk2026: module unsafe for safeseh image

女生的网名这么多〃 提交于 2019-12-18 11:41:03
问题 I got this error when building a sample visual C++ project. First I downloaded 3 sample projects, all solve the same problem, print out all the prime numbers less than N (you may know these sample projects ?). I built the pure-C project without any problem. But when I tried to build the assembly-based project one, I got this error. Thank you. 回答1: Try to disable SAFESEH. From spec: /SAFESEH was specified, but a module was not compatible with the safe exception handling feature. 回答2: In Visual

Fixed Statement in C#

£可爱£侵袭症+ 提交于 2019-12-18 07:45:10
问题 We have similar code to the following in one of our projects. Can anyone explain (in simple English) why the fixed statement is needed here? class TestClass { int iMyVariable; static void Main() { TestClass oTestClass = new TestClass(); unsafe { fixed (int* p = &oTestClasst.iMyVariable) { *p = 9; } } } } 回答1: It fixes the pointer in memory. Garbage collected languages have the freedom to move objects around memory for efficiency. This is all transparent to the programmer because they don't

LayoutKind.Sequential not followed when substruct has LayoutKind.Explicit

*爱你&永不变心* 提交于 2019-12-18 04:41:21
问题 When running this code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace StructLayoutTest { class Program { unsafe static void Main() { Console.WriteLine(IntPtr.Size); Console.WriteLine(); Sequential s = new Sequential(); s.A = 2; s.B = 3; s.Bool = true; s.Long = 6; s.C.Int32a = 4; s.C.Int32b = 5; int* ptr = (int*)&s; Console.WriteLine(ptr[0]); Console.WriteLine(ptr[1]); Console.WriteLine(ptr[2]); Console