By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,910 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,910 IT Pros & Developers. It's quick & easy.

static memory allocation versus dynamic memory allocation

P: n/a
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
Share this Question
Share on Google+
24 Replies


P: n/a
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

P: n/a

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

P: n/a
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

P: n/a

"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

P: n/a


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

P: n/a
"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

P: n/a

"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

P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a

"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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.