{"id":353,"date":"2022-05-02T13:51:17","date_gmt":"2022-05-02T13:51:17","guid":{"rendered":"https:\/\/baldsolutions.com\/?p=353"},"modified":"2022-05-02T13:54:18","modified_gmt":"2022-05-02T13:54:18","slug":"stackalloc-buffer-on-the-stack","status":"publish","type":"post","link":"https:\/\/baldsolutions.com\/index.php\/2022\/05\/02\/stackalloc-buffer-on-the-stack\/","title":{"rendered":"Stackalloc &#8211; buffer on the stack"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">Introduction<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Allocating some kind of a buffer (e.g. array) on a heap is a quite straightforward behaviour. It happens anytime we do something like this: <code>var array = new int[5]<\/code> . It is perfectly normal to use it &#8211; it is well known and intuitive solution. However, we can also allocate such a buffer on the stack. On the one hand it entails some risks,  but on the other it can safe some memory for us.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Stackalloc<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/language-reference\/operators\/stackalloc\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Stackalloc\">Stackalloc<\/a> is an expression that allocates a block of memory on the stack. Thanks to that we can allocate a buffer on the stack instead on the heap. To do that we can use <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.span-1?view=net-6.0\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Span\">Span<\/a> struct: <code>Span&lt;int> span = stackalloc int[7];<\/code>. The syntactic  seems to be pretty intuitive and understandable.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">BE CAREFUL: As it stands in the <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/language-reference\/operators\/stackalloc\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"\">documentation<\/a>: &#8220;The amount of memory available on the stack is limited. If you allocate too much memory on the stack, a\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.stackoverflowexception\" target=\"_blank\" rel=\"noreferrer noopener\">StackOverflowException<\/a>\u00a0is thrown&#8221;. It is also important to mention that the <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.stackoverflowexception\" target=\"_blank\" rel=\"noreferrer noopener\">StackOverflowException<\/a> is kind of exception that cannot be caught. In other words, <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.stackoverflowexception\" target=\"_blank\" rel=\"noreferrer noopener\">StackOverflowException<\/a> = application termination. It doesn&#8217;t mean that it shouldn&#8217;t be used at all. It does mean that it should be used with great care and awareness.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Example<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s consider the following <code>StackallocExecutor<\/code> class:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>public class StackallocExecutor\r\n{\r\n    private readonly int _size;\r\n    private readonly Random _random;\r\n\r\n    public StackallocExecutor(int size)\r\n    {\r\n        _size = size;\r\n        _random = new Random();\r\n    }\r\n\r\n    public int Stackalloc()\r\n    {\r\n        Span&lt;int> span = stackalloc int&#91;_size];\r\n\r\n        for (int i = 0; i &lt; span.Length; i++)\r\n            span&#91;i] = _random.Next();\r\n\r\n        for (int i = 0; i &lt; span.Length; i++)\r\n            if (i % 2 == 1)\r\n                span&#91;i] += 1;\r\n\r\n        var max = span&#91;0];\r\n\r\n        for (int i = 1; i &lt; span.Length; i++)\r\n            if (span&#91;i] > max)\r\n                max = span&#91;i];\r\n\r\n        return max;\r\n    }\r\n\r\n    public int NoStackalloc()\r\n    {\r\n        var array = new int&#91;_size];\r\n\r\n        for (int i = 0; i &lt; array.Length; i++)\r\n            array&#91;i] = _random.Next();\r\n\r\n        for (int i = 0; i &lt; array.Length; i++)\r\n            if (i % 2 == 1)\r\n                array&#91;i] += 1;\r\n\r\n        var max = array&#91;0];\r\n\r\n        for (int i = 1; i &lt; array.Length; i++)\r\n            if (array&#91;i] > max)\r\n                max = array&#91;i];\r\n\r\n        return max;\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The class has two methods <code>Stackalloc<\/code> and <code>NoStackalloc<\/code>. Both of them do the the same &#8211; create a buffer, populate the buffer, add &#8220;1&#8221; to the odd indexes and return the max value. Maybe it is not the most real-world scenario but I hadn&#8217;t better idea at the time of writing the code\ud83d\ude01.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Alright, but what&#8217;s the cool thing about the stackalloc at all? Let&#8217;s see the benchmark result!<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Benchmark<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">This time the crucial part of the results is the last column, i.e. &#8220;Allocated&#8221;.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"666\" height=\"300\" src=\"https:\/\/baldsolutions.com\/wp-content\/uploads\/2022\/05\/image.png\" alt=\"\" class=\"wp-image-358\" srcset=\"https:\/\/baldsolutions.com\/wp-content\/uploads\/2022\/05\/image.png 666w, https:\/\/baldsolutions.com\/wp-content\/uploads\/2022\/05\/image-300x135.png 300w\" sizes=\"auto, (max-width: 666px) 100vw, 666px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">As you can see, no matter what the size of buffer is, the &#8220;Allocated&#8221; value for the <code>Stackalloc<\/code> method is the same. <code>NoStackalloc<\/code> method does quite the opposite, the greater size, the greater allocations.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Summary<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">To sum up, having an ability to allocate reasonably small buffer on the stack seems to be interesting and useful feature. However, the <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.stackoverflowexception\" target=\"_blank\" rel=\"noreferrer noopener\">StackOverflowException<\/a> is still there and might kill the application without hesitation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As always I&#8217;ve prepared an example and you can find it on my <a href=\"https:\/\/github.com\/tglowka\/baldsolutions\/tree\/master\/.NET\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"github\">github<\/a>, project: <a href=\"https:\/\/github.com\/tglowka\/baldsolutions\/tree\/master\/.NET\/stackalloc\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"arraypool\">stackalloc<\/a>, benchmark project: <a href=\"https:\/\/github.com\/tglowka\/baldsolutions\/tree\/master\/.NET\/stackalloc-benchmark\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"\">stackalloc-benchmark<\/a>.<br>I encourage you to go through the code, debug it and try to test your own scenarios e.g. killing an application with StackOverflowException (for me the following throws the exception):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var size = 1_000_000;\r\nvar executor = new StackallocExecutor(size);\r\nexecutor.Stackalloc();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Have a nice day, bye!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Allocating some kind of a buffer (e.g. array) on a heap is a quite straightforward behaviour. It happens anytime we do something like this:&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/baldsolutions.com\/index.php\/2022\/05\/02\/stackalloc-buffer-on-the-stack\/\">Continue reading<span class=\"screen-reader-text\">Stackalloc &#8211; buffer on the stack<\/span><\/a><\/div>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-353","post","type-post","status-publish","format-standard","hentry","category-net","entry"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.8 - aioseo.com -->\n\t<meta name=\"description\" content=\"Introduction Allocating some kind of a buffer (e.g. array) on a heap is a quite straightforward behaviour. It happens anytime we do something like this: var array = new int[5] . It is perfectly normal to use it - it is well known and intuitive solution. However, we can also allocate such a buffer on\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Tomasz\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/baldsolutions.com\/index.php\/2022\/05\/02\/stackalloc-buffer-on-the-stack\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.8\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Bald Solutions -\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Stackalloc \u2013 buffer on the stack - Bald Solutions\" \/>\n\t\t<meta property=\"og:description\" content=\"Introduction Allocating some kind of a buffer (e.g. array) on a heap is a quite straightforward behaviour. It happens anytime we do something like this: var array = new int[5] . It is perfectly normal to use it - it is well known and intuitive solution. However, we can also allocate such a buffer on\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/baldsolutions.com\/index.php\/2022\/05\/02\/stackalloc-buffer-on-the-stack\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2022-05-02T13:51:17+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2022-05-02T13:54:18+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Stackalloc \u2013 buffer on the stack - Bald Solutions\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Introduction Allocating some kind of a buffer (e.g. array) on a heap is a quite straightforward behaviour. It happens anytime we do something like this: var array = new int[5] . It is perfectly normal to use it - it is well known and intuitive solution. However, we can also allocate such a buffer on\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/2022\\\/05\\\/02\\\/stackalloc-buffer-on-the-stack\\\/#blogposting\",\"name\":\"Stackalloc \\u2013 buffer on the stack - Bald Solutions\",\"headline\":\"Stackalloc &#8211; buffer on the stack\",\"author\":{\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/author\\\/tomasz3396\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/#organization\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/baldsolutions.com\\\/wp-content\\\/uploads\\\/2022\\\/05\\\/image.png\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/2022\\\/05\\\/02\\\/stackalloc-buffer-on-the-stack\\\/#articleImage\",\"width\":666,\"height\":300},\"datePublished\":\"2022-05-02T13:51:17+00:00\",\"dateModified\":\"2022-05-02T13:54:18+00:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/2022\\\/05\\\/02\\\/stackalloc-buffer-on-the-stack\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/2022\\\/05\\\/02\\\/stackalloc-buffer-on-the-stack\\\/#webpage\"},\"articleSection\":\".NET\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/2022\\\/05\\\/02\\\/stackalloc-buffer-on-the-stack\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/baldsolutions.com#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/baldsolutions.com\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/category\\\/net\\\/#listItem\",\"name\":\".NET\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/category\\\/net\\\/#listItem\",\"position\":2,\"name\":\".NET\",\"item\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/category\\\/net\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/2022\\\/05\\\/02\\\/stackalloc-buffer-on-the-stack\\\/#listItem\",\"name\":\"Stackalloc &#8211; buffer on the stack\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/baldsolutions.com#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/2022\\\/05\\\/02\\\/stackalloc-buffer-on-the-stack\\\/#listItem\",\"position\":3,\"name\":\"Stackalloc &#8211; buffer on the stack\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/category\\\/net\\\/#listItem\",\"name\":\".NET\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/#organization\",\"name\":\"Bald Solutions\",\"url\":\"https:\\\/\\\/baldsolutions.com\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/author\\\/tomasz3396\\\/#author\",\"url\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/author\\\/tomasz3396\\\/\",\"name\":\"Tomasz\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/2022\\\/05\\\/02\\\/stackalloc-buffer-on-the-stack\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d72f2f7f389921b820e5ef7c2880172fc2b9ae47698a933356df224581de0cc7?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Tomasz\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/2022\\\/05\\\/02\\\/stackalloc-buffer-on-the-stack\\\/#webpage\",\"url\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/2022\\\/05\\\/02\\\/stackalloc-buffer-on-the-stack\\\/\",\"name\":\"Stackalloc \\u2013 buffer on the stack - Bald Solutions\",\"description\":\"Introduction Allocating some kind of a buffer (e.g. array) on a heap is a quite straightforward behaviour. It happens anytime we do something like this: var array = new int[5] . It is perfectly normal to use it - it is well known and intuitive solution. However, we can also allocate such a buffer on\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/2022\\\/05\\\/02\\\/stackalloc-buffer-on-the-stack\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/author\\\/tomasz3396\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/author\\\/tomasz3396\\\/#author\"},\"datePublished\":\"2022-05-02T13:51:17+00:00\",\"dateModified\":\"2022-05-02T13:54:18+00:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/#website\",\"url\":\"https:\\\/\\\/baldsolutions.com\\\/\",\"name\":\"Bald Solutions\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Stackalloc \u2013 buffer on the stack - Bald Solutions","description":"Introduction Allocating some kind of a buffer (e.g. array) on a heap is a quite straightforward behaviour. It happens anytime we do something like this: var array = new int[5] . It is perfectly normal to use it - it is well known and intuitive solution. However, we can also allocate such a buffer on","canonical_url":"https:\/\/baldsolutions.com\/index.php\/2022\/05\/02\/stackalloc-buffer-on-the-stack\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/baldsolutions.com\/index.php\/2022\/05\/02\/stackalloc-buffer-on-the-stack\/#blogposting","name":"Stackalloc \u2013 buffer on the stack - Bald Solutions","headline":"Stackalloc &#8211; buffer on the stack","author":{"@id":"https:\/\/baldsolutions.com\/index.php\/author\/tomasz3396\/#author"},"publisher":{"@id":"https:\/\/baldsolutions.com\/#organization"},"image":{"@type":"ImageObject","url":"https:\/\/baldsolutions.com\/wp-content\/uploads\/2022\/05\/image.png","@id":"https:\/\/baldsolutions.com\/index.php\/2022\/05\/02\/stackalloc-buffer-on-the-stack\/#articleImage","width":666,"height":300},"datePublished":"2022-05-02T13:51:17+00:00","dateModified":"2022-05-02T13:54:18+00:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/baldsolutions.com\/index.php\/2022\/05\/02\/stackalloc-buffer-on-the-stack\/#webpage"},"isPartOf":{"@id":"https:\/\/baldsolutions.com\/index.php\/2022\/05\/02\/stackalloc-buffer-on-the-stack\/#webpage"},"articleSection":".NET"},{"@type":"BreadcrumbList","@id":"https:\/\/baldsolutions.com\/index.php\/2022\/05\/02\/stackalloc-buffer-on-the-stack\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/baldsolutions.com#listItem","position":1,"name":"Home","item":"https:\/\/baldsolutions.com","nextItem":{"@type":"ListItem","@id":"https:\/\/baldsolutions.com\/index.php\/category\/net\/#listItem","name":".NET"}},{"@type":"ListItem","@id":"https:\/\/baldsolutions.com\/index.php\/category\/net\/#listItem","position":2,"name":".NET","item":"https:\/\/baldsolutions.com\/index.php\/category\/net\/","nextItem":{"@type":"ListItem","@id":"https:\/\/baldsolutions.com\/index.php\/2022\/05\/02\/stackalloc-buffer-on-the-stack\/#listItem","name":"Stackalloc &#8211; buffer on the stack"},"previousItem":{"@type":"ListItem","@id":"https:\/\/baldsolutions.com#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/baldsolutions.com\/index.php\/2022\/05\/02\/stackalloc-buffer-on-the-stack\/#listItem","position":3,"name":"Stackalloc &#8211; buffer on the stack","previousItem":{"@type":"ListItem","@id":"https:\/\/baldsolutions.com\/index.php\/category\/net\/#listItem","name":".NET"}}]},{"@type":"Organization","@id":"https:\/\/baldsolutions.com\/#organization","name":"Bald Solutions","url":"https:\/\/baldsolutions.com\/"},{"@type":"Person","@id":"https:\/\/baldsolutions.com\/index.php\/author\/tomasz3396\/#author","url":"https:\/\/baldsolutions.com\/index.php\/author\/tomasz3396\/","name":"Tomasz","image":{"@type":"ImageObject","@id":"https:\/\/baldsolutions.com\/index.php\/2022\/05\/02\/stackalloc-buffer-on-the-stack\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/d72f2f7f389921b820e5ef7c2880172fc2b9ae47698a933356df224581de0cc7?s=96&d=mm&r=g","width":96,"height":96,"caption":"Tomasz"}},{"@type":"WebPage","@id":"https:\/\/baldsolutions.com\/index.php\/2022\/05\/02\/stackalloc-buffer-on-the-stack\/#webpage","url":"https:\/\/baldsolutions.com\/index.php\/2022\/05\/02\/stackalloc-buffer-on-the-stack\/","name":"Stackalloc \u2013 buffer on the stack - Bald Solutions","description":"Introduction Allocating some kind of a buffer (e.g. array) on a heap is a quite straightforward behaviour. It happens anytime we do something like this: var array = new int[5] . It is perfectly normal to use it - it is well known and intuitive solution. However, we can also allocate such a buffer on","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/baldsolutions.com\/#website"},"breadcrumb":{"@id":"https:\/\/baldsolutions.com\/index.php\/2022\/05\/02\/stackalloc-buffer-on-the-stack\/#breadcrumblist"},"author":{"@id":"https:\/\/baldsolutions.com\/index.php\/author\/tomasz3396\/#author"},"creator":{"@id":"https:\/\/baldsolutions.com\/index.php\/author\/tomasz3396\/#author"},"datePublished":"2022-05-02T13:51:17+00:00","dateModified":"2022-05-02T13:54:18+00:00"},{"@type":"WebSite","@id":"https:\/\/baldsolutions.com\/#website","url":"https:\/\/baldsolutions.com\/","name":"Bald Solutions","inLanguage":"en-US","publisher":{"@id":"https:\/\/baldsolutions.com\/#organization"}}]},"og:locale":"en_US","og:site_name":"Bald Solutions -","og:type":"article","og:title":"Stackalloc \u2013 buffer on the stack - Bald Solutions","og:description":"Introduction Allocating some kind of a buffer (e.g. array) on a heap is a quite straightforward behaviour. It happens anytime we do something like this: var array = new int[5] . It is perfectly normal to use it - it is well known and intuitive solution. However, we can also allocate such a buffer on","og:url":"https:\/\/baldsolutions.com\/index.php\/2022\/05\/02\/stackalloc-buffer-on-the-stack\/","article:published_time":"2022-05-02T13:51:17+00:00","article:modified_time":"2022-05-02T13:54:18+00:00","twitter:card":"summary","twitter:title":"Stackalloc \u2013 buffer on the stack - Bald Solutions","twitter:description":"Introduction Allocating some kind of a buffer (e.g. array) on a heap is a quite straightforward behaviour. It happens anytime we do something like this: var array = new int[5] . It is perfectly normal to use it - it is well known and intuitive solution. However, we can also allocate such a buffer on"},"aioseo_meta_data":{"post_id":"353","title":"#post_title #separator_sa #site_title","description":"#post_excerpt","keywords":[],"keyphrases":{"focus":{"keyphrase":"","score":0,"analysis":{"keyphraseInTitle":{"score":0,"maxScore":9,"error":1}}},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":[],"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[],"defaultGraph":"Article","defaultPostTypeGraph":""},"schema_type":"default","schema_type_options":"{\"article\":{\"articleType\":\"BlogPosting\"},\"course\":{\"name\":\"\",\"description\":\"\",\"provider\":\"\"},\"faq\":{\"pages\":[]},\"product\":{\"reviews\":[]},\"recipe\":{\"ingredients\":[],\"instructions\":[],\"keywords\":[]},\"software\":{\"reviews\":[],\"operatingSystems\":[]},\"webPage\":{\"webPageType\":\"WebPage\"}}","pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2022-05-02 12:56:14","updated":"2025-06-04 04:22:54","seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/baldsolutions.com\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/baldsolutions.com\/index.php\/category\/net\/\" title=\".NET\">.NET<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tStackalloc \u2013 buffer on the stack\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/baldsolutions.com"},{"label":".NET","link":"https:\/\/baldsolutions.com\/index.php\/category\/net\/"},{"label":"Stackalloc &#8211; buffer on the stack","link":"https:\/\/baldsolutions.com\/index.php\/2022\/05\/02\/stackalloc-buffer-on-the-stack\/"}],"_links":{"self":[{"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/posts\/353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/comments?post=353"}],"version-history":[{"count":13,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/posts\/353\/revisions"}],"predecessor-version":[{"id":367,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/posts\/353\/revisions\/367"}],"wp:attachment":[{"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/media?parent=353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/categories?post=353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/tags?post=353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}