pointers - Which is better in C allocating/deallocating memory of using a local variable? and Why? -


if need string manipulation or manipulating kind of arrays stantard types int or self defined data structure. better local variable or dynamically allocating , de-allocating memory?

i know if using local variable not need allocate/de-allocate of memory , might save memory leaks. want know why people using dynamically allocate memory. coding style or have benefits. depends on system on compiling or depend on compiler?

even if system have enough resources memory , speed technique better suited optimize code?

local, stack-allocated operations pretty trump dynamic memory allocation in terms of speed. reason being that, during dynamic memory allocation, program needs ask operating system help. causes context switch (which slow/expensive), , blocks until operating system returns block of memory (which may not able to).

your program however, has own stack already, , can manipulate necessary without interrupting flow of execution (other multi-tasking out of control anyway).

the benefits dynamic memory allocation not know how need allocate until runtime. without dynamic allocation, need allocate static buffer upfront, , reserve enough memory worst-case scenario (which may or may not have enough resources for).


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -