Memory management in C
C course. I guess I understand the memory management part. You allocate memory on the heap only? When something is to big for the stack? What would be a REAL LIFE SITUATION where I would need to do this? I feel like it’s very important and I have not proceeded with the course because I’m not understanding this too much. Is it ok if I keep on with the course even if I do not understand this section so much? Can I still learn and practice in C even if I don’t understand this part too much, at least for now?
1/23/2022 2:19:56 AMJermainne Espinoza
15 AnswersNew Answer
an example of using the heap memory: You want the user to enter the array elements, you don't know how many elements will be in the array, (it could be 5 or maybe 50 elements) you will create the array with malloc, with the size the user will specify at the run time, then you will loop and take from the user the array elements one at a time.
Not real life situation, and this is just my opinion. Use dynamic memory management when the data is presumably also dynamic e.g. may grow or shrink, or when the data is supposedly transferable out of current scope e.g. it can be passed as argument for, or returned to other functions. Because local scoped data (stack based data) are usually non transferable, although can be returned. Don't worry too much now, if you overthink it, you might end up getting stuck on that topic. Continue learning, so when you're done you can always revisit the topic for better understanding 👍
Jermainne Espinoza I would not recommend learning Python to help you get disciplined in memory management. Python largely removes you from most concerns about memory usage. In Python it is easy to use up large swaths of memory if you are reckless, and indeed you should guard against that. But you will learn to be more cognizant of handling memory concerns by studying C. And knowing C will help you be aware of what Python is doing behind the scenes.
Thank you so much for the responses. Well, i am an absolute beginner and it is bothering me that i am not understanding this topic fully. I was on the Python course before, did not complete it because i wanted to do the C course. I feel like i can do well on the Python course, i was understanding things. Would it be a good idea for me to go back to the python course, finish it amd practice with it so that i get the basic fundamentals of programming and at the same time i study specifically about the memory management in C on the side?
Of course you can jump to Python again, it's totally your choice, no one is supposed to dictate how your journey goes. We can always come back to any tutorial for revision. One thing though, the ease (rather spoil) Python offer *might* somehow decrease your interest in learning C memory management, cause in Python such complexities are encapsulated : )
Thank you, just one last question. I know that python abstracts all these things. But is it possible that it touches on it slightly, so that maybe i would understand it a little better if i am learning it through Python, and in return i could have at least a little better understanding about it for when i am studying the C memory management?
Jermainne Espinoza if you mix your learning between C and Python, here is advice to heed. Some concepts in Python resemble concepts in C, but be careful not to assume one works just like the other. In particular, boolean expression valuation is different, and index iteration in for loops is different.
Don't know Python enough to say, but AFAIK the internal works are less camouflaged in C than it is in Python.
At every language there are difficulties, for example the OOP in Python is not easy especially at Sololearn. If you really want to learn anything you can do it. if a topic is hard you can try some different resources. Also you do not always need to understand everything so deeply if you are a beginner. If you want to learn some basics of programming, you can do it with C or Python or almost any language. If you are interested in Python and C you can learn them, just be aware of their differences. example: In Python the index -1 will give you the last element of the list. In C index -1 is out of your array.
Thank you, do you think if i practice in python, i could learn maybe a little bit about memory management through python? I know that python takes care of all that stuff on its own but will i maybe be able to get a better idea about memory management using python?
Python will not help you much in understanding C, especially when it comes to memory management. when I started to learn C, Python was confusing me, I was making many errors because I am used to Python. https://youtu.be/zuegQmMdy8M This pointers course at freeCodeCamp. If you understand pointers well, the C language becomes so easy.
Thank you for rhe responses. I feel like i kind of understand memory management in C. What i dont understand is when i would use this. What would be some examples of when i would need to use memory management? Any feedback would be greatly appreciated, thank you
So, is python not i good idea for me? I really wanted to learn C but the memory management thing really through me off course😔then i thought, maybe it would be a good idea for me to practice on python to get my fundamentals down and start building things, getting practice, and then when i am comfortable with python, maybe i could revisit C and maybe then it will be A LITTLE EASIER FOR ME TO GRASP THE MEMORY MANAGEMENT🤷