Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7
The base difference is that static allocation means allocating memory BEFORE the execution of the program using a STACK. While dynamic allocation means allocating memory DURING THE EXECUTION using a HEAP. Did you know that there is an actual hacking tehnique called DLL injection where a program allocates dynamic memory to another program and reuses the memory to inject a virus/hack etc.
23rd Dec 2019, 11:04 AM
🈂️Toma
🈂️Toma - avatar
+ 5
In static memory allocation, memory allocated only once at compile time. After that, alter the size of memory not supported. Arrays best example for static allocation of memory. In Dynamic memory, it is possible to change size. The memory allocated at run time, can be changed when ever needed.. In this, first we just initialize a varible with a initial size, latter we can change it when needed. Pointers are best example for this. It can be done by malloc, calloc, new, realloc methods.
23rd Dec 2019, 11:11 AM
Jayakrishna 🇮🇳
+ 2
I explain in simple language static is fixed size and dynamic is change its size at run time .
24th Dec 2019, 4:47 PM
Jashpal Singh Rajpurohit
Jashpal Singh Rajpurohit - avatar
+ 1
Remember, often allocating mamory in the stack is faster than in the heap, because you know the size at compile time, and you haven't to ask the os to allocate memory for you. Dynamic allocation has a big problem, that sometimes is useful : In c++, memory allocation is menaged explicity (there is no garbage collector like in Java or C#). This gives a large amount of power but dynamic allocated memory must be deallocated by the programmer (using free in or delete)
24th Dec 2019, 11:54 AM
CPPCodeGuru
+ 1
STATIC MEMORY ALLOCATION 1) memory size is fixed 2)it allocate the memory in STACK so we can rectify the problem..to introduce to concept called DYNAMIC MEMORY allocation DYNAMIC MEMORY ALLOCATION 1) memory size is not fixed...if u want memory we can extend the memory.. 2)it can allocate the memory in HEAP DMA ==> malloc,calloc,realloc,free 1)malloc (allocate memory to structure) 2)calloc (allocate memory to array) 3) realloc (we want increase and decrease using realloc) 4)free ( deallocate or delete the memory)
25th Dec 2019, 7:43 AM
Dinesh S
Dinesh S - avatar