Allocating Memory from the Stack : Unsafe Code « Language Basics « C# / C Sharp






Allocating Memory from the Stack

 

using System;
   
public class MyClass
{
    public unsafe static void Main()
    {
        int * buf = stackalloc int [5];
   
        for(int i = 0; i < 5; i++)
            buf[i] = i;
        for(int i = 0; i < 5; i++)
            Console.WriteLine(buf[i]);
    }
}
           
         
  








Related examples in the same category

1.int pointer variable
2.Unsafe Methods
3.Accessing Structure Members with a Pointer
4.Supported sizeof() Types
5.Fixing Managed Data in Memory
6.Get variable address in unsafe mode
7.Unsafe code: get data type size
8.Address and size of pointer object
9.Using the unsafe keyword
10.object pointer
11.Use unsafe method to clone array
12.unsafe and fixed block
13.mark method as unsafe
14.Use unsage code to swap two integers
15.Pointer for struct
16.Compare pointer