Chapter Contents

Previous

Next
hprealloc

hprealloc



Change the Size of an Allocated Memory Block in Heap


SYNOPSIS
DESCRIPTION
RETURN VALUE
ERRORS
DIAGNOSTICS
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <multheap.h>
void *hprealloc(_heap_id heapid, void *p, 
size_t size);


DESCRIPTION

hprealloc decreases or increases the size of a memory block previously allocated, possibly moving it to another location and heap, or to another heap. The new storage will be allocated from the heap specified by heapid. p points to the previously allocated memory block. size is the size of the new block. The contents of the old block are preserved in the new block after reallocation, unless the old size is greater than the new size. If the old size is greater, the unwanted extra bytes are lost. When the new size is larger than the old size, the contents of the new block that follow the data from the old block are unpredictable.


RETURN VALUE

hprealloc returns the address of the first character of the new block of memory. The allocated block is suitably aligned for storage of any type of data. If a new memory block cannot be allocated, the contents of the location that p points to are not changed, and hprealloc returns NULL.


ERRORS

User ABEND 1205 or 1206 may occur if memory management data areas are overlaid.


DIAGNOSTICS

If adequate memory is not available, the heapid is invalid, or if 0 bytes are requested, NULL is returned.


IMPLEMENTATION

See hpalloc for a description of normal memory allocation.


EXAMPLE

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <multheap.h>


char **table, **temp, *item;
unsigned table_size, max_elem;
_heap_id id1;
_heap_attr attr;

  hpattr(&attr);                                      
  attr.initial = 4000;                                
  id1 = hpcreate(&attr, "realloc heap");                      

   /* Determine if table size is too small.             */
if (max_elem >= table_size) {
   table_size *= 2;    /* Double table size.            */

      /* Allocate more space for table. */
   temp = hprealloc(id1, (char*)table, table_size*sizeof(char*));

      /* If reallocation is successful, copy address of */
      /*  new area to table.                            */
   if (temp)
      table = temp;
   else {
      puts("Item table overflow");
      exit(16);
   }
}


RELATED FUNCTIONS

hpalloc, hpcreate


SEE ALSO

"Memory Allocation Functions" in Chapter 2, "Function Categories" of the SAS/C Library Reference, Volume 1.


Chapter Contents

Previous

Next

Top of Page

Copyright © 2004 by SAS Institute Inc., Cary, NC, USA. All rights reserved.