+ 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?
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 ^^
+ 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
+ 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.
+ 1
only that?
+ 1
okay, thanks :)