_bheapseg

Description

Allocates a based heap.

#include <malloc.h> Required only for function declarations  

__segment _bheapseg( size_t size );

size Segment size to allocate  

Remarks

The _bheapseg function allocates a based-heap segment of at least size bytes. (The block may be larger than size bytes because of space required for alignment and for maintenance information.)

The value returned by _bheapseg is the identifier of the based-heap segment. This value should be saved and used in subsequent calls to other based-heap functions. If the original block of memory is depleted (e.g., by calls to _bmalloc and _brealloc), the run-time code will try to enlarge the heap as necessary.

The _bheapseg function can be called repeatedly. For each call, the run-time library will allocate a new based-heap segment.

Return Value

The _bheapseg function returns the newly allocated segment selector; save this value for use in subsequent based-heap functions. A return value of _NULLSEG indicates failure.

Always check the return from the _bheapseg function (especially when it is used in real mode), even if the amount of memory requested is small.

Compatibility

Standards:None

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:None

See Also

calloc functions, free functions, malloc functions, realloc functions

Example

/* BHEAPSEG.C: This program C illustrates dynamic allocation of based

* memory using functions _bheapseg, _bfreeseg, _bmalloc, and _bfree.

*/

#include <stdio.h>

#include <malloc.h>

#include <stdlib.h>

#include <string.h>

void main( void )

{

__segment seg;

char __based( seg ) *outstr, __based( seg ) *instr;

char __based( seg ) *pout, __based( seg ) *pin;

char tmpstr[80];

int len;

printf( "Enter a string: " );

gets( tmpstr );

/* Request a based heap. Use based so that memory won't be taken from

* near heap.

*/

if( (seg = _bheapseg( 1000 )) == _NULLSEG )

exit( 1 );

/* Allocate based memory for two strings. */

len = strlen( tmpstr );

if( ((instr = _bmalloc( seg, len + 1 )) == _NULLOFF) ||

((outstr = _bmalloc( seg, len + 1 )) == _NULLOFF) )

exit( 1 );

/* Copy a lowercased string to dynamic memory. The based memory is

* far when addressed as a whole.

*/

_fstrlwr( _fstrcpy( (char __far *)instr, (char __far *)tmpstr ) );

/* Copy input string to output string in reversed order. When reading

* and writing individual characters from a based heap, the compiler will

* try to process them as near, thus speeding up the processing.

*/

for( pin = instr + len - 1, pout = outstr;

pout < outstr + len; pin--, pout++ )

*pout = *pin;

*pout = '\0';

/* Display strings. Again, strings as a whole are far. */

printf( "Input: %Fs\n", (char __far *)instr );

printf( "Output: %Fs\n", (char __far *)outstr );

/* Free blocks and release based heap. */

_bfree( seg, instr );

_bfree( seg, outstr );

_bfreeseg( seg );

}

Output

Enter a string: Was I god

Input: was i god

Output: dog i saw