Questions tagged [unsafe]

In C# or Rust, the unsafe keyword marks code able to work directly with memory pointers, bypassing some of the language's safety checks. In Java, `sun.misc.Unsafe` is a special class performing low-level, unsafe operations.

In C# The unsafe keyword denotes an unsafe context, which is required for any operation involving pointers. You can use the unsafe modifier in the declaration of a type or a member. Once so declared, the entire textual extent of the type or member is considered an unsafe context.

For more information, see Unsafe Code and Pointers (C# Programming Guide)

The unsafe Keyword on MSDN
http://msdn.microsoft.com/en-us/library/chfa2zb8(v=vs.100).aspx


In Java, sun.misc.Unsafe is a special class performing low-level, unsafe operations. Though this is a private platform API, this class is used extensively in many projects and libraries including Hadoop, Spark, Guava, Cassandra, etc.


In Rust, the unsafe keyword denotes a function or block that is implemented outside some of the safeguards of the compiler. Thus it is as much an escape hatch as a device to easily document code that might require increased attention. More information can be found in the Rust Book


In Go, package unsafe contains operations that step around the type safety of Go programs. Packages that import unsafe may be non-portable and are not protected by the Go 1 compatibility guidelines.

990 questions
0
votes
2 answers

C# Child Process from Legacy C++ App Windowing Problems

We have a c++ legacy application and have been extending it with c# applets that are invoked using COM from the parent c++ app. They bring up windows that are not modal. Moreover, I think these .NET windows are not proper children of the c++…
Steve L
  • 1,523
  • 3
  • 17
  • 24
0
votes
0 answers

What's the difference between publishing and building an Mvc 4 app with respect to unsafe code?

I have an asp.net mvc 4 app that references a c# project which uses the unsafe keyword and this is allowed for in the project properties. The problem is when I publish the web app and I get the unsafe errors. In another post (Publish web application…
R Swasey
  • 307
  • 3
  • 7
0
votes
2 answers

Pinning a pointer to a managed variable

Currently, I have this code: class SO { public SO() { var ptrManager = new PointerManager(); int i = 1; ptrManager.SavePointer(ref i); Console.WriteLine(i); // prints 1 i = 2; //change value to 2…
dcastro
  • 66,540
  • 21
  • 145
  • 155
0
votes
0 answers

C# Pinning pixel data of custom object

I have two IntPtr's to two different float images (source.IP and destination.IP). I copy a block of pixels from one image to the other. int j = 0; for (int y0 = y; y0 < h + y; y0++) { float* s = ((float*)source.IP) + (y0 * sourcewidth) + x; …
smerlung
  • 1,459
  • 1
  • 13
  • 32
0
votes
1 answer

Unsafe code and fixed statements with StringBuilder

I was wondering about how passing a String or a StringBuilder to a C function which output a string by parameter. I've found a great answer in Calling unmanaged function from C#: should I pass StringBuilder or use unsafe code? But I have a doubt.…
Luca
  • 11,646
  • 11
  • 70
  • 125
0
votes
1 answer

Cast pointer to generic structure

I am traying to cast a pointer to a generic structure (blittable). It all seems fine when I am doing with non-generic structures => then I am able to use Marshal.PtrToStructure(...) but that function does not receive generic structures (Why ?) So I…
dajuric
  • 2,373
  • 2
  • 20
  • 43
0
votes
1 answer

How do we 'whitelist' our website for Facebook?

How do we 'whitelist' our website - when people click on a link to our website (www.ineed.co.uk) it comes up as 'Facebook thinks this site is unsafe ...'
0
votes
1 answer

Why such slow performance when parsing data after it is read in using Windows ReadFile?

I am running performance tests of several ways to perform File IO in C# .Net. The two tasks I perform are to read a text file in its entirety into a buffer, then parse that file. The data is tab delimited records with a header record, but that is…
Paul Chernoch
  • 5,275
  • 3
  • 52
  • 73
0
votes
2 answers

Project9.java uses unchecked or unsafe operations

public class Project9 { public static void main(String args[]) { InventoryItem[] items = new InventoryItem[3]; items[0] = new InventoryItem("Coffee", 1); items[1] = new InventoryItem("Pencils", 2); items[2] =…
0
votes
2 answers

Get Type of object and then cast it?

I'm trying to do a little "memcpy" hack in C#. I keep getting stuck on this part, because it won't convert System.Type to byte* public unsafe void memcpy(byte* dest, object src, int length) { byte* nsrc; byte* ndst; …
user1594121
  • 107
  • 1
  • 8
0
votes
2 answers

To call a method that requires IntPtr, is it better to use /unsafe, or Marshal.AllocHGlobal?

I have a class that will have a few instances persistent throughout the duration of the application. These objects will each need to call a dll method that appends data from an existing float[] buffer, and passes the full dataset to a DLL method…
Dax Fohl
  • 10,654
  • 6
  • 46
  • 90
0
votes
1 answer

Dealing with 16 bit characters using sun.misc.Unsafe

Help me understand something. Using .. import sun.misc.Unsafe; If i need to put a character 0-127 to some memory address, in order to prevent out of range chars, i do this if (0 != (c & 0xFF80)) { throw new RuntimeException("Only Ascii…
James Raitsev
  • 92,517
  • 154
  • 335
  • 470
0
votes
2 answers

Can a WPF Browser Application run unsafe code?

I am writing some software that plots out fractals, and lets the user explore them interactively. I currently have my code in a windowed wpf app. I would like get into a browser hosted wpf app, so that I could display them on my website. The…
Mr Bell
  • 9,228
  • 18
  • 84
  • 134
0
votes
2 answers

Reading from an unmanaged stream - unsafe code, IntPtr

The following is exposed in the Firefox (Gecko) 3.5 code: [Guid("fa9c7f6c-61b3-11d4-9877-00c04fa0cf4a"), ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] interface nsIInputStream { void Close(); int Available(); int…
Nathan Ridley
  • 33,766
  • 35
  • 123
  • 197
0
votes
1 answer

Structure Generics in C#

Please, help me with this problem: I Try define a structure like this: unsafe struct sNodo { public T info; public sNodo* sIzq;} but i get this error: Cannot take the address of, get the size of, or declare a pointer to…
Carlos B
  • 1
  • 1
  • 1