Library function used in turbo C for dynamic memory management
HOME
At the core of C’s dynamic alocation system are the functions malloc ( ) and free( ) , which are part of the standard C library. Each time a malloc( ) memory request is made, a portiion of the remaining free memory is allocated.each time a free( ) memory release call is made , memory is returned to the system.
The proposed ANSI standards defines only for functions for the dynamic allocation system : calloc ( ) , malloc( ) , free( ) and realloc( ). However turbo C containt several other dynamic allocation function some of these additional functions are neccessary to support the segment architecture of the 8086 familyof processor
The proposed ANSI standard specifies that the header information neccessary to the dynamic allocation fuctions defined by the standard willbe in stdlib.h .turboC lets you use either stdlib.h or alloc.h . this guide uses stdlib.h because it is portable . some of the other turbo C dynamic allocation function require the header alloc.h , and two of them require dos.h header. You should pay special attention to which header file is used with which function.
The final point: some of turbo C’s allocation functiions allocate memory from the far heap which lies outside the programs default data segment . this provides for two very imp. Features
All the RAM in the system can be allocated - not just that within the data segment.
Blocks of memory larger then 64 KB can be allocated.
Memory in the far heap must be accessed with far pointers .
1. Int allocmem( unsigned size, unsigned * seg)
Description
The prototype for allocmem( ) is in dos.h . the allocmem ( ) function executes a dos 0x48 function called to allocate a para graph - aligned block of memory. It puts the segment add. Of the block into the unsigned integer pointed to by seg. The size argument specifies the no. Of paragraphs to be allocated . ( a paragraph is 16 bytes)
If the requested mem. Can be allocated , a - 1 is returned. If insufficient free memory exists , no assignment is made to the unsigned integer pointed to by seg. And the size of trhe largest available block is returned .
Example :
The fragments allocates 100 paragraphs of memory:
Unsigned i;
i= 0 ;
if ( i = (allocmem(100,&i)) == -1) printf (“ alloc is successful”);
else
print(“allocatioin unsuccessful”);
2. Int brk(void *eds)
Description
The prototype for bkr( ) is in alloc.h .
The brk ( ) function dynamically change the amont of memory for use by the data segment . if succesful, the end of data segment is eds and 0 is returned if unsuccessful , -1 is returned and errno. Is said to ENOMEM ( unsufficient memory).
Becuse the application of brk( ) is highly specialised, no example is presented here.
3. Void * calloc (unsigned no. Unsigned size)
Description
The protoype for calloc ( ) is in stdlib.h.
The calloc( ) function returns a pointer to the allocated memory. The amount of memory allocated is equal to num* size where size is in bytes i. e . calloc( ) allocates sufficient memory for an array of num objects of size size.
The calloc( ) function returns a pointer to thef first byte of the allocated reason .if there is not enough memory to satisfy the request, a null pointer is returned. It always important to varify that rhe returned value is not a null pointer before attempting to use the pointer .
4. Unsigned coreleft( void );/ *smallspace data more models */
Unsigned long core left( void ); /* large data models*/
Description
The prototype for coreleft( ) is in alloc.h .
The core left ( ) function returns the no. Of bytes of unused memory left on the heap. For programs compiled using a small memory model , the functions returned a unsigned integer. For programs compiled using a larger data model core left( ) returns an unsigned lons integer.
5. Void far *farcalloc( unsigned long num, unsigned long size)
Description
The prototype for calloc ( ) is in alloc.h .
The far calloc( ) function is the same as calloc ( ) except the memory is allocated from outside the current data segment by using the far heap .
6. long farcoreleft( void )
Description
The prototype for farcoreleft( ) is in alloc.h.
The function farcoreleft( ) returns the no. Of bytes of free memory left in the in the far heap.
Example:
# include “ alloc.h”
main ( )
{
printf(“ farheapfree memory : ld”, farcoreleft( ));
}
7. void farfree( void far *ptr)
Description
The prototype for farfree( ) is in alloc.h.
The function farfree( )is used to release memory allocated from the heap via a call to farmalloc ( ) or farcalloc ( ).
You must use great care to call farfree ( ) only with a valid pointer into the far heap. Doing otherwise will corrupt the far heap. You cannot free a far heap pointer with the free ( ) function or a regular heap pointer with farfree ( ).
Example :
# include < alloc.h>
main ( )
{
char far *p;
p = farmalloc(100);
if (p) farfree(p);
}
8. void far *farmalloc ( unsigned long size)
Description
The prototype for farmalloc( ) is in alloc.h .
The farmalloc ( ) function returns a pointer into the far heap that is the first byte in a region of memory size bytes long . it is the same as malloc( ) except that the far heap is used instead of the heap within the default data segment .
9. void far *farrealloc ( void far *ptr, unsigned long newsize)
Description
The prototype for farrealloc( ) is in alloc.h
The farrealloc( ) function resizes the block of memory previously
From the far heap and pointed to by ptr to the new size specified in the newsize . it is functionally equivalent to realloc ( ) except that it operates on the far heap instead of the heap within the default data segment.
10. Void free ( void *ptr)
Description
The prototype for free ( ) is in stdlib.h
The free ( ) function returns the memory pointed to by ptr back to the heap. This makes the memory available for future use.
It is imperative that free ( ) be called only with a pointer that was previosly allocated by using one of the dynamic allocation system’s functions, such as malloc ( ) or calloc( ) . using an invalid pointer in the call will most likely destroy the memory - management mechanism and cause asystem crash.
11. Int freemem( unsigned seg )
Description
The prototype for freemem ( ) function is in DOS.H
The freemem( ) function frees the block of memory whose first byte is at seg. This memory must have been previously allocated using allocmem ( ).
The function returns 0 on success . on failure , it returns -1 .
Example :
Unsigned i;
If ( allocmem ( some , &i) != -1 )
Printf ( “ allocation error “);
Else
Freemem( i);
12. void * malloc ( unsigned size)
Description
The prototype for malloc( ) is in stdlib.h
The malloc ( ) function returns a pointer to the first byte of a region of memory of size size that has been allocated from the heap . if there is in sufficient memory in the heap to satisfy the request. Malloc( ) returns a null pointer. It is always important to verify that the return value is not a null pointer before
Attempting to use the pointer . attempting to use anull pointer usually causes a system crash.
13. void *realloc ( void *ptr, unsigned newsize)
Description
The prototype for realloc( ) is in stdlib.h.
The realloc( ) function changes the size of the allocated memory pointed to by ptr to that specified newsize . The value of newsize can be greater or less than the original.A pointer to the memory block is returned because it may be necessary for realloc( ) to move the block to increase its size . Ifthis occurs,the contents of the old block are copied in to the new block and no information is lost.
If there is not enough free memory in the heap to allocate newsize bytes,a null
pointer is returned and the original block is freed (lost).Thus it is important
to verifythe success of a call to realloc( ).
14. Char*sbrk( int amount )
Description
The prototype for sbrk ( )is in alloc.h.
The sbrk( ) function increments (or decrements,if a negative value is used )the amount of memory allocated to the data segmant by amount number of bytes .
Because the use of sbrk ( ) is highly specialized,no example is given .
Related function
Brk( )
15. int setblock (int seg ,int size )
Description
The prototype for set block ( ) is in dos.h.
The setblock ( ) function changes the size of the block of memory whose segmant address is seg.The size is specified in paragraphs (16 bytes ) .The block of memory must have been previously allocated using allocmem( ).
If the size adjustment cannot be made .setblock ( ) returnsthe largest block that can be allocated .On success, it returns -1.
Example
If ( setblock(seg,100!=-1) printf (“resize error”);