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
1 answer

unchecked or unsafe operations even after specifying type

I get the following warning Note: com.......\BeerSelect.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. I have specified the type as well.. I would like to know the reason rather than using the…
0
votes
1 answer

Program uses unchecked or unsafe operations for 2d ArrayList[][]

I have a task at the university to make a play board made of play fields. Every field can contain numerous items on it. I made it with an array arrayList like that: List[][] items = new ArrayList[x][y]; In Eclipse everything's OK, but when…
0
votes
2 answers

Serialization/Deserialization of Pointers / Memory References

I am writing a XML serializer which uses reflection to recursively crawl through an object's public or private fields to store as XML and later reconstruct, and while testing my serializer on the infamously unserialzeable DataTable, it somehow…
Richard
  • 991
  • 2
  • 11
  • 24
0
votes
1 answer

variable lost after unsafe code block

I have a very strange problem. Here is my code: JV.Math_Mul(E); //// public new void Math_Mul(Matrix a) { double[,] vC = new double[a.ColCount, this.RowCount]; …
Alex Voskresenskiy
  • 2,143
  • 2
  • 20
  • 29
0
votes
2 answers

How do I **write** to ArraySegment.Array? Should I use unsafe code?

There are many questions that ask how to get a subset of an array on this site, however no one has successfully gotten an answer on how to write to the array as if it were byte[]. byte[] bigTargetBytes = new byte[10000]; byte[] twentyBytes =…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
0
votes
1 answer

Pointer to string of type int

I'm trying to get a pointer to a string, where the pointer is of type int. I'm not sure if I'm doing it correctly. I just found out yesterday about unsafe and fixed, so I'm still questioning this a bit. Can I just cast the char* to int? I'm not…
DanM7
  • 2,203
  • 3
  • 28
  • 46
0
votes
2 answers

Not clear unsafe code in draw pixel method

Lately I got low level graphics programming job to do (implementing raster graphics algorithms). That's why I started to look for tools (classes) which would be helpful to draw primitives (lines, rectangles, ellipses etc.) on the low level of…
patryk.beza
  • 4,876
  • 5
  • 37
  • 56
0
votes
1 answer

Initialise an unsafe fixed double array in struct

This question is a follow up from Marshalling C# structure to C++ Using StructureToPtr. I have the following struct: [StructLayout(LayoutKind.Explicit, Size = 120, CharSet = CharSet.Unicode)] public unsafe struct DynamicState { …
Seth
  • 8,213
  • 14
  • 71
  • 103
0
votes
1 answer

EntryPointNotFoundException TaskDialog at start of Application but runs fine later

I have the TaskDialog source directly from the WindowsAPI Pack for .NET (The wrapper) but whenever I try and open a TaskDialog straight in the static void Main() area of my program it throws an EntryPointNotFoundException. However the TaskDialog…
jduncanator
  • 2,154
  • 1
  • 22
  • 39
0
votes
1 answer

SafeHandleZeroOrMinusOneIsInvalid in Portable Class Library

The SafeHandleZeroOrMinusOneIsInvalid or similar classes cannot be used in a Portable Class Library targeting .NET 4.0 and Windows Store Apps. Does anybody know why or how one could use this or use a different class? Yes, I would like some unsafe…
nietras
  • 3,949
  • 1
  • 34
  • 38
0
votes
1 answer

Why is my code causing "unchecked or unsafe operations" warning being flagged by compiler?

My java compiler is complaining that my code uses "unchecked or unsafe operations". Any idea which lines are causing that in the two code snippets? @SuppressWarnings("unchecked") private final void…
ikevin8me
  • 4,253
  • 5
  • 44
  • 84
0
votes
4 answers

Can this function be any safer ? Looking for tips and your thoughts !

this is somewhat of an odd question. I wrote a C function. Its 'like' strchr / strrchr. It's supposed to look for a character in a c-string, but going backwards, and return a pointer to it. As c strings are not "null initiated", it also takes a…
Tom
  • 43,810
  • 29
  • 138
  • 169
0
votes
2 answers

Requirement which is apt for using pointer, unsafe code in .net? Image processing is resource hungry enough to go for high end optimization?

We had recently few threads (below) on SO where one of the common suggestion was do not use pointer. fixed block in .net In .NET is there any difference between using pointers as function parameters or using the "ref" keyword? is strings in .net…
Pritesh
  • 1,938
  • 7
  • 32
  • 46
-1
votes
1 answer

why this pointer gets garbage collected before being freed in rust

fn test() -> *const Vec { let b = vec![9_u8]; let ret: *const Vec = &b; println!("ret ptr={:#p} inside {:#p}", ret, b.as_ptr()); std::mem::forget(b); ret } fn main() { let a = test(); let v = unsafe { …
-1
votes
1 answer

How to use reflect.NewAt on interface{}?

package main import ( "encoding/json" "fmt" "reflect" "unsafe" ) type Stu struct { Name string `json:"name"` } func MakeStu() interface{} { return Stu{ Name: "Test", } } func main() { jsonData :=…
kuokongqingyun
  • 965
  • 6
  • 8