【发布时间】:2016-01-27 08:11:44
【问题描述】:
我正在尝试有关 C# 控制台项目上的堆和指针的一些东西(最初来自 here)。我的程序是这样的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
public class Win32
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr malloc(int size);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int free(IntPtr region); //Change IntPtr befroe free method to int ---update---
}
public class Program
{
public unsafe void Heap()
{
int* num1, num2, answer;
num1 = Win32.malloc(sizeof(int));
*num1 = 999; // 999 should be the value stored at where pointer num1 refers to
num2 = Win32.malloc(sizeof(int));
*num2 = 1; // 1 should be the value stored at where pointer num2 refers to
answer = Win32.malloc(sizeof(int));
*answer = *num1 + *num2; // 1000 should be the value of pointer answer's reference
Console.WriteLine(*answer); // 1000?
Win32.free(num1);
Win32.free(num2);
Win32.free(answer);
}
}
调试后,得到错误信息:
错误 1 无法将类型“System.IntPtr”隐式转换为“int*”。一个 存在显式转换(您是否缺少演员表?)
error CS1502: The best overloaded method match for 'Win32.free(System.IntPtr)' has some invalid arguments
error CS1503: Argument 1: cannot convert from 'int*' to 'System.IntPtr'
我的问题是为什么我不能在 malloc 和 free 之前使用 IntPtr,因为这两种方法都返回 void?我应该对我的代码进行哪些更改? 谢谢你的帮助。
---更新---
更改:public static extern IntPtr free(int hWnd); 为公共静态 extern int free(IntPtr region); ,free(*num) 为 free(num)
给出额外的“CS1502”和“CS1503”两个错误。
---第二次更新--- C# 自动处理堆的东西。 C# 中没有 malloc 的等价物。这是一个死胡同。 T_T
【问题讨论】:
-
are you missing a cast? -
@Rob 问题是我不认为我错过了一个演员表。而且我不知道如何将 void 转换为 int。