473,399 Members | 4,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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 19020
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.invalidwrote 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
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.
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 incomprehensibles
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********@yahoo.comwrote in message
news:45***************@yahoo.com...
"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.
Nov 26 '06 #8
"Malcolm" <re*******@btinternet.comwrites:
"CBFalconer" <cb********@yahoo.comwrote in message
news:45***************@yahoo.com...
>"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.
Nov 26 '06 #9
Ben Bacarisse wrote:
"Malcolm" <re*******@btinternet.comwrites:
"CBFalconer" <cb********@yahoo.comwrote in message
news:45***************@yahoo.com...
"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.

Nov 27 '06 #10
"santosh" <sa*********@gmail.comwrites:
Ben Bacarisse wrote:
>"Malcolm" <re*******@btinternet.comwrites:
"CBFalconer" <cb********@yahoo.comwrote in message
news:45***************@yahoo.com...
<original snipped>
>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.
Oh, OK. Because the reply was to a comment about *posting* style
(using u and n) I though Malcolm was suggesting is was equally silly
to use words like int and char in posting in this group! Not to self:
sleep more, post less.

--
Ben.
Nov 27 '06 #11

"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
"santosh" <sa*********@gmail.comwrites:
>Ben Bacarisse wrote:
>>"Malcolm" <re*******@btinternet.comwrites:

"CBFalconer" <cb********@yahoo.comwrote in message
news:45***************@yahoo.com...
<original snipped>
>>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.

Oh, OK. Because the reply was to a comment about *posting* style
(using u and n) I though Malcolm was suggesting is was equally silly
to use words like int and char in posting in this group! Not to self:
sleep more, post less.
If you attack "u" for "you" on what basis do you defend "int" for
"integer?".
--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.
Nov 28 '06 #12
"Malcolm" <re*******@btinternet.comwrites:
If you attack "u" for "you" on what basis do you defend "int" for
"integer?".
Which one of them is defined in the C standard?
--
"You call this a *C* question? What the hell are you smoking?" --Kaz
Nov 28 '06 #13
Ben Pfaff <bl*@cs.stanford.eduwrites:
"Malcolm" <re*******@btinternet.comwrites:
>If you attack "u" for "you" on what basis do you defend "int" for
"integer?".

Which one of them is defined in the C standard?
Both are. There are several integer types; "int" is one of them. See
C99 6.2.5.

Malcolm appears to be of the opinion that there should be one and
only one integer type. He is, as far as I can tell, in a minority of
one in this regard.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 28 '06 #14
Keith Thompson <ks***@mib.orgwrites:
Ben Pfaff <bl*@cs.stanford.eduwrites:
>"Malcolm" <re*******@btinternet.comwrites:
>>If you attack "u" for "you" on what basis do you defend "int" for
"integer?".

Which one of them is defined in the C standard?

Both are. There are several integer types; "int" is one of them. See
C99 6.2.5.
My point is that whereas "u" is *not* defined in the C standard,
"int" *is*, so it is a proper term for use in comp.lang.c.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 28 '06 #15
Ben Pfaff <bl*@cs.stanford.eduwrites:
Keith Thompson <ks***@mib.orgwrites:
>Ben Pfaff <bl*@cs.stanford.eduwrites:
>>"Malcolm" <re*******@btinternet.comwrites:

If you attack "u" for "you" on what basis do you defend "int" for
"integer?".

Which one of them is defined in the C standard?

Both are. There are several integer types; "int" is one of them. See
C99 6.2.5.

My point is that whereas "u" is *not* defined in the C standard,
"int" *is*, so it is a proper term for use in comp.lang.c.
Of course, I agree completely. I was merely quibbling about the way
you expressed the point. We do that here, you know. 8-)}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 28 '06 #16
On Wed, 29 Nov 2006 00:01:59 GMT, Keith Thompson <ks***@mib.orgwrote:
>Ben Pfaff <bl*@cs.stanford.eduwrites:
>Keith Thompson <ks***@mib.orgwrites:
>>Ben Pfaff <bl*@cs.stanford.eduwrites:
"Malcolm" <re*******@btinternet.comwrites:

If you attack "u" for "you" on what basis do you defend "int" for
"integer?".

Which one of them is defined in the C standard?

Both are. There are several integer types; "int" is one of them. See
C99 6.2.5.

My point is that whereas "u" is *not* defined in the C standard,
"int" *is*, so it is a proper term for use in comp.lang.c.

Of course, I agree completely. I was merely quibbling about the way
you expressed the point. We do that here, you know. 8-)}
Oh well, as well as we are going to quibble, the "both" should refer to
"u" and "int", the issue being parallelism of usage. OTOH, this being
comp.lang.c the governing rule is that if it can be misread, then it
should be. :-)
Nov 28 '06 #17
"Malcolm" <re*******@btinternet.comwrites:
"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
>"santosh" <sa*********@gmail.comwrites:
>>Ben Bacarisse wrote:
"Malcolm" <re*******@btinternet.comwrites:

