Is it possible to cause memory corruption in C# using the unsafe keyword

606 Views Asked by At

If I get a pointer to a first element of an array, can I just go beyond the range of the array and write arbitrary data to the GC heap memory, effectively breaking the heap and the garbage collector, or are there any safe guards in the CLR against this kind of messing.

Code example:

unsafe {
    int[] intArray = new int[100];
    fixed (int* ptr = intArray) {
        *(ptr - 42) = 456872;
    }
}

Is it possible to cause memory corruption in this way, potentially corrupting the CLRs own data structures?

0

There are 0 best solutions below