|Deep C# - What's The Matter With Pointers?|
|Written by Mike James|
|Thursday, 21 November 2019|
Page 4 of 4
As well as working with value types you can create your own primitive data types using the stack.
The statement stackalloc type[n] allocates enough stack to store n copies of the stated data type and returns a pointer to the start of the allocation.
You don’t need to fix the storage as the stack isn’t moved or garbage collected while the variables are in scope.
You also don’t have to remember to deallocate the memory because the stack is automatically cleaned up when the variables go out of scope, usually when the method that declared them returns.
allocates 100 integers, i.e. 400 bytes, on the stack and uses the pointer to store 34 in the four bytes starting at the 40th byte and then displays this value.
Notice that the use of the array indexing makes this look exactly like allocating and using a standard array. However, the block of memory really is just a block of memory that you can do what you like with.
allocates a struct on the stack but this is really just sizeof(MyStructType) which happens to be just two integers, i.e. 8 bytes.
We can use the pointer to the structure in the usual way to set or access a field. That is, we can use the block as if it was a struct but if we want to we can just treat it as a block of 8 bytes and use it as some other data structure.
For example, if you want to treat the data as an array of int you can by casting the pointer:
This expression is a little difficult to follow so a simpler multi-step version is:
This casts a pointer to int then uses array indexing to access the first element of the array which is the same as the x field of the struct. Notice that this correspondence depends on the way the struct is organised in memory and currently the C# compiler stores fields in the order that they are declared.
A range of attributes can be applied to ask the compiler to use particular memory layouts for a struct, see Inside C# 4 Data Structs, but even then you are making use of details of implementation that could change and make your program invalid.
What’s it all for?
You might be excited to learn all about pointers but, as you might have detected, they are not really a good idea. C and C++ programmers moving to C# tend to think in terms of pointers and hence like the facility, but in practice you almost never need them.
If you need to do a quick conversion of a C/C++ program that uses pointer then it might be acceptable to use C# pointers to get things moving but it should be considered a stop-gap measure until you can implement the ideas without pointers.
About the only time pointers might be necessary is in making use of API calls within the P/Invoke subsystem. Even here there are usually alternatives and often passing by reference solves even pointer to pointer problems. Things do get more difficult when you need to pass a pointer to a block of memory and in this case you might have to use a fixed array or allocate memory on the stack and pass it directly to the API call.
In most cases you don't need to use pointers when implementing an algorithm in C# from scratch.
More Deep C#
or email your comment to: email@example.com
|Last Updated ( Saturday, 23 November 2019 )|