There are a lot of application of dynamic memory allocation. In this article, I’m going to give a brief intuition so that, you may understand intuitively why dynamic memory allocation is important & fascinating in the programming world.

#### Let’s take a look at the following code

```#include <iostream>
using namespace std;

int *ArrMul(int a[], int b[], int size) {

int *c = new int[size];

for (int i = 0; i < size; i++) {
c[i] = a[i] * b[i];
}

return c;
}

void printArray(int *arg, int length) {
for (int i = 0; i < length; i++) {
cout << *(arg + i) << endl;
}
}

int main()
{
int firstArray = { 5, 10, 15, 20, 25 };
int secondArray = { 2, 4, 6, 8, 10 };

int *z = NULL;

z = ArrMul(firstArray, secondArray, 5);

printArray(z, 5);

return 0;
}
```

Output:

```10
40
90
160
250```

I’m assuming you know the basic concept of function call & able to play with the array. That’s why I’m not explaining the code. If you still having problems to understand the code please let me know in the comment. I will try my best to make you understand.

In the following program look at line 6. In this line, you can see that I’ve allocated memory dynamically.

If I wrote the syntax of line 6 as:

`int c[size];`

What do you think the program would print the exact same result?

No

#### Why?

Because when a function has executed, the variable (data) within the function is erased (deallocated) automatically & freed to used by other programs. So when the array is called without dynamic memory allocation this will give garbage value. Because by the time the address of C array is returned it’s deallocated. And the memory block reserved by this array is no longer holding its value in memory (RAM). So there is a higher potential that the address will hold another program value or garbage value without being the array allocated dynamically in the program.

But when I allocated the c array memory dynamically the value of array was still reserved in the memory after the function had executed & was able to assign in the main function pointer variable exactly.
{"ft_lang_label":"__label__en","ft_lang_prob":0.85996103,"math_prob":0.8793603,"size":2355,"snap":"2019-51-2020-05","text_gpt3_token_len":567,"char_repetition_ratio":0.13228413,"word_repetition_ratio":0.018779343,"special_character_ratio":0.26539278,"punctuation_ratio":0.122621566,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97822016,"pos_list":[0,1,2],"im_url_duplicate_count":[null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-12-14T14:40:42Z\",\"WARC-Record-ID\":\"<urn:uuid:398266f9-c617-4782-ad5d-1cef24ad7f4e>\",\"Content-Length\":\"107962\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:dac12b02-61c6-4d9f-81a6-a56570f4c395>\",\"WARC-Concurrent-To\":\"<urn:uuid:34d7a902-bab0-4b8c-9dc2-8b13561df209>\",\"WARC-IP-Address\":\"104.223.62.200\",\"WARC-Target-URI\":\"https://heapneverflow.com/a-brief-intuition-about-dynamic-memory-allocation/\",\"WARC-Payload-Digest\":\"sha1:D7NC3P74R5LGQMBK66NU2WWDHOCYBXS4\",\"WARC-Block-Digest\":\"sha1:LQMKPUUU4L4Z27PUEFPIMY2V3DQXDHFC\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-51/CC-MAIN-2019-51_segments_1575541157498.50_warc_CC-MAIN-20191214122253-20191214150253-00260.warc.gz\"}"}