Use stackalloc to allocate memory for integer array : stackalloc « Language Basics « C# / C Sharp






Use stackalloc to allocate memory for integer array

 

using System;

class TestStackallocApp
{
    unsafe public static void Foo(int* pa)
    {
        for (int* ip = pa; ip < (pa+5); ip++)
        {
            Console.WriteLine("value {0} at address: {1}", *ip, (int)ip);
        }
    }
   
    static void Main(string[] args)
    {
        unsafe
        {
            int* pa = stackalloc int[5];
            pa[0] = 12;
            pa[1] = 34;
            pa[2] = 56;
            pa[3] = 78;
            pa[4] = 90;
            Foo(pa);
        }
    }
}

 








Related examples in the same category

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