"CBFalconer" <cb********@yahoo.comwrote in message
news:45***************@yahoo.com...
<original snipped>
>>>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.

Oh, OK. Because the reply was to a comment about *posting* style
(using u and n) I though Malcolm was suggesting is was equally silly
to use words like int and char in posting in this group! Not to self:
sleep more, post less.
If you attack "u" for "you" on what basis do you defend "int" for
"integer?".
On the basis that it is the keyword for an important type in the
language discussed here. Can you discuss C without using the keywords
that designate the core types of the language?

I though I had misunderstood you comment but it seem I had not.

(BTW, I don't attack using "u" for "you". I find it does not impede
my understanding and it probably gives useful "out of band" data about
the poster.)

--
Ben.
Nov 29 '06 #18
Keith Thompson wrote:
Ben Pfaff <bl*@cs.stanford.eduwrites:
.... snip ...
>>
My point is that whereas "u" is *not* defined in the C standard,
"int" *is*, so it is a proper term for use in comp.lang.c.

Of course, I agree completely. I was merely quibbling about the
way you expressed the point. We do that here, you know. 8-)}
Quibble? Here? I am shocked, shocked I tell you.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Nov 29 '06 #19
CBFalconer <cb********@yahoo.comwrites:
Keith Thompson wrote:
>Ben Pfaff <bl*@cs.stanford.eduwrites:
... snip ...
>>>
My point is that whereas "u" is *not* defined in the C standard,
"int" *is*, so it is a proper term for use in comp.lang.c.

Of course, I agree completely. I was merely quibbling about the
way you expressed the point. We do that here, you know. 8-)}

Quibble? Here? I am shocked, shocked I tell you.
"Your winnings, sir."

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 29 '06 #20
CBFalconer wrote:
Keith Thompson wrote:
>Ben Pfaff <bl*@cs.stanford.eduwrites:
... snip ...
>>>
My point is that whereas "u" is *not* defined in the C standard,
"int" *is*, so it is a proper term for use in comp.lang.c.

Of course, I agree completely. I was merely quibbling about the
way you expressed the point. We do that here, you know. 8-)}

Quibble? Here? I am shocked, shocked I tell you.
All right, all right, who's been sending CB the results of their UB?

--
Chris "Magenta - the best colour of sound" Dollin
"Never ask that question!" Ambassador Kosh, /Babylon 5/

Nov 29 '06 #21
Ben Pfaff schrieb:
"Malcolm" <re*******@btinternet.comwrites:

>>If you attack "u" for "you" on what basis do you defend "int" for
"integer?".


Which one of them is defined in the C standard?
N1124 has 20084 [Uu]'s
Nov 29 '06 #22
On Wed, 29 Nov 2006 00:36:31 +0000, Ben Bacarisse
<be********@bsb.me.ukwrote:
>(BTW, I don't attack using "u" for "you". I find it does not impede
my understanding and it probably gives useful "out of band" data about
the poster.)
Such nonsense doesn't affect my ultimate understanding, since I can
usually figure out what the writer meant. However, it does *impede*
understanding, just as bad sentence structure, misspelling, and use of
incorrect words does. What makes the kiddy speak particularly
irritating is that it's done deliberately.

I agree that it does provide further data about the user. However,
pointing it out can lead to even more important information - their
educability.

--
Al Balmer
Sun City, AZ
Nov 29 '06 #23
Ben Bacarisse wrote:
>
.... snip ...
>
(BTW, I don't attack using "u" for "you". I find it does not
impede my understanding and it probably gives useful "out of band"
data about the poster.)
That's because you are a native English speaker. Now consider the
usenetter who has no idea of how English is pronounced, nor how the
various letters are pronounced (in English). He is basically
decoding an arbitrary set of glyphs.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Nov 29 '06 #24
CBFalconer <cb********@yahoo.comwrites:
Ben Bacarisse wrote:
>>
... snip ...
>>
(BTW, I don't attack using "u" for "you". I find it does not
impede my understanding and it probably gives useful "out of band"
data about the poster.)

That's because you are a native English speaker.
I don't think so. I think it is because I am mildly dyslexic and, for
me, a set of letters that suggests a word is almost as good as the
work itself. Other native speakers report such contractions as
impeding understanding and I have no reason to doubt them
Now consider the
usenetter who has no idea of how English is pronounced, nor how the
various letters are pronounced (in English). He is basically
decoding an arbitrary set of glyphs.
Indeed. I am surprised at how few non-native English speakers
complain about such usage, though. They certainly should complain if
a poster makes his or her post harder to follow than it need be.

I don't like the usage. I don't want to encourage it. I don't object
when anyone else complains about it. I was making a personal point
that it does not seem to impede my understanding.

--
Ben.
} ((C, "yoda")invented) if
Nov 30 '06 #25

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

Similar topics

5
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...
6
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...
5
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...
13
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...
1
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...
14
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.