{"id":7081,"date":"2024-02-01T08:55:00","date_gmt":"2024-02-01T08:55:00","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=7081"},"modified":"2024-12-06T12:21:53","modified_gmt":"2024-12-06T12:21:53","slug":"what-is-solidity-memory-in-ethereum","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/what-is-solidity-memory-in-ethereum\/","title":{"rendered":"What is the Solidity memory Keyword in Ethereum?"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_56_1 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/metaschool.so\/articles\/what-is-solidity-memory-in-ethereum\/#Memory_in_Solidity\" title=\"Memory in Solidity\">Memory in Solidity<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/metaschool.so\/articles\/what-is-solidity-memory-in-ethereum\/#Example_of_Memory_in_Solidity\" title=\"Example of Memory in Solidity\">Example of Memory in Solidity<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/metaschool.so\/articles\/what-is-solidity-memory-in-ethereum\/#Conclusion\" title=\"Conclusion\">Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n\n<p><a href=\"https:\/\/metaschool.so\/articles\/what-is-a-smart-contract\/\" target=\"_blank\" rel=\"noreferrer noopener\">Smart contracts<\/a> are the backbone of decentralized apps. They handle the storage and manipulation of data. <a href=\"https:\/\/metaschool.so\/articles\/guide-solidity-contracts\/\" target=\"_blank\" rel=\"noreferrer noopener\">Solidity<\/a> is the language of Ethereum. It lets you store data in memory or storage during the contract execution. Now, let&#8217;s dig into a cool part of their work: solidity memory. Think of it as a short-term memory for a computer, kind of like its RAM.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"memory-in-solidity\"><span class=\"ez-toc-section\" id=\"Memory_in_Solidity\"><\/span>Memory in Solidity<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In Solidity, think of memory as a quick storage spot mostly holding function details and local bits of info. Unlike storage, which sticks around even after the job is done, memory is more of a temporary helper.<\/p>\n\n\n\n<p>Because memory space is limited, we have to actively clear out local stuff once a function is done. Every time a function actively calls, it makes new local spots in memory. This is why it&#8217;s crucial to actively be smart about using them.<\/p>\n\n\n\n<p><strong>\ud83d\udd25 Check this out:<\/strong>\u00a0<a href=\"https:\/\/metaschool.so\/build-in-public\">B<\/a><a href=\"https:\/\/metaschool.so\/build-in-public\" target=\"_blank\" rel=\"noreferrer noopener\">uild in Public<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-of-memory-in-solidity\"><span class=\"ez-toc-section\" id=\"Example_of_Memory_in_Solidity\"><\/span>Example of Memory in Solidity<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Let&#8217;s illustrate the concept with a simple Solidity smart contract:<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:16px\"><code>pragma solidity ^0.8.9;\ncontract memoryExampleContract {\n    function setRecentYear() public pure returns (uint&#91;5] memory)\n    {\n        uint&#91;5] memory year;\n        year&#91;0] = 2024;\n        return year;\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Smart Contract Declaration:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The contract, named <strong><code>memoryExampleContract<\/code><\/strong>, is declared in Solidity.<\/li><li>The compiler version is specified as ^0.8.9.<\/li><\/ul>\n\n\n\n<p><strong>Function to Set Recent Year:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The contract includes a publicly accessible and pure function, <strong><code>setRecentYear<\/code><\/strong>.<\/li><li>Within this function, a dynamic array named <strong><code>year<\/code><\/strong> is declared in memory to store a single <strong><code>uint<\/code><\/strong> value.<\/li><li>The value of <strong><code>year[0]<\/code><\/strong> is set to 2024.<\/li><li>The function returns the array <strong><code>year<\/code><\/strong> as a memory reference.<\/li><li>The &#8220;year&#8221; array is automatically deleted from memory once the function concludes.<\/li><\/ul>\n\n\n\n<p>\ud83d\udd25<strong>\u00a0Check this course out:<\/strong> <a href=\"https:\/\/metaschool.so\/courses\/build-on-move-on-sui-and-explore-its-applications\" target=\"_blank\" rel=\"noreferrer noopener\">Build on Move on Sui and Explore its Applications<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Solidity memory is a temporary data storage space, much like a computer&#8217;s RAM. It&#8217;s fast at grabbing info, making it great for dealing with data that doesn&#8217;t need to stick around. But developers need to watch out for memory limits and use them wisely to make smart contracts work their best.<\/p>\n\n\n\n<p>As the decentralized world keeps changing, getting how memory and storage work in Solidity becomes super important for crafting smart contracts that are both efficient and secure.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Tried this out? Let us know how it went by taking a screenshot and tagging Metaschool on your Social Media!<\/strong><\/p>\n\n\n\n<p><strong>Follow us on<\/strong>&nbsp;\u2013<\/p>\n\n\n\n<p>\ud83d\udd2eTwitter \u2013&nbsp;<a href=\"https:\/\/twitter.com\/0xmetaschool\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/twitter.com\/0xmetaschool<\/a><\/p>\n\n\n\n<p>\ud83d\udd17LinkedIn \u2013&nbsp;<a href=\"https:\/\/www.linkedin.com\/company\/0xmetaschool\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.linkedin.com\/company\/0xmetaschool\/<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":16,"featured_media":11069,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[292],"tags":[51,31,69,46,29],"class_list":["post-7081","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-answers","tag-blockchain","tag-ethereum-blockchain","tag-learn-web3","tag-solidity","tag-web3"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7081","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/comments?post=7081"}],"version-history":[{"count":9,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7081\/revisions"}],"predecessor-version":[{"id":11070,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7081\/revisions\/11070"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/11069"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=7081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=7081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=7081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}