![]() The simple explanation is that MyInt2 is allocated storage alongside MyInt and when we assign an 8-byte value to MyInt the extra 4 bytes overwrite MyInt2. You might be surprised to discover that the value of MyInt2 has changed and is no longer zero, even though it isn’t assigned a new value within the program. This is the reason pointers are considered unsafe. Of course, reading from a memory location that you don’t understand is fairly safe, but who knows what the effect of writing to such a location is going to have. The answer is that you have succeeded in reading data from a neighbouring memory location, one that you normally would not be able to access. Where did the additional 4 bytes come from? If you try this out you will find that it works but it is mostly nonsense because the original int was only 4 bytes of storage and the double is 8 bytes. double*, and finally uses the indirection operator to return the value so pointed at. a 32-bit integer, then casts it to a double pointer, i.e. This sets a void pointer to an integer, i.e. MessageBox.Show((*(double*)pMyData).ToString()) Notice that this process can produce complete nonsense. Obviously it makes no sense to use indirection on a void pointer, what would the data type of the result be, but you can always cast a void pointer and then use indirection. The indirection operator and the address of operator really are the inverse of one another.Ī pointer can be null and applying the indirection operator in this case generates an exception. the value of whatever pMyInt is pointing at. For example:ĭisplays the current contents of MyInt, i.e. The indirection operator returns the values stored at the address that a pointer is pointing at. Also notice that the fact that the way that machine represents an address comes into consideration and this is a reflection of the fact that pointers are a low level construct. Notice that this is not deferencing the pointer but showing you the pointer's contents. Of course this assumes that the current implementation of int is large enough to contain a pointer, i.e. MessageBox.Show(((int)pMyInt).ToString()) ![]() ![]() So, to display the contents of the pointer you would use something like: What you can do, however, is to use a cast to convert a pointer to a more usual data type. For example, you can’t use a ToString() method call to display the value of a pointer. The first important thing to note is that a pointer does not inherit from an object and so there are no methods associated with it and no boxing and unboxing. pMyInt and stores the address of the integer MyInt in the pointer. The * is the dereferencing or indirection operator and is generally used in conjunction with the address of operator &, which as its name suggests, returns the address of a variable.Ĭreates a pointer to an integer, i.e. To declare a pointer type C# uses the C++ like syntax: to use pointer arithmetic, you need to cast a void pointer to a pointer to a given type. a pointer to an unknown type but to be of any use in, i.e. You can also have a pointer of type void, i.e. This is generally enough to allow pointers to be used to work with legacy implementations of data structures or to work with binary data structures that have been received over the network or from a file. However you can have a pointer to a struct that contains simple value types and you can create pointers to arrays of simple value types. This is restrictive and basically amounts to not allowing pointers to anything created on the heap or subject to dynamic memory management. So you can’t have a pointer to an object, or to a delegate or to a reference. int, float, char, to an enum, to another pointer or to a struct that doesn’t contain other managed types. Then any use of pointers has to be enclosed in an usafe block to mark it out even more clearly.Įven more restrictive is the fact that you cannot create a pointer to anything you care to point at, only to a restricted subset of types that have a simpler way of using memory so making pointer use slightly less tricky.Įssentially you can only create a pointer to any simple value type, e.g. First the entire project has to be marked as unsafe by using the Project Properties to set the "Build, Allow Unsafe Code" flag. Pointers are so dangerous that they have to be quarantined within your code. In other words, a reference that you can do arithmetic with, is a pointer. ![]() Pointers in C#' are a generalisation of the reference type to include pointer arithmetic.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |