> Online tutorial

realloc



 realloc<ALLOC.H>

   realloc reallocates main memory

 Declaration:
 
   void far *farrealloc(void far *oldblock, unsigned long nbytes);

 Remarks:

realloc adjusts the size of the allocated block to size, copying the
contents to a new location if necessary.


 Return Value:


   On success, both functions return the address of the reallocated block,
    which might be different than the address of the original block.
   On failure (if the block can't be reallocated, or if size == 0 for
    realloc), the functions return null.



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

int main(void)
{
   char *str;

   /* allocate memory for string */
   str = (char *) malloc(10);

   /* copy "Hello" into string */
   strcpy(str, "Hello");

   printf("String is %s\n  Address is %p\n", str, str);
   str = (char *) realloc(str, 20);
   printf("String is %s\n  New address is %p\n", str, str);

   /* free memory */
   free(str);

   return 0;
}

farrealloc



 farrealloc<ALLOC.H>
 
   farrealloc adjusts allocated block in far heap

 Declaration:


  void *realloc(void *block, size_t size);

 Remarks:


farrealloc adjusts the size of the allocated block to nbytes, copying the
contents to a new location, if necessary.

 Return Value:


   On success, both functions return the address of the reallocated block,
    which might be different than the address of the original block.
 
Program.


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

int main(void)
{
   char far *fptr;
   char far *newptr;

   fptr = (char far *) farmalloc(16);
   printf("First address: %Fp\n", fptr);

   /*  we use a second pointer, newptr, so that in the case of */
   /*  farrealloc() returning NULL, our original pointer is    */
   /*  not set to NULL.                                        */

   newptr = (char far *) farrealloc(fptr,64);
   printf("New address  : %Fp\n", newptr);
   if (newptr != NULL)
      farfree(newptr);
   return 0;
}



farcoreleft



 farcoreleft<ALLOC.H>


  farcoreleft returns a measure of unused memory in the far heap

 Declaration:


    All except tiny models:          unsigned long farcoreleft(void);

 Remarks:

farcoreleft returns a measure of the amount of unused memory in the far heap
beyond the highest allocated block.

A tiny model program can't use farcoreleft.

 Return Value:

 farcoreleft: returns the total amount of space left in the far heap,
    between the highest allocated block and the end of available memory.

coreleft



 coreleft<ALLOC.H>

   coreleft returns a measure of unused memory


 Declaration:


   Tiny, small, and medium models:  unsigned coreleft(void);
  Compact, large, and huge models: unsigned long coreleft(void);

 Remarks:


coreleft returns a measure of RAM memory not in use.

The value coreleft gives depends on whether the memory model is of the small
data group or the large data group.

 Return Value:


   coreleft:
      Small data models: returns the amount of unused memory between the
       top of the heap and the stack.
      Large data models: returns the amount of memory between the highest
       allocated block and the end of available memory.

farmalloc() in c



 farmalloc()
 Allocates from far heap

 Declaration:

 void far *farmalloc(unsigned long nbytes);

 Remarks:

farmalloc allocates a block of memory nbytes bytes long from the far heap.



 Return Value:

  On success, farmalloc returns a pointer
    to the newly allocated block
   On failure (not enough space exists for
    the new block), farmalloc returns null




 Example:

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

 int main(void)
 {
    char far *fptr;
    char *str = "Hello";

    /* allocate memory for the far pointer */
    fptr = (char far *) farmalloc(10);

    /* copy "Hello" into allocated memory */
    /*
       Note: movedata is used because we might be in a small data model,
       in which case a normal string copy routine can not be used since it
       assumes the pointer size is near.
    */
    movedata(FP_SEG(str), FP_OFF(str),
             FP_SEG(fptr), FP_OFF(fptr),
             strlen(str) + 1);

    /* display string (note the F modifier)
*/
    printf("Far string is: %Fs\n", fptr);

    /* free the memory */
    farfree(fptr);

    return 0;
 }