Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > static memory allocation versus dynamic memory allocation

Reply
Thread Tools

static memory allocation versus dynamic memory allocation

 
 
Ken
Guest
Posts: n/a
 
      11-26-2006
In C programming, I want to know in what situations we should use
static memory allocation instead of dynamic memory allocation. My
understanding is that static memory allocation like using array is
faster than malloc, but dynamic memory allocation is more flexible.

Please comment... thanks.

 
Reply With Quote
 
 
 
 
Tom St Denis
Guest
Posts: n/a
 
      11-26-2006

Ken wrote:
> In C programming, I want to know in what situations we should use
> static memory allocation instead of dynamic memory allocation. My
> understanding is that static memory allocation like using array is
> faster than malloc, but dynamic memory allocation is more flexible.


Ho, Ho, Ho, Hold the homework!

Tom

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      11-26-2006
Ken said:

> In C programming, I want to know in what situations we should use
> static memory allocation instead of dynamic memory allocation. My
> understanding is that static memory allocation like using array is
> faster than malloc, but dynamic memory allocation is more flexible.


That's about right. Broadly, if you know in advance how much memory you
need, you don't need to use malloc (unless your implementation has a very
low limit on the amount of memory available for auto objects, but a less
restrictive limit on free store).

But malloc & co give you the flexibility to grow your data structures only
as large as they need to be, and (most importantly) the decision is
deferred until runtime (which is when you actually find out how much data
you've got!).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      11-26-2006
Ken wrote:
> In C programming, I want to know in what situations we should use
> static memory allocation instead of dynamic memory allocation. My
> understanding is that static memory allocation like using array is
> faster than malloc, but dynamic memory allocation is more flexible.


Use static allocation when you know in advance the amount of memory
needed, or if dynamic allocation is not available, (as may happen in
some embedded systems). Use of auto variables, (a form of static
allocation, though in reality it may not be), also helps in data
encapsulation and automatic memory management.

Dynamic memory is particularly relevant when you anticipate growth in
your data structures, where you may only know the maximum needed
memory, but actual usage may be far less than that, and when you need
for the data to persist throughout the process's lifetime but you
don't, (rightly), want to use global memory for the purpose.

 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      11-26-2006



"Richard Heathfield" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Ken said:
>
>> In C programming, I want to know in what situations we should use
>> static memory allocation instead of dynamic memory allocation. My
>> understanding is that static memory allocation like using array is
>> faster than malloc, but dynamic memory allocation is more flexible.

>
> That's about right. Broadly, if you know in advance how much memory you
> need, you don't need to use malloc (unless your implementation has a very
> low limit on the amount of memory available for auto objects, but a less
> restrictive limit on free store).
>
> But malloc & co give you the flexibility to grow your data structures only
> as large as they need to be, and (most importantly) the decision is
> deferred until runtime (which is when you actually find out how much data
> you've got!).
>

The other factor is that malloc() makes modules much more resuable.
Often you will know the size of the object at complie time, but not when you
write the code that handles arbitrary objects. For instance, a hash table
could hold any number of objects of any size, even though you know that you
need at most 1000 of type CAR.
--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.


 
Reply With Quote
 
rhle.freak
Guest
Posts: n/a
 
      11-26-2006


On Nov 26, 10:48 am, "Ken" <(E-Mail Removed)> wrote:
> In C programming, I want to know in what situations we should use
> static memory allocation instead of dynamic memory allocation. My
> understanding is that static memory allocation like using array is
> faster than malloc, but dynamic memory allocation is more flexible.
>
> Please comment... thanks.


Static Memory Allocation is used when u know the memory requirement in
advance ;memory is assigned by the compiler at runtime . The space is
allocated once, when your program is started (part of the exec
operation), and is never freed.
Static allocation is what happens when you declare a static or global
variable. Each static or global variable defines one block of space, of
a fixed size.

Dynamic Memory Allocation is used when the memory you need, or how
long you continue to need it, depends on factors that are not known
before the program runs or if u working with a dynamic data structure
..Memory is allocated at runtime rather than during compilation n u can
free a block of memory once u no longer need it during the execution of
the program.

further Dynamic allocation is not supported by C variables; there is no
storage class "dynamic", and there can never be a C variable whose
value is stored in dynamically allocated space. The only way to get
dynamically allocated memory is via a system call and the only way to
refer to dynamically allocated space is through a pointer.
the actual process of dynamic allocation requires more computation time
n hence its slower than static memory allocation

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      11-26-2006
"rhle.freak" wrote:
>

.... snip ...
>
> Dynamic Memory Allocation is used when the memory you need, or
> how long you continue to need it, depends on factors that are not
> known before the program runs or if u working with a dynamic data
> structure .Memory is allocated at runtime rather than during
> compilation n u can free a block of memory once u no longer need
> it during the execution of the program.


You appear to be fairly capable of using English. Why spoil your
articles and make them hard to read by using such incomprehensibles
as u and n?

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      11-26-2006



"CBFalconer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "rhle.freak" wrote:
>>

> ... snip ...
>>
>> Dynamic Memory Allocation is used when the memory you need, or
>> how long you continue to need it, depends on factors that are not
>> known before the program runs or if u working with a dynamic data
>> structure .Memory is allocated at runtime rather than during
>> compilation n u can free a block of memory once u no longer need
>> it during the execution of the program.

>
> You appear to be fairly capable of using English. Why spoil your
> articles and make them hard to read by using such incomprehensibles
> as u and n?
>

Or nonsense words like "int", "float" and "char" for
"integer", "real number", and "character"?
--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.


 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      11-26-2006
"Malcolm" <(E-Mail Removed)> writes:

> "CBFalconer" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> "rhle.freak" wrote:
>>>

>> ... snip ...
>>>
>>> Dynamic Memory Allocation is used when the memory you need, or
>>> how long you continue to need it, depends on factors that are not
>>> known before the program runs or if u working with a dynamic data
>>> structure .Memory is allocated at runtime rather than during
>>> compilation n u can free a block of memory once u no longer need
>>> it during the execution of the program.

>>
>> You appear to be fairly capable of using English. Why spoil your
>> articles and make them hard to read by using such incomprehensibles
>> as u and n?
>>

> Or nonsense words like "int", "float" and "char" for
> "integer", "real number", and "character"?


Why would you even consider removing such important distinctions in a
C new group? Did you forget the smiley?

--
Ben.
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      11-27-2006
Ben Bacarisse wrote:
> "Malcolm" <(E-Mail Removed)> writes:
>
> > "CBFalconer" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> >> "rhle.freak" wrote:
> >>>
> >> ... snip ...
> >>>
> >>> Dynamic Memory Allocation is used when the memory you need, or
> >>> how long you continue to need it, depends on factors that are not
> >>> known before the program runs or if u working with a dynamic data
> >>> structure .Memory is allocated at runtime rather than during
> >>> compilation n u can free a block of memory once u no longer need
> >>> it during the execution of the program.
> >>
> >> You appear to be fairly capable of using English. Why spoil your
> >> articles and make them hard to read by using such incomprehensibles
> >> as u and n?
> >>

> > Or nonsense words like "int", "float" and "char" for
> > "integer", "real number", and "character"?

>
> Why would you even consider removing such important distinctions in a
> C new group? Did you forget the smiley?


No distinction is removed. He's probably just pointing out that full
names like 'integer' would have been a better design choice than terse
abbreviations like int, float etc. Too late now and anyway, Pascal is
always available.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic memory allocation with static pointer variable Shivanand Kadwadkar C Programming 7 01-04-2011 07:16 AM
Memory Allocation : Static or Dynamic? swornavidhya.mahadevan@gmail.com C Programming 7 05-09-2008 08:14 AM
Re: Mozilla versus IE versus Opera versus Safari Peter Potamus the Purple Hippo Firefox 0 05-08-2008 12:56 PM
equal? versus eql? versus == versus === verus <=> Paul Butcher Ruby 12 11-28-2007 06:06 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM



Advertisments