unsafe

GoLang 强制类型转换:unsafe.Pointer

天涯浪子 提交于 2020-04-20 07:08:59
注意此种转换只适合简单类型,对于有对象描述的类型是完全不适用的,鸡肋啊 更详细的文章请参见@陈一回 http://my.oschina.net/goal/blog/193698 ps:补充另外一种用法,这次就不鸡肋了 Go语言是个强类型语言。也就是说Go对类型要求严格,不同类型不能进行赋值操作。 指针也是具有明确类型的对象,进行严格类型检查。下面的代码会产生编译错误 package main import ( "fmt" ) func main() { u := uint32(32) i := int32(1) fmt.Println(&u, &i) // 打印出地址 p := &i // p 的类型是 *int32 p = &u // &u的类型是 *uint32,于 p 的类型不同,不能赋值 p = (*int32)(&u) // 这种类型转换语法也是无效的 fmt.Println(p) } unsafe 包提供的Pointer方法可以完成这个任务 package main import ( "fmt" "unsafe" ) func main() { u := uint32(32) i := int32(1) fmt.Println(&u, &i) p := &i p = (*int32)(&u) p = (*int32)(unsafe.Pointer(&u)) fmt

golang: 利用unsafe操作未导出变量

心不动则不痛 提交于 2020-03-17 01:29:17
某厂面试归来,发现自己落伍了!>>> 看了 @喻恒春 大神的利用unsafe.Pointer来突破私有成员,觉得例子举得不太好。而且不应该简单的放个demo,至少要讲一下其中的原理,让看的童鞋明白所以然。see: http://my.oschina.net/achun/blog/122540 unsafe.Pointer其实就是类似C的void *,在golang中是用于各种指针相互转换的桥梁。uintptr是golang的内置类型,是能存储指针的整型,uintptr的底层类型是int,它和unsafe.Pointer可相互转换。uintptr和unsafe.Pointer的区别就是:unsafe.Pointer只是单纯的通用指针类型,用于转换不同类型指针,它不可以参与指针运算;而uintptr是用于指针运算的,GC 不把 uintptr 当指针,也就是说 uintptr 无法持有对象,uintptr类型的目标会被回收。golang的unsafe包很强大,基本上很少会去用它。它可以像C一样去操作内存,但由于golang不支持直接进行指针运算,所以用起来稍显麻烦。 切入正题。利用unsafe包,可操作私有变量(在golang中称为“未导出变量”,变量名以小写字母开始),下面是具体例子。 在$GOPATH/src下建立poit包,并在poit下建立子包p,目录结构如下: $GOPATH

Return unsafe pointer to type parameter

放肆的年华 提交于 2020-01-29 03:45:07
问题 I am trying to define a property that returns a pointer to a generic type argument like so: public class MemWrapper<T> where T: struct { readonly IntPtr pointerToUnmanagedHeapMem; // ... do some memory management also ... public unsafe T* Ptr { get {return (T*)(pointerToUnmanagedHeapMem);} } } The compiler complains that it is not possible to declare a pointer to the managed type T or get its address or size (CS0208). The curious thing is, if I manually replace the generic type parameter by a

Trying to Understand this Image function

我只是一个虾纸丫 提交于 2020-01-17 07:05:25
问题 private void ReadImage() { int i, j; GreyImage = new int[Width, Height]; //[Row,Column] Bitmap image = Obj; BitmapData bitmapData1 = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); unsafe { byte* imagePointer1 = (byte*)bitmapData1.Scan0; for (i = 0; i < bitmapData1.Height; i++) { for (j = 0; j < bitmapData1.Width; j++) { GreyImage[j, i] = (int)((imagePointer1[0] + imagePointer1[1] + imagePointer1[2]) / 3.0); //4 bytes per

Trying to Understand this Image function

孤者浪人 提交于 2020-01-17 07:04:26
问题 private void ReadImage() { int i, j; GreyImage = new int[Width, Height]; //[Row,Column] Bitmap image = Obj; BitmapData bitmapData1 = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); unsafe { byte* imagePointer1 = (byte*)bitmapData1.Scan0; for (i = 0; i < bitmapData1.Height; i++) { for (j = 0; j < bitmapData1.Width; j++) { GreyImage[j, i] = (int)((imagePointer1[0] + imagePointer1[1] + imagePointer1[2]) / 3.0); //4 bytes per

is strings in .net get changed?? is there some bug?

半世苍凉 提交于 2020-01-16 04:34:10
问题 I have written a function Reverse to reverse a string in .net using pointers in unsafe context. I do like this. I allocate “greet” and “x” same value. I reverse greet to my surprise x also gets reversed. using System; class Test{ private unsafe static void Reverse(string text){ fixed(char* pStr = text){ char* pBegin = pStr; char* pEnd = pStr + text.Length - 1; while(pBegin < pEnd){ char t = *pBegin; *pBegin++ = *pEnd; *pEnd-- = t; } } } public static void Main(){ string greet = "Hello World";

How do I convert a struct to a byte array without a copy?

半腔热情 提交于 2020-01-15 05:45:08
问题 [StructLayout(LayoutKind.Explicit)] public struct struct1 { [FieldOffset(0)] public byte a; // 1 byte [FieldOffset(1)] public int b; // 4 bytes [FieldOffset(5)] public short c; // 2 bytes [FieldOffset(7)] public byte buffer; [FieldOffset(18)] public byte[] shaHashResult; // 20 bytes } void DoStuff() { struct1 myTest = new struct1(); myTest.shaHashResult = sha256.ComputeHash(pkBytes); // 20 bytes byte[] newParameter = myTest.ToArray() //<-- How do I convert a struct // to array without a copy?

Java: How to store and retrieve memory address like in C++

泪湿孤枕 提交于 2020-01-14 10:23:20
问题 I come from a C++ background. In C++ I can store a memory adress which I just new'd in a global array and re-use it later. For example, say I have two classes X, Y and I create two objects x, y. The global array StoreAddresses[2] is defined as: uint32_t StoreAddresses[2]; I write: X * x = new X(); Y * y = new Y(); StoreAdresses[0] = (uint32t *) x; //for example, 0x12345678 StoreAdresses[1] = (uint32t *) y; //for example, 0x12345698 Anywhere in my program, I can retrieve the data written in

LockBits Performance Critical Code

醉酒当歌 提交于 2020-01-13 05:23:10
问题 I have a method which needs to be as fast as it possibly can, it uses unsafe memory pointers and its my first foray into this type of coding so I know it can probably be faster. /// <summary> /// Copies bitmapdata from one bitmap to another at a specified point on the output bitmapdata /// </summary> /// <param name="sourcebtmpdata">The sourcebitmap must be smaller that the destbitmap</param> /// <param name="destbtmpdata"></param> /// <param name="point">The point on the destination bitmap

error C4996: 'ctime': This function or variable may be unsafe

試著忘記壹切 提交于 2020-01-09 10:12:28
问题 I have a large project about static source code analysis, and everything compiles successfully, except for one thing. I have provided the error message in the title. The point that confuses me is that it gives an error message saying unsafe. I thought it should be just warning, not an error. By the way, I'm using Visual Studio 2012. Here is the part of the code where I get the error, in ctime. If someone can help me overcome this error, I would be glad. void CppCheckExecutor::reportProgress