2

I am a bit confused on when fixed block is required. I have example which gives me a contradicting scenario below:

enum RoomType { Economy, Buisness, Executive, Deluxe }; struct HotelRoom { public int Number; public bool Taken; public RoomType Category; public void Print() { String status = Taken ? "Occupied" : "available"; Console.WriteLine("Room {0} is of {1} class and is currently {2}", Number, Category, status); } } 

I have made a function which will take a pointer to a HotelRoom

private unsafe static void Reserve(HotelRoom* room) { if (room->Taken) Console.WriteLine("Cannot reserve room {0}", room->Number); else room->Taken = true; } 

In the main method I have the following:

unsafe static void Main(string[] args) { HotelRoom[] myfloor = new HotelRoom[4]; for (int i = 0; i < myfloor.Length; i++) { myfloor[i].Number = 501 + i; myfloor[i].Taken = false; myfloor[i].Category = (RoomType)i; } HotelRoom Room = myfloor[1]; Reserve(&Room); //I am able to do this without fixed block. //Reserve(&myfloor[1]); //Not able to do this so have to use fixed block below. fixed (HotelRoom* pRoom = &myfloor[1]) { Reserve(pRoom); } myfloor[1].Print(); Room.Print(); } 

My confusion is I am able to do Reserve(&Room) but not Reserve(&myfloor[1]). I think they're doing the same thing - passing memeory address of a HotelRoom struct to the Reserve function. Why do I need fixed to do this?

2 Answers 2

1

Room is a local variable which is stored on stack, while myfloor is stored in heap. Garbage collector can move objects in the heap to compact it (adresses will change), so you need to "pin" them. That's why you need fixed statement.

Update:

Also, there is a way to allocate memory on stack insted of heap:

HotelRoom* fib = stackalloc HotelRoom[4];

In this case you won't need fixed statement.

Small disclamer: being able to do this doesn't mean you should of course. As others already mentioned, it is very non-.NET way of writing code, so I just consider this question is theoretical.

Sign up to request clarification or add additional context in comments.

1 Comment

got I altogether missed that point.. You immdeiately pointed. thx.
1

Is this just a throw-away example?

or...

I'm guessing you're trying to rewrite C++ as C#. You very rarely need to use unsafe, the major point of .NET is that it is a managed framework.

Unless I'm completely wrong here (all your code could be written as managed code), you should read up on the differences between unmanaged and managed code, especially how to write C# coming from a C++ background.

Have a look at these:

what is the difference between “managed” vs “unmanaged”?

Difference between Managed Code and Unmanaged Code?

C# for C++ Developers

.NET Book Zero

(edited from C# books or web sites for C++ developers [closed])

5 Comments

you guessed write I also have C++ bagrounds. I was wondering I could do this by uisng ref keyword to pass parameter. In C# there are times we need pointer one instance I noted is. Suppose I want my implemntation of string reversal, then only we i could think of was.
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"; string x = "Hello World"; Reverse(greet); Console.WriteLine(greet); Console.WriteLine(x); } }
link gave good idea about managed/unmanged code. I was wondering if unmanged code is not recommendable why Microsoft kept it in .Net framewrok??
unsafe is there because it can be useful for performance (for instance using a pointer to iterate along a long 'string' using a pointer, without worrying about the overflow checks, e.g. a CRC implementation can take advantage of this) and especially it is often necessary for InterOp (accessing COM objects from .NET). For string reversal, you just iterate through the string backwards, creating a new 'StringBuilder' as you go, no need for pointers :)
Oh, underneath all the .NET code, the actual work will be done in pointers, of course. however, the managed framework hides all this complexity and potential for error from you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.