Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: Memory Allo...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 11 Topic 26122 of 26972
Post > Topic >>

Re: Memory Allocation : Static or Dynamic?

by Antoninus Twink <nospam@[EMAIL PROTECTED] > May 8, 2008 at 07:48 PM

On  8 May 2008 at 16:58, swornavidhya.mahadevan@[EMAIL PROTECTED]
 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.
 




 11 Posts in Topic:
Memory Allocation : Static or Dynamic?
swornavidhya.mahadevan@[E  2008-05-08 09:58:37 
Re: Memory Allocation : Static or Dynamic?
Antoninus Twink <nospa  2008-05-08 19:48:06 
Re: Memory Allocation : Static or Dynamic?
roberson@[EMAIL PROTECTED  2008-05-08 18:37:36 
Re: Memory Allocation : Static or Dynamic?
Eric Sosman <Eric.Sosm  2008-05-08 15:05:47 
Re: Memory Allocation : Static or Dynamic?
pete <pfiland@[EMAIL P  2008-05-08 17:35:19 
Re: Memory Allocation : Static or Dynamic?
"Malcolm McLean"  2008-05-10 09:20:37 
Re: Memory Allocation : Static or Dynamic?
roberson@[EMAIL PROTECTED  2008-05-08 17:54:14 
Re: Memory Allocation : Static or Dynamic?
Antoninus Twink <nospa  2008-05-08 21:00:32 
Re: Memory Allocation : Static or Dynamic?
Antoninus Twink <nospa  2008-05-08 21:20:49 
Re: Memory Allocation : Static or Dynamic?
Antoninus Twink <nospa  2008-05-08 23:52:02 
Re: Memory Allocation : Static or Dynamic?
"cr88192" <c  2008-05-09 18:14:08 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 21:26:37 CDT 2008.