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

NullReferenceException when setting a out parameter in c#

I got a unsafe assembly using easyhook in c#.. this code bellows works well but sum time, I got a NullReferenceException when this method is called.. The instance of this class is a Singleton pattern It passe several time and one point it got a null…
-1
votes
1 answer

Handling streaming iterator as normal iterator by using PhantomData and unsafe

I know the code below is hacky, but could it be called safe and idiomatic Rust? Is there better way for this? // needs to do 'rustup default nightly' to run under valgrind // #![feature(alloc_system, global_allocator, allocator_api)] // extern crate…
chamaken
  • 81
  • 5
-1
votes
1 answer

Error “Unsafe code may only appear if compiling with /unsafe” in Xamarin VSO despite Unsafe code enabled?

Using Visual Studio 2015 with the Xamarin extension, It seems to exhibit a strange behavior, when it comes to using unsafe code. I have enabled the Unsafe code option in the project properties. Yet, I still get the IDE error, probably coming from…
Sold Out
  • 1,321
  • 14
  • 34
-1
votes
1 answer

(C#) Convert int type to address type

I can compile a code like this unsafe static void Main() { int i = 5; int* j = &i; } But how can I convert address type to int type and vice versa? like: unsafe static void Main() { int i = 5; int _j = (int)&i; int* j =…
DaveG
  • 491
  • 1
  • 6
  • 19
-1
votes
1 answer

.NET C# unsafe/fixed CS1666 working with array of pointers to elements in another array

Warning! Sorry, I'm bad english! Using a fixed array in struct: [StructLayout(LayoutKind.Explicit, Size = 12)] public unsafe struct union_reg { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)] [FieldOffset(0)] public fixed byte…
-1
votes
2 answers

Avoid using unsafe code to pass reference from C# to C++/CLI

I have a DLL in C++/CLI which is of the form : bool func (String^, unsigned long, LPDWORD) I wish to call this function from within C# code, where LPDOWRD is exposed as uint*. While I can easily put the final parameter within an unsafe context, I…
user1173240
  • 1,455
  • 2
  • 23
  • 50
-1
votes
3 answers

Avoid array initialization in Java

Is it possible to avoid array zeroing/initialization in Java? I need to quickly allocate fresh byte arrays of fixed length that will completely be filled with predefined data. Therefore, I don't need the JVM to automatically zero the array on…
maxim1
  • 53
  • 5
-1
votes
1 answer

Is there a good way to destroy caller's stack in C++?

There are some tricks practicing C++ programmers do know like "Scope Guard" and maybe others involving references to temporaries. I'm not a practicing C++ programmer, but I'd like to ask (of curiosity) if there is a way third party library could…
rostamn739
  • 323
  • 3
  • 8
-1
votes
1 answer

new ArrayList() as an argument to a map.put() is an unsafe operation?

The following code: for (String day:daysOfWeek) {classesData.put(day, new ArrayList());} gives me the following error: Note: ./com/myname/MyClass/MyClass1.java uses unchecked or unsafe operations. …
Gatonito
  • 1,662
  • 5
  • 26
  • 55
-1
votes
1 answer

What can i do to resolve a java warning during compilation?

I have a class DAG which extends another class ARCO. When I compile it, the prompt reports this warning in two lines: Note: .\ARCO.java uses unchecked or unsafe operations Recompile with -Xlint: unchecked for details What is the problem? (class…
James The Beard
  • 205
  • 4
  • 10
-1
votes
1 answer

Panic using unsafe.Pointer in Go

The code is: package main import ( "fmt" "unsafe" ) type Point struct { x int y int } func main() { buf := make([]byte, 50) fmt.Println(buf) t := (*Point)(unsafe.Pointer(&buf)) t.x = 10 t.y = 100 …
-1
votes
1 answer

Pointer to System.Windows.Forms.Cursor.Position.X

I'm trying to cancel the while(true) in the code: while(true) { int t = Cursor.Potion.X; } I tried to use this: unsafe { int* p = &Cursor.Potion.X; } But it raises an error: Cannot take the address of the given expression I also tried to…
-1
votes
1 answer

Size of a Java Object that contains only primitive types

I have a class that is consisted of the following primitive types only: public class Test{ private int a1; //size of int =4 private long a2; //size of long = 8 private double a3[]; //size of double =8 } Moreover, I have an object for this class…
Mike B
  • 1,522
  • 1
  • 14
  • 24
-2
votes
1 answer

Why Rust's raw pointer can access function and some parts of fields' information of a struct variable out of scope?

I want to use Rust raw pointer like *const to reproduce the security problem of use after free, but when I use the raw pointer to access fields or call the function of a freed struct variable, there's no panic occurred. Here's my codes: new method…
gfzum
  • 3
  • 1
-2
votes
1 answer

What's the reason behind this design of rust?

Why does the below code not compile ? I know that you can't leave any variable partially initialised but i do initialise in the next line, So is it that it's difficult for compiler to infer that ( though i don't really think that ) or is it for some…
Darshan V
  • 198
  • 9
1 2 3
65
66