Doubt in static vs dynamic

what is the difference between static array and dynamic array.

And what is a static variable or is there any concept of dynamic variable if yes then what is it ?

When do we use static and when do we use dynamic ?

Hey @yashsharma4304

Actually they are not static or dynamic array ,its a way to allocate array i.e static allocation of array or dynamic allocation of array
Static arrays are allocated memory at compile time and the memory is allocated on the stack. Whereas, the dynamic arrays are allocated memory at the runtime and the memory is allocated from heap.
Stack have limited amount of memory but heaps have all the memory ur system can offer.

This is a different concept than previous one .
A static variable can be thought of as a variable that is allocated in memory for the lifetime of during program execution. Even if it is used in a function that is called several times, the same memory space will be utilized for the static variable. The calculated value of this variable will be carried to the next function call too. The concept is useful for applications where the previous active state of a function is required.
I never heard of dynamic variable unless we are considering it as a variable which is dynamically allocated.
You will learn about static variable in oops lectures.

mem
I am taking this in pretext of my first answer
We allocate statically when we know the amount of memory required before execution of the program

And we use dynamic allocation when we we don’t know the size of array or how much memory we will require

1 Like

Ok thank you for your help. :slightly_smiling_face:

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.