+ 1

C++ when to use dynamic memory

i was saw some website like stackoverflow and dreamincode. they said: heap is more flexible and accessible. but, what kind of situation i should use dynamic memory?

7th Feb 2018, 5:15 AM
Kevin AS
Kevin AS - avatar
5 Answers
+ 3
Yes If you know at compile time how many memory space you'll need, then you can use variables and static arrays "Only", yes, but it is still a big part ^^
7th Feb 2018, 6:32 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 4
When you know how many space you need at runtime Often, you can delegate this use to objects like std::vector or std::list
7th Feb 2018, 6:07 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
Only in situations in which you have to do so, for deterministic memory- and/or process optimization. This is why reading source codes of libraries is important, so that you know how to optimize your code, and more importantly, when. Usually though, you only use dynamic memory allocation when dealing with multi-dimensional arrays or when transferring arrays to external scopes. You could get away with using vectors in C++, but you're not really losing anything with handling it with proper dynamic allocation, only gaining understanding of your own code. So, if you're comfortable with your knowledge on the logic, you don't really lose anything by implementing dynamic allocation, but you shouldn't touch it without the understanding.
7th Feb 2018, 7:04 AM
eRosz
eRosz - avatar
+ 1
only that?
7th Feb 2018, 6:18 AM
Kevin AS
Kevin AS - avatar
+ 1
okay, thanks :)
7th Feb 2018, 6:34 AM
Kevin AS
Kevin AS - avatar