stackalloc Demo : stackalloc « Language Basics « C# / C Sharp






stackalloc Demo

 
using System;

class MainEntryPoint {
    static unsafe void Main() {
        Console.Write("How big an array do you want? \n> ");
        string userInput = Console.ReadLine();
        uint size = uint.Parse(userInput);

        long* pArray = stackalloc long[(int)size];
        for (int i = 0; i < size; i++)
            pArray[i] = i * i;

        for (int i = 0; i < size; i++)
            Console.WriteLine("Element {0} = {1}", i, *(pArray + i));
    }
}

 








Related examples in the same category

1.Allocating Memory from the Stack
2.Invalid Cast Exceptions with Implicit Operators
3.allocates 26 characters on the stack the for loop assigns alphabetic characters to each element
4.Use stackalloc to allocate memory for integer array