473,837 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Memory Allocation : Static or Dynamic?

Which allocation (Static / Dynamic) is suitable for the situation when
we are trying to allocate for a overloaded memory when the memory is
full and no space to allocate.
What will happen if both the allocation is impossible.

Sworna vidhya
Jun 27 '08 #1
10 4434
On 8 May 2008 at 16:58, sw************* *******@gmail.c om wrote:
Which allocation (Static / Dynamic) is suitable for the situation when
we are trying to allocate for a overloaded memory when the memory is
full and no space to allocate.
What will happen if both the allocation is impossible.
Definitely use dynamic allocation if there's a serious risk of running
out of memory (and as a general principle, don't allocate large arrays
on the stack). If malloc() fails, you can detect this (it will return a
null pointer) and attempt to recover or exit cleanly. If you run out of
static memory for your automatic variables, the most likely outcome is
that the heap will become corrupted without warning. If you're lucky,
your program will quickly segfault; if your unlucky, you could go on for
hours processing corrupted data.

What's going on is that in your process's virtual address space, memory
is arranged like this:

-----------------------
| |
| STACK |
| (grows downwards) |
| |
-----------------------
| |
| FREE MEMORY |
| (available for the |
| stack or heap) |
| |
-----------------------
| |
| HEAP |
| (grows upwards) |
| |
-----------------------

So if you run out of free memory and overflow the stack, you can see
that bad things will happen.

Jun 27 '08 #2
In article <4f************ *************** *******@u6g2000 prc.googlegroup s.com>,
<sw************ ********@gmail. comwrote:
>Which allocation (Static / Dynamic) is suitable for the situation when
we are trying to allocate for a overloaded memory when the memory is
full and no space to allocate.
What will happen if both the allocation is impossible.
Sounds like a homework question to me...

But anyhow: The failure of dynamic allocation has well-defined
results: all of the dynamic allocation functions have methods
of indicating that they were unable to allocate more memory. This
allows you to cleanly detect and take action when memory is full.
Static memory has no defined method of indicating failure of
allocation, so you cannot cleanly detect and take action in
such cases.

If there is a failure of static memory allocation, then *some*
systems will generate a signal that can be handled through
the signal handling mechanism, but C89 at least does not define
a signal for this condition and does not document it as a
possibility, so any such signal would be an OS extension, not part of C
itself. And in C89, if the system itself raises any of the signals
defined specifically in C89, then behaviour of the program
after returning from the signal handler is undefined -- all of
the C89- defined signals are for conditions that are typically
fatal on most operating systems. (Note: if the program itself
used raise() to raise one of the C89- defined signals, then
returning from the signal handler -is- well defined.)

I would have to look up the details of the signals that C99 defines
to see if there are any of them for which returning from the signal
handler is well defined. C99 differs slightly from C89 in that
there are operations (such as overflow of a signed integer) that C89
just leaves the behaviour completely undefined, but for which C99
says that the behaviour is undefined or that the system may optionally
raise an implementation-defined signal. This is where the language
lawyers make their money, arguing about the difference between
behaviour that is -always- undefined, vs behaviour that the
implementation may define as being undefined!
--
"All human knowledge takes the form of interpretation. "
-- Walter Benjamin
Jun 27 '08 #3
In article <sl************ *******@nospam. invalid>,
Antoninus Twink <no****@nospam. invalidwrote:
>What's going on is that in your process's virtual address space, memory
is arranged like this:
>-----------------------
| |
| STACK |
| (grows downwards) |
| |
-----------------------
| |
| FREE MEMORY |
| (available for the |
| stack or heap) |
| |
-----------------------
| |
| HEAP |
| (grows upwards) |
| |
-----------------------
>So if you run out of free memory and overflow the stack, you can see
that bad things will happen.

You truly are an amazing mind-reader! I don't know how you do it!!
*Knowing* with certainty that the original poster is now and will
always be using a system on which the stack grows downwards
and the heap grows upwards.

It doesn't happen that way in the system I'm using right now (a
general-purpose system that at one time was the market leader in
several different computing niches), and it doesn't happen that way
when you start dealing with threads; and it isn't unusual for it not to
happen that way once you start considering where -exactly- shared
libraries get mapped into memory...

--
"It's a hard life sometimes and the biggest temptation is to let
how hard it is be an excuse to weaken." -- Walter Dean Myers
Jun 27 '08 #4
On 8 May 2008 at 18:37, Walter Roberson wrote:
It doesn't happen that way in the system I'm using right now (a
general-purpose system that at one time was the market leader in
several different computing niches), and it doesn't happen that way
when you start dealing with threads; and it isn't unusual for it not to
happen that way once you start considering where -exactly- shared
libraries get mapped into memory...
Nonetheless, as a schematic picture to have in mind, it's very valuable.

