allocmem, _dos_allocmem
Allocates DOS memory segment.
Defined in header <dos.h>
Syntax
#include <dos.h>
int allocmem(unsigned size, unsigned *segp);
unsigned _dos_allocmem(unsigned size, unsigned *segp);
Portability
| DOS | UNIX | Windows | ANSI C | C++ only |
|---|---|---|---|---|
| ■ |
Remarks
allocmem and _clos_allocmem use the DOS system call 0x48 to allocate a block of free memory and return the segment address of the allocated block.
size is the desired size in paragraphs (a paragraph is 16 bytes), segp is a pointer to a word that will be assigned the segment address of the newly allocated block.
For allocmem, if not enough room is available, no assignment is made to the word pointed to by segp.
For _dos_allocmem, if not enough room is available, the size of the largest available block will be stored in the word pointed to by segp.
All allocated blocks are paragraph-aligned.
| allocmem and _dos_allocmem | cannot coexist with malloc. |
Return value
allocmem returns -1 on success. In the event of error, allocmem returns a number indicating the size in paragraphs of the largest available block.
| _dos_allocmem | returns 0 on success. In the event of error, |
| _dos_allocmem | returns the DOS error code and sets the word pointed to |
by segp to the size in paragraphs of the largest available block.
| An error return from allocmem or _dos_allocmem | sets the global variable |
_doserrno and sets the global variable errno to
| ENOMEM | Not enough memory |
See also
Example
#include <dos.h>
#include <stdio.h>
int main(void)
{
unsigned int segp, maxb;
unsigned int size = 64; /* (64*16) = 1024 bytes V
int largest;
/* Use _dos_allocmem, _dos_setblock, and _dos_freemem. */
if (_dos_allocmem(size, &segp) == 0)
printf ("Allocated memory at segment: %x\n", segp);
else {
perror("Unable to allocate block.");
printf ("Maximum no. of paragraphs"
" available is %u\n", segp);
return 1;
}
if (_dos_setblock(size * 2, segp, &maxb) == 0)
printf ("Grew memory block at segment: %X\n", segp);
else {
perror("Unable to grow block.");
printf ("Maximum number of paragraphs"
" available is %u\n", maxb) ;
}
_dos_freemem(segp) ;
/* Use allocmem, setblock, and freemem. */
if ((largest = allocmem(size, &segp)) == -1)
printf ("Allocated memory at segment: %x\n", segp);
else {
perror("Unable to allocate block.");
printf("Maximum number of paragraphs"
" available is %u\n", largest);
return 1;
}
if ((largest = setblock(segp, size * 2)) == -1)
printf("Grew memory block at segment: %X\n", segp);
else {
perror("Unable to grow block.");
printf("Maximum number of paragraphs"
" available is %u\n", largest);
}
freemem(segp);
return 0;
Differences from modern implementations
Nothing recorded yet.
Modern references
These are search links, not yet checked by hand.
Extraction notes
- heading `allocmem, dos allocmem` read as `allocmem, _dos_allocmem`
Stable link: /3.1/dos.h/allocmem/
· short form /3.1/allocmem/