473,883 Members | 1,682 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static memory allocation versus dynamic memory allocation

Ken
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.

Nov 26 '06 #1
24 19113
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.
Nov 26 '06 #2

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

Nov 26 '06 #3
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.

Nov 26 '06 #4

"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:2v******** ************@bt .com...
Ken said:
>In C programming, I want to know in what situations we should use
static memory allocation instead of dynamic memory allocation. My
understandin g 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.
Nov 26 '06 #5


On Nov 26, 10:48 am, "Ken" <java...@gmail. comwrote:
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

Nov 26 '06 #6
"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 incomprehensibl es
as u and n?

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

Nov 26 '06 #7

"CBFalconer " <cb********@yah oo.comwrote in message
news:45******** *******@yahoo.c om...
"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 incomprehensibl es
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.
Nov 26 '06 #8
"Malcolm" <re*******@btin ternet.comwrite s:
"CBFalconer " <cb********@yah oo.comwrote in message
news:45******** *******@yahoo.c om...
>"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 incomprehensibl es
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.
Nov 26 '06 #9
Ben Bacarisse wrote:
"Malcolm" <re*******@btin ternet.comwrite s:
"CBFalconer " <cb********@yah oo.comwrote in message
news:45******** *******@yahoo.c om...
"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 incomprehensibl es
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.

Nov 27 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3417
by: meyousikmann | last post by:
I am having a little trouble with dynamic memory allocation. I am trying to read a text file and put the contents into a dynamic array. I know I can use vectors to make this easier, but it has to be done using dynamic arrays. I don't know the size of the text file ahead of time, though, so I created a class that includes a method to resize the array. Here is that class: class Data { public: Data(int initialsize);
6
8217
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory allocation to create object of that class ? 2. If it says "dynamic memory allocation", is it mean the following code : DestinationAddress* dest = new DestinationAddress(); // code 1
5
3771
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I believe the problem centers around my handling of strings, arrays, pointers and dynamic memory allocation. Here is the problem I'm trying to solve: I want to fill a list box with a list of Project Names from a database (in Palm this is more...
13
4731
by: xian_hong2046 | last post by:
Hello, I think dynamic memory allocation is supposed to be used when one doesn't know in advance how much memory to allocate until run time. An example from Thinking in C++ is to dynamically create an array (using "new") since one doesn't know it size when writing the program. However, it looks to me that the size information must come from somewhere at run time, and this information can be passed to array creation function as a...
1
7984
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this? Second Program: /* Best Method to allocate memory for 2D Array because it's ...
14
3843
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is this stored in stack. If yes whether it will be deleted on exiting from the function. is dynamic memory allocation needed for this purpose
2
1793
thatos
by: thatos | last post by:
I searched about this from the net and the only difference which I could get was that static memory allocation is determined by the compiler whereas dynamic memory allocation is determined during run-time. Can you please tell were can I get information about this like a specific website?
0
9933
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11114
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10835
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9563
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7960
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5787
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3230
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.