Dynamic memory allocation

what is recommended ,allocating memory at run time or just take an array of maximum possible size.what are the advantages of one over other if any ?

In coding competitions, when you have to optimize your code mainly in time, and not in space, then even taking the array of max possible size doesn’t matter much, but that said, it is a good practice to allocate memory at run time, according to me. Opinions may vary.

1 Like

is there any difference between the max memory for Dynamic memory allocation and static memory allocation?

taking array of maximum possible size may not be feasible in all cases… typical example being dynamic programming…

at the same time it is also not advisable to use memory operations like malloc/calloc/realloc/free very frequently… the reason being that, these involve system/OS calls that are sometimes time consuming…

what is typically done in application development is that a self-memory-manager is put in place wherein it replicates the behavior of the system/OS. when the application starts, it starts with a medium size memory chunk managed on it own… this gradually increases over the session of the application, but the net interaction with system/OS is reduced…

well, this is also true that the above practice is not used in coding competitions though… in such circumstances, you have to breakeven between static and dynamic memory as per situation…

1 Like

obviously yes! the available memory in static allocation is always less than what you can get in dynamic allocation… please read more about ‘stack memory’ and ‘heap memory’ with respect to programming on the internet…

1 Like

well said ! malloc/calloc involve system/OS calls but as for as the coding competitions are concerned most of the time we are dealing with multiple test cases and that’s why at the starting of every test case we’ll have to initialize the static array, that would also consume some time isn’t it ?