Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Sylvia Else Newsgroups: sci.electronics.design Subject: Re: Paper on dynamic memory allocation for real-time systems Date: Wed, 25 Oct 2017 11:55:37 +1100 Lines: 37 Message-ID: References: <%D4GB.50140$6V2.21096@fx21.iad> <85uqucp76e1m9j14ito1m9kqr5fnukkg2o@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net nIzU5skp/hUU4X202BSBGAUmjy4GouoRLE6R/+eNZQXdwaY6vF Cancel-Lock: sha1:HydADLXxEXw9Sylo7kTouau/VVg= User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 In-Reply-To: Content-Language: en-US Xref: csiph.com sci.electronics.design:482793 On 24/10/2017 8:12 PM, Tom Gardner wrote: > On 24/10/17 09:26, Sylvia Else wrote: >> On 23/10/2017 4:17 PM, upsidedown@downunder.com wrote: >> >>> I have often wondered, why C-programmers are so keen of using dynamic >>> memory allocation instead of stack allocation. Is it because some >>> early processors had a very limited stack, typically intended only for >>> return addresses ? >> >> Very often one's using dynamic memory exactly because one wants it to >> persist >> after the function returns. Otherwise, unless the memory requirement >> is large, >> or of unknown size (at time of programming), one uses an automatic >> variable >> stack allocated variable. > > I'm unconvinced. Persisting a variable after a function > returns can be satisfactorily achieved either by allocating > it in any of the calling functions "further up" the stack, > or failing that as a global. > > Dynamic memory is necessary when the number of such variables > cannot be determined at compile time. > A fairly typical scenario is parsing a hierarchical data stream which may something as relatively simple as XML, or may be the text of a program in a high-level language such as C. The number and nature of the elements cannot be determined in advance of the parsing (which tends to be recursive), so it is not practical to allocate them further up the stack. Sylvia.