Jun 27 '08 #5
Walter Roberson wrote:
In article <sl************ *******@nospam. invalid>,
Antoninus Twink <no****@nospam. invalidwrote:
>[...]
So if you run out of free memory and overflow the stack, you can see
that bad things will happen.


You truly are an amazing mind-reader! I don't know how you do it!!
*Knowing* with certainty that the original poster is now and will
always be using a system on which the stack grows downwards
and the heap grows upwards.
Not only that, he's discovered that static memory
is allocated on the stack!

--
Er*********@sun .com
Jun 27 '08 #6
On 8 May 2008 at 19:05, Eric Sosman wrote:
Not only that, he's discovered that static memory
is allocated on the stack!
Ahem, yeah, good spot... Maybe it's just because static and stack sound
alike, but certainly I read into the OP's question that he was
contrasting dynamic and automatic allocation.

Jun 27 '08 #7
Antoninus Twink wrote:
If you run out of
static memory for your automatic variables,
ITYM "static memory for your static variables,"
the most likely outcome is
that the heap will become corrupted without warning.
--
pete
Jun 27 '08 #8
On 8 May 2008 at 21:35, pete wrote:
Antoninus Twink wrote:
>If you run out of
static memory for your automatic variables,

ITYM "static memory for your static variables,"
ITIM "stack memory for your automatic variables"... it's been a long day
:)

Jun 27 '08 #9

<sw************ ********@gmail. comwrote in message
news:4f******** *************** ***********@u6g 2000prc.googleg roups.com...
Which allocation (Static / Dynamic) is suitable for the situation when
we are trying to allocate for a overloaded memory when the memory is
full and no space to allocate.
What will happen if both the allocation is impossible.
static memory is good for fixed-memory buffers (it will never need to be
freed or made any larger).

however, note that static memory is also allocated as a part of the app's
initial process image, so it will either be available, or the app will fail
somehow early in app startup (like, an app that as soon as you try to start
it, either the OS refuses to load it, it crashes, or the OS crashes).

also note that, because of the fixed nature of static memory, what is
located there can't be used by elsewhere (it is like putting a big crate in
ones' house... one can put things in the crate, but otherwise this space is
unusable, and would one rather have a house full of empty space, or filled
with odd-sized crates?...).

one is either limited by all these odd-sized crates, or all of these crates
form a big horrible mess...
otherwise, dynamic memory is a lot more adaptable, and I would recommend
this as a general rule unless one has some specific reason to use large
static buffers.

after all, if dynamic memory were not useful, why would it have been
implemented and used in the first place, when static memory is so much
simpler?...

Sworna vidhya

Jun 27 '08 #10

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

Similar topics

6
3293
by: J Anderson | last post by:
Greetings, I was going through the quake2 source code (as you do) when I noticed arrays are statically allocated on the stack instead of being allocated dynamically. Iíve seen this before and know itís for efficiency. Iím basically questioning the merit of such technique nowadays, for performance-orientated applications? Is it better to dynamically allocate a smaller array to fit the memory or to use one huge statically allocated...
9
2815
by: Andrew Au | last post by:
Dear all, I am trying to write a piece of software that use Object Oriented design and implement it with C, I did the following == In Object.h == typedef struct ObjectStructure* Object; Object object_create(); int object_getIntegerAttribute(Object object);
5
3765
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...
7
2811
by: Michael | last post by:
Hi, What's the benefit to dynamically allocate memory? using namespace std; int main() { char* ptr; ptr="abc";
24
19107
by: Ken | last post by:
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.
1
7982
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 ...
3
3001
by: ranjeetasharma81 | last post by:
Hi all, I have a big C-cod, in which there are lots of dynamic memory allocation used. I want to replace dynamic memroy allocation by static arrays. The following are the problems that i am facing: 1- From structure and dynamic memory allocation point of view, the code is very complicated. The code has various ďnested structuresĒ with a number of levels. The size of memory allocated for pointer to structure or its pointer...
14
3842
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
0
1344
by: abir | last post by:
I have a big dynamic sparse directed graph implementation where new nodes are added & old nodes are removed. Usually, at any moment of time the number of nodes in the graph and the number of edges are approximately constant. The nodes and associated data are stored in a quasi-static dynamic memory (quasi_buffer) which reserves a minimum memory, but on demand can grow upto an user defined maximum memory. The no of nodes in the graph are...
0
9837
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
10870
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...
0
10563
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10267
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6998
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5668
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...
1
4474
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
2
4038
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3123
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.