{"id":113,"date":"2026-05-21T04:37:51","date_gmt":"2026-05-21T04:37:51","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=113"},"modified":"2026-05-22T10:44:34","modified_gmt":"2026-05-22T10:44:34","slug":"dynamic-memory-allocation","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=dynamic-memory-allocation","title":{"rendered":"Dynamic Memory Allocation"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Dynamic memory allocation in C++ allows memory to be allocated during program execution. It helps programs use memory efficiently according to their needs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Dynamic Memory Allocation?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Dynamic memory allocation means creating memory at runtime instead of compile time. The memory is allocated from heap memory using pointers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use Dynamic Memory Allocation?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Dynamic memory allocation is useful because it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Allocates memory when needed<\/li>\n\n\n\n<li>Saves unused memory<\/li>\n\n\n\n<li>Supports flexible program design<\/li>\n\n\n\n<li>Helps manage large data dynamically<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Heap Memory<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Dynamic memory is stored in the heap area of memory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Unlike stack memory, heap memory is manually managed by the programmer.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Operators Used<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">C++ provides:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>new<\/code> \u2192 allocate memory<\/li>\n\n\n\n<li><code>delete<\/code> \u2192 free memory<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Allocating Single Variable<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>int *ptr = new int;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Assigning Value<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>*ptr = 100;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example Program<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br><br>    int *ptr = new int;<br><br>    *ptr = 50;<br><br>    cout &lt;&lt; *ptr &lt;&lt; endl;<br><br>    delete ptr;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>50<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How Dynamic Allocation Works<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>new<\/code> allocates memory in heap<\/li>\n\n\n\n<li>Pointer stores memory address<\/li>\n\n\n\n<li>Data is accessed using pointer<\/li>\n\n\n\n<li><code>delete<\/code> frees allocated memory<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Dynamic Array Allocation<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>int *arr = new int&#91;5];<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This creates an array of 5 integers dynamically.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Dynamic Array<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br><br>    int size = 5;<br><br>    int *arr = new int&#91;size];<br><br>    for (int i = 0; i &lt; size; i++) {<br>        arr&#91;i] = i + 1;<br>    }<br><br>    for (int i = 0; i &lt; size; i++) {<br>        cout &lt;&lt; arr&#91;i] &lt;&lt; endl;<br>    }<br><br>    delete&#91;] arr;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">delete vs delete[]<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>delete<\/th><th>delete[]<\/th><\/tr><\/thead><tbody><tr><td>Used for single variable<\/td><td>Used for arrays<\/td><\/tr><tr><td>Frees one memory block<\/td><td>Frees multiple memory blocks<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Null Pointer After Deletion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Good practice:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ptr = nullptr;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This avoids dangling pointers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Memory Leak<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A memory leak happens when allocated memory is not released.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example of Memory Leak<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>int *ptr = new int;<br><br>\/\/ forgot delete<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Dangling Pointer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A dangling pointer points to memory that has already been freed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>delete ptr;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After deletion:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ptr = nullptr;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of Dynamic Memory Allocation<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Flexible memory usage<\/li>\n\n\n\n<li>Efficient resource management<\/li>\n\n\n\n<li>Useful for large programs<\/li>\n\n\n\n<li>Supports dynamic data structures<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Disadvantages<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Manual memory handling required<\/li>\n\n\n\n<li>Risk of memory leaks<\/li>\n\n\n\n<li>More complex than static memory<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why Dynamic Memory Allocation is Important<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">It is important because it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Allows efficient memory management<\/li>\n\n\n\n<li>Supports linked lists, trees, and graphs<\/li>\n\n\n\n<li>Helps build scalable applications<\/li>\n\n\n\n<li>Is essential for advanced programming<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Real-Life Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Think of renting rooms in a hotel:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Rooms are booked only when needed<\/li>\n\n\n\n<li>Unused rooms are released later<\/li>\n<\/ul>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1779446715398\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Dynamic memory works similarly by allocating memory only when required.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Dynamic memory allocation in C++ allows programmers to manage memory during runtime using <code>new<\/code> and <code>delete<\/code>. It provides flexibility, efficient memory usage, and support for advanced programming concepts and data structures.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/cpp\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Intermediate C++ > Pointers > Dynamic Memory Allocation<\/span><\/span><\/div>","protected":false},"menu_order":33,"template":"","class_list":["post-113","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Dynamic Memory Allocation - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C++ dynamic memory allocation using new and delete with examples of pointers, arrays, and efficient memory management techniques.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Dynamic Memory Allocation - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C++ dynamic memory allocation using new and delete with examples of pointers, arrays, and efficient memory management techniques.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn C++Language with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-22T10:44:34+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?lesson=dynamic-memory-allocation\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Dynamic Memory Allocation - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-21T04:37:51+00:00\",\"dateModified\":\"2026-05-22T10:44:34+00:00\",\"description\":\"Learn C++ dynamic memory allocation using new and delete with examples of pointers, arrays, and efficient memory management techniques.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/cpp\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Intermediate C++ > Pointers > Dynamic Memory Allocation\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/\",\"name\":\"Learn C++Language with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Dynamic Memory Allocation - Learn C++Language with GiGz.PK","description":"Learn C++ dynamic memory allocation using new and delete with examples of pointers, arrays, and efficient memory management techniques.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/","og_locale":"en_US","og_type":"article","og_title":"Dynamic Memory Allocation - Learn C++Language with GiGz.PK","og_description":"Learn C++ dynamic memory allocation using new and delete with examples of pointers, arrays, and efficient memory management techniques.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-22T10:44:34+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/cpp\/?lesson=dynamic-memory-allocation","url":"https:\/\/gigz.pk\/","name":"Dynamic Memory Allocation - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-21T04:37:51+00:00","dateModified":"2026-05-22T10:44:34+00:00","description":"Learn C++ dynamic memory allocation using new and delete with examples of pointers, arrays, and efficient memory management techniques.","breadcrumb":{"@id":"https:\/\/gigz.pk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/cpp"},{"@type":"ListItem","position":2,"name":"Intermediate C++ > Pointers > Dynamic Memory Allocation"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/cpp\/#website","url":"https:\/\/gigz.pk\/cpp\/","name":"Learn C++Language with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/cpp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson\/113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}