473,748 Members | 4,065 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

malloc()

Hi Everyone,

I wanted to know as to how malloc() works, if my understanding is
correct, it is implementation specific of the vendor who provides the
library(alloc.h ).

If so, is there any standard as to how it should be implemented?

If there is no space in the RAM, will malloc() return NULL or will it
allocate a memory in a new page using virtual memory and return the
address? If so, what if the operating system doesn't support Virtual
memory?

Please share your comments, if possible, some links as to how
malloc() works?

Thanks in advance!!!

Apr 24 '07 #1
6 5492
sa*****@yahoo.c o.in wrote:
Hi Everyone,

I wanted to know as to how malloc() works, if my understanding is
correct, it is implementation specific of the vendor who provides the
library(alloc.h ).
If alloc.h were part of C, it would be a header, not a library.
The malloc, calloc, realloc, and free functions are declared in the
standard header <stdlib.h>. How any libraries are created or even named
is implementation-specific, but dollars-to-donuts they aren't named with
an ".h" extension.
Since platforms vary widely in the way they handle memory, the way in
which malloc operates is of necessity implementation-specific.
If so, is there any standard as to how it should be implemented?
There is not, and there cannot be, for quite obvious reasons.
If there is no space in the RAM, will malloc() return NULL or will it
allocate a memory in a new page using virtual memory and return the
address?
If malloc fails, then it returns NULL. What it means for malloc to fail
is, for obvious reasons, implementation-specific.
If so, what if the operating system doesn't support Virtual
memory?
If malloc requires something and that something isn't there, then malloc
fails, for obvious reasons.
Apr 24 '07 #2
sa*****@yahoo.c o.in wrote:
>
I wanted to know as to how malloc() works, if my understanding is
correct, it is implementation specific of the vendor who provides
the library(alloc.h ).

If so, is there any standard as to how it should be implemented?
Take a look at nmalloc (for DJGPP), at:

<http://cbfalconer.home .att.net/download/>

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline.net

--
Posted via a free Usenet account from http://www.teranews.com

Apr 24 '07 #3
CBFalconer <cb********@yah oo.comwrites:
sa*****@yahoo.c o.in wrote:
>I wanted to know as to how malloc() works, if my understanding is
correct, it is implementation specific of the vendor who provides
the library(alloc.h ).

If so, is there any standard as to how it should be implemented?

Take a look at nmalloc (for DJGPP), at:

<http://cbfalconer.home .att.net/download/>
That doesn't answer the OP's question. nmalloc illustrates one
possible way that malloc() can be implemented. The answer to the
original question is no.

--
Keith Thompson (The_Other_Keit h) 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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Apr 24 '07 #4
On Apr 23, 11:23 pm, sam_...@yahoo.c o.in wrote:
I wanted to know as to how malloc() works, if my understanding is
correct, it is implementation specific of the vendor who provides the
library(alloc.h ).
The header for these functions is stdlib.h . Some platforms include
an alloc.h, but this is redundant and irrelevant.
If so, is there any standard as to how it should be implemented?
The only standard is that it return either NULL or a pointer to memory
of sufficient storage capacity.
If there is no space in the RAM, will malloc() return NULL or will it
allocate a memory in a new page using virtual memory and return the
address? If so, what if the operating system doesn't support Virtual
memory?
If the system is unable to allocate the memory (due to any kind of
unavailability, for example) it must return NULL. In all other cases,
it really just depends on the quality of the implementation.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Apr 24 '07 #5
In article <11************ **********@o40g 2000prh.googleg roups.com>,
<sa*****@yahoo. co.inwrote:
If there is no space in the RAM, will malloc() return NULL or will it
allocate a memory in a new page using virtual memory and return the
address? If so, what if the operating system doesn't support Virtual
memory?
malloc() usually does not deal with virtual memory: instead, it
usually just tells the operating system that it needs more memory
and lets the operating system figure out how to put the memory
in the right place (such as by allocating another page of virtual
memory and mapping it in so that it appears to be contiguous
physical memory.)

There are notable variations: for example, some mallocs use
OS-specific calls to allocate shared memory segments, telling the
OS to map the shared memory into some convenient (not necessarily
contiguous) memory address. This kind of scheme requires more
knowledge of operating system facilities, but still does not
require any knowledge of how the OS actually finds available
physical memory and makes it usable to the program.

