malloc()

S

sam_cit

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!!!
 
M

Martin Ambuhl

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.
 
W

websnarf

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.
 
W

Walter Roberson

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.
 
M

Malcolm McLean

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top