473,569 Members | 2,870 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Memory Allocation for Dynamic Alloccation

Hi,

If the entire heap memory for dynamic allocation is not available,
does the compiler always return NULL ?

eg:

char *s;
s = (char *)malloc(...);

During execution, if the entire memory reserved for dynamic allocation
is not available, what would the OS return? Does it use ny mechanism
to still allocate the memory?

Rgds
Sandeep
Nov 14 '05 #1
6 2388
Sandeep Chikkerur <sa**********@y ahoo.com> scribbled the following:
Hi, If the entire heap memory for dynamic allocation is not available,
does the compiler always return NULL ?
What is "heap memory"? C knows of no such thing.
eg: char *s;
s = (char *)malloc(...);
Can't you afford stdlib.h?
During execution, if the entire memory reserved for dynamic allocation
is not available, what would the OS return? Does it use ny mechanism
to still allocate the memory?


If the OS can't allocate at least as much memory as you asked for in
the malloc() call, it must return NULL. It might use some mechanism to
allocate some other memory "behind the scenes", but it must still
return NULL from malloc(). Allocating other memory "behind the scenes"
would be a bit pointless (I'm sure Dan Pop will now tell me it
sometimes isn't), but the standard allows such behaviour.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The day Microsoft makes something that doesn't suck is probably the day they
start making vacuum cleaners."
- Ernst Jan Plugge
Nov 14 '05 #2
sa**********@ya hoo.com (Sandeep Chikkerur) wrote:
If the entire heap memory for dynamic allocation is not available,
does the compiler always return NULL ?
No. It returns a null pointer, not a compile-time constant.
char *s;
s = (char *)malloc(...);
You do not need the cast, and if you think you do, you probably forgot
some headers.
During execution, if the entire memory reserved for dynamic allocation
is not available, what would the OS return?
The OS has nothing to do with it. The C implementation uses _some_
mechanism to get your allocated memory for you. The Standard doesn't
specify what mechanism should be used, and if the implementation wants
to ask the OS for memory it can do so, but from the POV of the C
program, it simply gets some allocated memory, no matter whence.

And yes, when this allocated memory store is _entirely_ depleted,
malloc() must return a null pointer.
Does it use ny mechanism to still allocate the memory?


How can it? The _entire_ memory reserved for allocation is full; do you
know of any mechanism that allows a CPU to go to the store, buy some
memory chips or swap disks, and mount them inside its case on the fly?

Richard
Nov 14 '05 #3
>And yes, when this allocated memory store is _entirely_ depleted,
malloc() must return a null pointer.
Does it use ny mechanism to still allocate the memory?

What's a ny? A large city on the east coast of the USA?
How can it? The _entire_ memory reserved for allocation is full; do you
know of any mechanism that allows a CPU to go to the store, buy some
memory chips or swap disks, and mount them inside its case on the fly?


If the CPU can buy the memory on Ebay and pay to get it installed,
or buy/rent more disk space on a remote server and then swap/page
to it, then the available memory ISN'T entirely depleted, and
malloc() can return a pointer to part of it. This call to malloc()
may take a while to complete, though. So will any mechanism that
involves swapping to paper tape, punch cards, or stone tablets.
Gordon L. Burditt
Nov 14 '05 #4
Sandeep Chikkerur wrote:
Hi,

If the entire heap memory for dynamic allocation is not available,
does the compiler always return NULL ?


This is how it must behave according to the C standard. However, I am
told that Linux behaves differently, in a possibly non-conforming
manner: the memory is not actually allocated until you try to use it, at
which point there may no longer be free memory left. So you may get a
non-null pointer back from malloc() that crashes when you try to use it
under Linux and possibly other OSes.

[...]

-Peter

Nov 14 '05 #5
On Thu, 19 Feb 2004 10:21:27 GMT, rl*@hoekstra-uitgeverij.nl (Richard
Bos) wrote:
Does it use ny mechanism to still allocate the memory?


How can it? The _entire_ memory reserved for allocation is full; do you
know of any mechanism that allows a CPU to go to the store, buy some
memory chips or swap disks, and mount them inside its case on the fly?


I can imagine memory being sold the way larger multi-cpu systems are -
pay more and they use software to turn on more cpus.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 14 '05 #6

In article <c1**********@n ews.apple.com>, Peter Ammon <pe*********@ro cketmail.com> writes:

This is how it must behave according to the C standard. However, I am
told that Linux behaves differently, in a possibly non-conforming
manner: the memory is not actually allocated until you try to use it, at
which point there may no longer be free memory left.


There was a thread about lazy allocation here not long ago, and the
consensus appears to be that lazy allocation is a QoI issue (and a
debatable one at that), not a conformance one. See [1] (downthread
a ways). (I don't know about typical Linux implementations , but AIX,
for example, provides a lazy allocator by default, though there are
ways to select a strict allocator and require backing store for all
allocated memory.)

Consider that allocated memory may become unavailable after allocation,
even without lazy allocation, eg due to hardware failure in the portion
of the disk used for paging space in a virtual-memory OS. A non-null
return from malloc should not be interpreted as a guarantee from the OS
that the program will never run into difficulties due to accessing that
memory, just as a non-null return from fopen does not guarantee that
the program will always be able to perform I/O successfully on that
file.
1. http://groups.google.c om/groups?threadm= EATNb.826$xu6.1 13%40fe02.usene tserver.com&rnu m=1

--
Michael Wojcik mi************@ microfocus.com

Pocket #9: A complete "artificial glen" with rocks, and artificial moon,
and forester's station. Excellent for achieving the effect of the
sublime without going out-of-doors. -- Joe Green
Nov 14 '05 #7

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

Similar topics

18
6660
by: Tron Thomas | last post by:
Given the following information about memory management in C++: ----- The c-runtime dynamic memory manager (and most other commercial memory managers) has issues with fragmentation similar to a hard drive file system. Over time, the more often use call new/delete or alloc/free, there will be gaps and fragments in the heap. This can lead...
6
8186
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...
9
2769
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);
13
4694
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...
24
19045
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
7953
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...
3
2982
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”...
14
3815
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
10
4397
by: swornavidhya.mahadevan | last post by:
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
0
7693
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...
0
7605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7917
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. ...
1
7665
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...
0
7962
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...
0
6277
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...
0
5217
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...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
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

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.