C# Stackalloc Operator

Star (asterisk) character

Stackalloc allocates memory in an unsafe context. This C# operator is similar to the alloca function from the C language: it implements a form of malloc that frees the memory when the calling function returns. Stackalloc can be used to create buffers for usage with external code or string constructors.

Keywords

Example

Note

This program uses an unsafe method context, which means that to compile it you must enable unsafe code to be used. It is meant to demonstrate an example of the stackalloc operator for allocating memory on the stack, not as a useful method for real-world programs.

Basically, the stackalloc operator is similar to the alloca function in C, which is a form of malloc that frees its memory automatically. The program here allocates a character buffer on the stack, writes values to it, and then returns a string type that was created through the constructor and will reside on the managed heap.

Program that uses stackalloc operator [C#]

using System;

class Program
{
    unsafe static string GetStringStackalloc()
    {
	// Allocate a character buffer with the stackalloc operator.
	// ... Assign the memory to specific letters.
	// ... Add the terminal character.
	char* buffer = stackalloc char[50 + 1];
	for (int i = 0; i < 10; i++)
	{
	    buffer[i] = 'a';
	}
	for (int i = 10; i < 50; i++)
	{
	    buffer[i] = 'z';
	}
	buffer[50] = '\0';
	return new string(buffer);
    }

    static void Main()
    {
	// Call the stackalloc method to get the result string.
	// ... Also demonstrate its correctness by using the string constructor.
	Console.WriteLine(GetStringStackalloc());
	Console.WriteLine(new string('a', 10) + new string('z', 40));
    }
}

Output

aaaaaaaaaazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
aaaaaaaaaazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
Main method

Description. This program introduces two methods: the GetStringStackalloc method, which internally uses the stackalloc operator and is in an unsafe context; and the Main entry point, where control flow begins.

The stackalloc operator is used in the first method to allocate a buffer of 51 characters on the stack, which is 102 bytes. The memory is initialized to the values 'a' and 'z'. The fifty-first character is set to the null character literal, to signal that the string is only 50 characters long and that no uninitialized memory following this should be stored.

String Constructor

Review: You can initialize a character buffer with stackalloc and then use that to construct a new string type. The stackalloc operator can also be used for other primitive data type buffers.

Question and answer

When is memory freed? The C# specification describes the purpose of the stackalloc operator as a way to circumvent the standard garbage collection mechanism. The C# language describes the stackalloc operator as a version of the alloca function in many C implementations: the memory allocated by the function is freed upon return from the function, as when the activation record is popped from the stack.

Summary

The C# programming language

We described an example of the stackalloc operator in an unsafe context. The stackalloc operator is used as a way of manually allocated memory buffers that can be used without type safety checks. Stackalloc can be used for external DLL code written in other languages, and it is freed when the method that uses the operator returns.

Unsafe Keyword
.NET