RAD Studio
ContentsIndex
PreviousUpNext
_heapset

Header File 

malloc.h 

Category 

Memory Routines 

Prototype 

int _heapset(unsigned int fillvalue); 

Description 

Fills the free blocks on the heap with a constant value. 

_heapset checks the heap for consistency using the same methods as _heapchk. It then fills each free block in the heap with the value contained in the least significant byte of fillvalue. This function can be used to find heap-related problems. It does not guarantee that subsequently allocated blocks will be filled with the specified value. 

Return Value 

One of the following values:

_HEAPOK 
The heap appears to be uncorrupted 
_HEAPEMPTY 
No heap exists 
_HEAPBADNODE 
A corrupted heap block has been found 

Example

#include <windowsx.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
BOOL InitApplication(HINSTANCE hInstance);
HWND InitInstance(HINSTANCE hInstance, int nCmdShow);
LRESULT FAR PASCAL _export MainWndProc(HWND hWnd, UINT message,
            WPARAM wParam, LPARAM lParam);
void ExampleHeapSet(HWND hWnd);            
#pragma argsused
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;      // message
  if (!InitApplication(hInstance))  // Initialize shared things
   return (FALSE); // Exits if unable to initialize
  /* Perform initializations that apply to a specific instance */
  if (!(InitInstance(hInstance, nCmdShow)))
   return (FALSE);
  /* Acquire and dispatch messages until a WM_QUIT message is received. */
  while (GetMessage(&msg, // message structure
   NULL, // handle of window receiving the message
   NULL, // lowest message to examine
   NULL))  // highest message to examine
  {
  TranslateMessage(&msg); // Translates virtual key codes
  DispatchMessage(&msg);  // Dispatches message to window
  }
  return (msg.wParam);  // Returns the value from PostQuitMessage
}
BOOL InitApplication(HINSTANCE hInstance)
{
  WNDCLASS  wc;
  // Fill in window class structure with parameters that describe the
  // main window.
  wc.style = CS_HREDRAW | CS_VREDRAW; // Class style(s).
  wc.lpfnWndProc = (long (FAR PASCAL*)(void *,unsigned int,unsigned int, long ))MainWndProc; // Function to retrieve messages for
                // windows of this class.
  wc.cbClsExtra = 0;  // No per-class extra data.
  wc.cbWndExtra = 0;  // No per-window extra data.
  wc.hInstance = hInstance; // Application that owns the class.
  wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  wc.lpszMenuName = NULL;  // Name of menu resource in .RC file.
  wc.lpszClassName = "Example"; // Name used in call to CreateWindow.
  /* Register the window class and return success/failure code. */
  return (RegisterClass(&wc));
}
HWND InitInstance(HINSTANCE hInstance, int nCmdShow)
{
  HWND  hWnd; // Main window handle.
  /* Create a main window for this application instance.  */
  hWnd = CreateWindow(
   "Example",                 // See RegisterClass() call.
   "Example _heapset 32 bit only",  // Text for window title bar.
   WS_OVERLAPPEDWINDOW,        // Window style.
   CW_USEDEFAULT,         // Default horizontal position.
   CW_USEDEFAULT,          // Default vertical position.
   CW_USEDEFAULT,            // Default width.
   CW_USEDEFAULT,        // Default height.
   NULL,                  // Overlapped windows have no parent.
   NULL,                  // Use the window class menu.
   hInstance,              // This instance owns this window.
   NULL                   // Pointer not needed.
  );
  /* If window could not be created, return "failure" */
  if (!hWnd)
   return (FALSE);
  /* Make the window visible; update its client area; and return "success" */
  ShowWindow(hWnd, nCmdShow); // Show the window
  UpdateWindow(hWnd);     // Sends WM_PAINT message
  return (hWnd);        // Returns the value from PostQuitMessage
}
void ExampleHeapSet(HWND hWnd)
{
  int hsts;
  char *buffer;
  if ( (buffer = (char *)malloc( 1 )) == NULL )
    exit(0);
  hsts = _heapset( 'Z' );
  switch (hsts)
  {
    case _HEAPOK:
      MessageBox(hWnd,"Heap is OK","Heap",MB_OK|MB_ICONINFORMATION);
      break;
    case _HEAPEMPTY:
      MessageBox(hWnd,"Heap is empty","Heap",MB_OK|MB_ICONINFORMATION);
      break;
    case _HEAPBADNODE:
      MessageBox(hWnd,"Bad node in heap","Heap",MB_OK|MB_ICONINFORMATION);
      break;
    default:
      break;
  }
  free (buffer);
}
#pragma argsused
LRESULT FAR PASCAL _export MainWndProc(HWND hWnd, UINT message,
            WPARAM wParam, LPARAM lParam)
{
  switch (message) {
    case WM_CREATE:
    {
      //Example _heapset
      ExampleHeapSet(hWnd);
      return NULL;
    }
    case WM_QUIT:
    case WM_DESTROY:  // message: window being destroyed
      PostQuitMessage(0);
    break;
    default:      // Passes it on if unprocessed
      return (DefWindowProc(hWnd, message, wParam, lParam));
  }
}

Portability

POSIX 
Win32 
ANSI C 
ANSI C++ 
 
 
 
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!