Confused about Dynamic Memory | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Confused about Dynamic Memory

Are there other resources that can help me understand dynamic memory and pointers?

15th Dec 2017, 3:47 AM
Dipti Muni
Dipti Muni - avatar
8 Answers
+ 6
I hope I can explain it , so it doesn't get too confusing. Dynamic memory allocation is where you control the lifetime of your variables . Local variables are stored on the stack. Think of variables in a stack as a stack of plates or a pile of plates one on top of the other. Variables that are pushed on the stack first are poped off the stack last , and variables pushed on the stack last are poped off the stack first (LIFO). Local variables go out of scope when a function is finished . But in Dynamic memory , variables go on the heap or free store , a area of memory not used by the program . The advantage of allocating memory from the heap is that the memory you reserve remains available until you release it. You allocate memory with the new operator in C++, C# and Java, and in C it's malloc (). Also in programs such as C and C++ , you are in charge of releasing memory . In C#, Java , and other programming languages, they take care of releasing memory for you.
15th Dec 2017, 7:14 AM
Rick Zalman
+ 6
Look at my answer here for a topical analogy related to pointers and values (memory concepts are only peripheral though). https://www.sololearn.com/Discuss/925671/?ref=app
15th Dec 2017, 4:49 AM
Kirk Schafer
Kirk Schafer - avatar
+ 5
I am glad you thought my answer helped you understand about Dynamic Memory.
16th Dec 2017, 5:16 AM
Rick Zalman
+ 3
Dynamic memory is for allocating variables to the Heap. That is done when variables are of unknown size and may get really huge. Pointers point to the Heap where the variable's address is. They take up very little space.
25th Dec 2017, 4:40 AM
Jeffrey Koh
Jeffrey Koh - avatar
+ 2
Thanks Rick that was very helpful
16th Dec 2017, 2:59 AM
Dipti Muni
Dipti Muni - avatar
+ 1
Which is the doubt that you dont understand? Maybe i can help you or other person.
15th Dec 2017, 3:53 AM
Iago
Iago - avatar
+ 1
I think I don't understand how it is used. Why do it? The reasoning? Example might help. @lago
15th Dec 2017, 4:02 AM
Dipti Muni
Dipti Muni - avatar
0
Try to read this pages, and if you continues with doubt, ask and we try to give you a good answer.
15th Dec 2017, 4:14 AM
Iago
Iago - avatar