Any given implementation of malloc() could potentially have several
different methods for asking the underlying operating system for
more memory. If the operating system says "No" to all of the
methods, then as Sam indicated, the malloc() implementation must
return NULL.
--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
Apr 24 '07 #6

<sa*****@yahoo. co.inwrote in message
news:11******** **************@ o40g2000prh.goo glegroups.com.. .
Hi Everyone,

I wanted to know as to how malloc() works, if my understanding is
correct, it is implementation specific of the vendor who provides the
library(alloc.h ).

If so, is there any standard as to how it should be implemented?

If there is no space in the RAM, will malloc() return NULL or will it
allocate a memory in a new page using virtual memory and return the
address? If so, what if the operating system doesn't support Virtual
memory?

Please share your comments, if possible, some links as to how
malloc() works?

Thanks in advance!!!
You can implement a simple malloc youself.

unsigned char arena[1024 * 100]

void *mymalloc(size_ t N)
{
}

void myfree(void *ptr)
{
}

Now all you need to do is find some clever way of keeping track of blocks in
arena. Most systems require arbitary chunks of memory to be aligned to
double - alignment is actually something that can't be done portably, though
you can make it portable enough without too much difficulty.

The trick is to store the control information immediately before the pointer
you return. The myfree subtracts a bit, and reads off how to mark the block
as free again. There are of course other systems you can use.

Real memory allocation systems on modern hosted systems tend to be rather
complicated. Most systems have a scheme by which virtual memory addresses
are mapped to physical pages, so the user program sees a flat memory space,
the system sees a complicated set of mappings, often with some pages swapped
out to disk.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Apr 26 '07 #7

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

Similar topics

19
683
by: john smith | last post by:
Can someone please explain to me what is happening when I do a malloc(0). This is what I did. int* p = (int*)malloc(0); Then I printed the value of p and of course it was non-null. But has it allocated memory or what?
34
6440
by: Richard Hunt | last post by:
I'm sorry for asking such a silly question, but I can't quite get my head around malloc. Using gcc I have always programmed in a lax C/C++ hybrid (which I suppose is actually c++). But I have started messing around in Plan 9, and that sort of thing is totally no go there :). Is this correct to allocate memory for my struct? It works on my computer, but I'm suspicious that I'm doing it wrong. --
231
23198
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought (perhaps mistakenly) that the purpose of a void pointer was to cast into a legitimate date type. Is this wrong? Why, and what is considered to be correct form?
7
2215
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ http://www.eskimo.com/~scs/C-faq/faq.html but it doesn't seem to answer my questions... So, I've made an example behind, with some included questions...
20
10756
by: spasmous | last post by:
main() { float * f; initialize_f(f); // ...use f for processing free(f); }
15
2587
by: Martin Jørgensen | last post by:
Hi, I have a (bigger) program with about 15-30 malloc's in it (too big to post it here)... The last thing I tried today was to add yet another malloc **two_dimensional_data. But I found out that malloc always returned null at this moment and the program exited (even though if I malloc'ed only 20 bytes or something)... Then I googled for this problem and found something about a memory pool??? Is that standard C? I didn't understand it,...
68
15700
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting; some may find it off-topic or confusing. Disclaimers at end. The code samples are intended to be nearly minimal demonstrations. They are *not* related to any actual application code.
40
2599
by: Why Tea | last post by:
What happens to the pointer below? SomeStruct *p; p = malloc(100*sizeof(SomeStruct)); /* without a cast */ return((void *)(p+1)); /* will the returned pointer point to the 2nd struct? */ Seems to me there is no guarantee it will. /Why Tea
71
19114
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a problem? I cannot see why using malloc instead of new does not give the same result.
23
2729
by: raphfrk | last post by:
I am having an issue with malloc and gcc. Is there something wrong with my code or is this a compiler bug ? I am running this program: #include <stdio.h> #include <stdlib.h> typedef struct pxl { double lon, lat;
0
9534
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
9366
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...
1
9316
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8239
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
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
4597
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...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3303
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.