Table of Content

C allocmem() function : C Reference



Allocmem() : Allocates DOS memory segment in C [dos.h function]

  1. Allocates DOS memory segment
  2. Allocmem use the DOS system call 0x48 .
  3. It allocate a block of free memory
  4. It return the segment address of the allocated block.

Declaration :

int allocmem(unsigned size, unsigned *segp);

Parameters :

Parameter
What it does ?
size
The number of 16-byte paragraphs requested
segp
Pointer to a word that will be assigned the segment address of the newly allocated block

If not enough room is available,

  1. allocmem makes no assignment to the word *segp
  2. All allocated blocks are paragraph-aligned.

NOTE :  malloc can’t coexist with either allocmem or _dos_allocmem.

Return Value :

  1. On success, allocmem returns -1
  2. On error, allocmem returns the size of the largest available block and sets both _doserrno and errno to ENOMEM (Not enough memory)

Live Example :

#include <dos.h>
#include <alloc.h>
#include <stdio.h>

int main(void)
{
  unsigned int size, segp;
  int stat;

  size = 64;                    /* (64 x 16) = 1024 bytes */
  stat = allocmem(size, &segp);
  if (stat == -1)
    printf("Allocated memory at segment: %x\n", segp);
  else
    printf("Failed: maximum number of paragraphs available is %u\n", stat);

  return 0;
}