C#中如何使用指针和分配内存? C#虽然支持指针,但是并不鼓励使用指针。使用指针需要加上unsafe的关键字。最好和Java一样,更不就不要用指针。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 using System;namespace cshrp{ struct Employee { public int Id, Age, Salary; } class Test { public static unsafe void Main() { Employee Jane, Carl; Employee* pJane, pCarl; Jane = new Employee(); pJane = &Jane; pJane->Id = 1; pJane->Age = 29; pJane->Salary = 95000; Console.WriteLine("Jane's salary of {0} is stored at {1:x}", (int)pJane->Salary, (int)&Jane.Salary); Carl = Jane; pCarl = &Carl; pCarl->Id++; pCarl->Age = 32; pCarl->Salary = 10200; Console.WriteLine("Carl's salary of {0} is stored at {1:x}", (int)pCarl->Salary, (int)&Carl.Salary); } }} C#用delegate代替指针来使用,也支持指针,但不推荐使用。 Mada的方法有点问题,如果只是pJane = &Jane并不保险,还需要使用fixed语句将Jane的地址固定,否则GC有可能移动Jane的位置从而使指针失效 fixed(Employee* pJane = &Jane)。delegate只能取代函数指针,C#仍然需要有效的数组指针,尤其是为了与unmanaged code交互。由于指针可以指向一个managed树组,我不知道在managed world中是否有必要使用指针分配内存。但在Pinvoke交互中,可以使用Marshal类的成员函数在GlobalHeap种分配内存(结果是IntPtr,可以转换成pointer) 我想在C#中完成这样一个功能,首先分配一段内存,然后在这段内存中存入一个字符串,返回一个指针,再通过这个指针,得到内存中保留的字符串。这个功能在C和C#很容易实现,但不知道在C#中如何实现,希望各位朋友不吝赐教,谢谢! 没有必要,因为reference在多数情况下已经够用了。你的函数问什么一定要返回指针呢?返回string的引用是一样的,在C#里这样就简单多了。string str = "Whatever string value you like";return str;也可以用stringbuilder来实现。...如果你需要一个指针,那随时可以用fixed语句和&运算符取一个string对象的地址。 using System;public class Stack{ private Node first=null; public bool Empty { get { return (first==null); } } public object Pop() { if(first==null) throw new Exception("Cann't Pop from an empty Stack!"); else { object temp=first.Value; first=first.Next; return temp; } } public void Push(object o) { first=new Node(o,first); } class Node { public Node Next; public object Value; public Node(object value):this(value,null){} public Node(object value,Node next) { Next=next; Value=value; } }}class Test{//使用指针 unsafe static void ww(byte[] arr) { fixed(byte *p_arr=arr) { byte *p_elem=p_arr; for(int i=0;i<arr.Length;i++) { byte value=*p_elem;// string addr=string.Format((string)p_elem,"x");//输出16进制地址 Console.WriteLine("arr[{0}]at 0x{1} is{2}",i,(int)p_elem,value); p_elem++; } } } unsafe static void Main() { Stack s=new Stack(); string kk=new string('k',100);// for(int i=0;i<10;i++)// kk="skdfjsdjfkskdfjsdk"; s.Push(kk); // while(!s.Empty)// Console.WriteLine(s.Pop()); //指针的使用/ //byte[] arr=new byte[]{56,2,3,4,5}; //ww(arr); //byte *mm=null; // mm=mm+13119028; // byte value=*mm;// Console.WriteLine("从指针地址取值{0}",value); //字符指针用法 //char[] ch=new char[]{'k','m'}; //fixed(char *chpoint=ch) //{ // char newch=*chpoint;// Console.WriteLine(newch); // } //字符串指针用法 // string a="ksj;sdlkjsdjkkdsjkdjsk";//=new string(; // fixed(char *l=a) // { // string n=new string(l);// Console.WriteLine(n);// Console.WriteLine("zifuchuan point is {0}",(int)l); // } fixed(char *lw=kk) {; lw[0]='k'; lw[1]='j'; string bb=new string(lw); Console.WriteLine("{0}",bb);} }} devexpress richeditcontrol的字体设置 计算小问题 Request.QueryString中文乱码求解 硬盘重新分区,但是没有格式化,数据能够恢复吗? datetimepicker的问题, 分数不多, 希望大侠帮忙! 串口控件下载!!! 如何在运行时的界面上拖动一个控件? 问一个简单的问题?怎么把一个字符转成Unicode码? ASP.NET如何计算当前WEB程序占用了多少内存?? 如何从int型转到sting? 请问c#下有把一个文件写成byte[]的方法么?java下很好实现的说....... 请问C#的长处是编写普通应用程序,还是Web Services?
{
struct Employee
{
public int Id, Age, Salary;
}
class Test
{
public static unsafe void Main()
{
Employee Jane, Carl;
Employee* pJane, pCarl; Jane = new Employee();
pJane = &Jane; pJane->Id = 1;
pJane->Age = 29;
pJane->Salary = 95000; Console.WriteLine("Jane's salary of {0} is stored at {1:x}",
(int)pJane->Salary,
(int)&Jane.Salary);
Carl = Jane;
pCarl = &Carl; pCarl->Id++;
pCarl->Age = 32;
pCarl->Salary = 10200; Console.WriteLine("Carl's salary of {0} is stored at {1:x}",
(int)pCarl->Salary,
(int)&Carl.Salary);
}
}
}
你的函数问什么一定要返回指针呢?返回string的引用是一样的,在C#里这样就简单多了。
string str = "Whatever string value you like";
return str;
也可以用stringbuilder来实现。
...
如果你需要一个指针,那随时可以用fixed语句和&运算符取一个string对象的地址。
public class Stack
{
private Node first=null;
public bool Empty
{
get
{
return (first==null);
}
}
public object Pop()
{
if(first==null)
throw new Exception("Cann't Pop from an empty Stack!");
else
{
object temp=first.Value;
first=first.Next;
return temp;
}
}
public void Push(object o)
{
first=new Node(o,first);
}
class Node
{
public Node Next;
public object Value;
public Node(object value):this(value,null){}
public Node(object value,Node next)
{
Next=next;
Value=value;
}
}
}
class Test
{
//使用指针
unsafe static void ww(byte[] arr)
{
fixed(byte *p_arr=arr)
{
byte *p_elem=p_arr;
for(int i=0;i<arr.Length;i++)
{
byte value=*p_elem;
// string addr=string.Format((string)p_elem,"x");//输出16进制地址
Console.WriteLine("arr[{0}]at 0x{1} is{2}",i,(int)p_elem,value);
p_elem++;
}
}
}
unsafe static void Main()
{
Stack s=new Stack();
string kk=new string('k',100);
// for(int i=0;i<10;i++)
// kk="skdfjsdjfkskdfjsdk";
s.Push(kk);
// while(!s.Empty)
// Console.WriteLine(s.Pop()); //指针的使用/
//byte[] arr=new byte[]{56,2,3,4,5};
//ww(arr);
//byte *mm=null;
// mm=mm+13119028;
// byte value=*mm;
// Console.WriteLine("从指针地址取值{0}",value); //字符指针用法
//char[] ch=new char[]{'k','m'};
//fixed(char *chpoint=ch)
//{
// char newch=*chpoint;
// Console.WriteLine(newch);
// }
//字符串指针用法
// string a="ksj;sdlkjsdjkkdsjkdjsk";//=new string(;
// fixed(char *l=a)
// {
// string n=new string(l);
// Console.WriteLine(n);
// Console.WriteLine("zifuchuan point is {0}",(int)l);
// } fixed(char *lw=kk)
{;
lw[0]='k';
lw[1]='j';
string bb=new string(lw);
Console.WriteLine("{0}",bb);}
}
}