using System;
public class MainEntryPoint {
public static unsafe void Main() {
Console.WriteLine("Size of Currency struct is " + sizeof(MyStruct));
MyStruct amount1, amount2;
MyStruct* pointerStruct = &amount1;
long* pDollars = &(pointerStruct->Dollars);
byte* pCents = &(pointerStruct->Cents);
Console.WriteLine("Address of amount1 is 0x{0:X}", (uint)&amount1);
Console.WriteLine("Address of amount2 is 0x{0:X}", (uint)&amount2);
Console.WriteLine("Address of pAmt is 0x{0:X}", (uint)&pointerStruct);
Console.WriteLine("Address of pDollars is 0x{0:X}", (uint)&pDollars);
Console.WriteLine("Address of pCents is 0x{0:X}", (uint)&pCents);
pointerStruct->Dollars = 20;
*pCents = 50;
Console.WriteLine("amount1 contains " + amount1);
--pointerStruct;
Console.WriteLine("amount2 has address 0x{0:X} and contains {1}",
(uint)pointerStruct, *pointerStruct);
MyStruct* pTempCurrency = (MyStruct*)pCents;
pCents = (byte*)(--pTempCurrency);
Console.WriteLine("Address of pCents is now 0x{0:X}", (uint)&pCents);
Console.WriteLine("
Now with classes");
MyClass amount3 = new MyClass();
fixed (long* pDollars2 = &(amount3.Dollars))
fixed (byte* pCents2 = &(amount3.Cents)) {
Console.WriteLine("amount3.Dollars has address 0x{0:X}", (uint)pDollars2);
Console.WriteLine("amount3.Cents has address 0x{0:X}", (uint)pCents2);
*pDollars2 = -100;
Console.WriteLine("amount3 contains " + amount3);
}
}
}
struct MyStruct {
public long Dollars;
public byte Cents;
public override string ToString() {
return "$" + Dollars + "." + Cents;
}
}
class MyClass {
public long Dollars;
public byte Cents;
public override string ToString() {
return "$" + Dollars + "." + Cents;
}
}
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
Allocating Memory from the Stack
using System;
public class MyClass
{
public unsafe static void Main()
{
int * buf = stackalloc int [5];
for(int i = 0; i < 5; i++) buf[i] = i; for(int i = 0; i < 5; i++) Console.WriteLine(buf[i]); } } [/csharp]
Fixing Managed Data in Memory
using System;
public class MyClass
{
public unsafe static void Main()
{
int i;
int [] intArr;
intArr = new int [5];
fixed(int * IntegerPointer = intArr)
{
for(i = 0; i < 5; i++)
IntegerPointer[i] = i;
}
for(i = 0; i < 5; i++)
Console.WriteLine(intArr[i]);
}
}
[/csharp]
Supported sizeof() Types
Expression Result
sizeof(sbyte) 1
sizeof(byte) 1
sizeof(short) 2
sizeof(ushort) 2
sizeof(int) 4
sizeof(uint) 4
sizeof(long) 8
sizeof(ulong) 8
sizeof(char) 2
sizeof(float) 4
sizeof(double) 8
sizeof(bool) 1
Accessing Structure Members with a Pointer
using System;
public struct Point2D {
public int X;
public int Y;
}
public class MyClass {
public unsafe static void Main() {
Point2D MyPoint;
Point2D * PointerToMyPoint;
MyPoint = new Point2D();
PointerToMyPoint = &MyPoint;
PointerToMyPoint->X = 100;
PointerToMyPoint->Y = 200;
Console.WriteLine("({0}, {1})", PointerToMyPoint->X, PointerToMyPoint->Y);
}
}
Unsafe Methods
using System;
public class MyClass
{
public unsafe static void Main()
{
int MyInteger = 123;
int * MyIntegerPointer = &MyInteger;
Console.WriteLine(*MyIntegerPointer);
}
}
int pointer variable
using System;
public class MainClass {
static void Main(string[] args) {
unsafe {
int variable = 10;
int* pVariable = &variable;
Console.WriteLine("Value at address is {0}.", *pVariable);
}
}
}