全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1223
推到 Plurk!
推到 Facebook!

Windows Safe allocate memory VC ++

 
AB
高階會員


發表:166
回覆:262
積分:125
註冊:2003-08-21

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-12-01 23:13:54 IP:61.64.xxx.xxx 未訂閱
Windows Safe allocate memory     http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2134&lngWId=3  
      This function is used to allocate memory in Windows. It increases the size of a pointer (using realloc()) without damaging its contents. It includes (from what I can tell) all the required error checking for memory allocation    INCLUDE files:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//     
//INCLUDE files for :Windows Safe alloca
//     te memory
//**************************************
//     
It includes MALLOC.H, STDLIB.H, and WINDOWS.H. It really only needs MALLOC.H, I'm just used to
SDTLIB.H from my early days. WINDOWS.H is used just for the message box function that
outputs error messages.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!     
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.      //**************************************
//     
// Name: Windows Safe allocate memory
// Description:This function is used to 
//     allocate memory in Windows. It increases
//     the size of a pointer (using realloc()) 
//     without damaging its contents. It includ
//     es (from what I can tell) all the requir
//     ed error checking for memory allocation.
//     
// By: Jason Beighel
//
// Inputs:void *WinAddMem(void *Pointer,
//     size_t Amount)
The first parameter is the pointer to be
adjusted, if this parameter does not have any memory allocated to it it must be equal to NULL.
Amount of memory to add to the pointer, usually found with the sizeof() macro, this is only the
memory to add to the pointer not the new size. for instance if the pointer already holding 2
bytes and you want to increase it to 4 bytes then this parameter should be 2, not 4 like it would
be in realloc(). this function determines the amount of memory already assigned to the pointer
and addes this value to it, so you don't need to keep track of that value yourself, incidently
this is why the pointer must equal NULL if it does not have memory assigned to it, from my
experience with it that is the only sure fire method to crash this function.
//
// Returns:It returns the new pointer as
//     type void *, so a typecast is needed to 
//     switch the pointer to the form you are r
//     eally using.
//
// Assumes:Its helpful to have a basic u
//     nderstanding of pointers. Most of the bo
//     okkeeping and error checking for the all
//     ocation process is taken care of for you
//     here (which is the intent) so that can b
//     e ignored.
//
// Side Effects:I don't know of any side
//     effects, but its possibly they exist. Pl
//     ease let me know if you find any. This f
//     unction does not keep track of the memor
//     y and it still must be free() -ed as wit
//     h all other allocated memory.
//
//This code is copyrighted and has// limited warranties.Please see http://
//     www.Planet-Source-Code.com/vb/scripts/Sh
//     owCode.asp?txtCodeId=2134&lngWId=3//for details.//**************************************
//         // Includes ----------------------------
//     ---------------------------------------
#include
#include
#include
// Defines -----------------------------
//     ---------------------------------------
#define WIN_ADDMEM_VERID                "0.01.001"
#define WIN_ADDMEM_ERRPTRLOST        "001"
#define WIN_ADDMEM_ERR_NOADD        "002"
// Structures --------------------------
//     ---------------------------------------
// Classes -----------------------------
//     ---------------------------------------
// Prototypes --------------------------
//     ---------------------------------------
void *WinAddMem(void *Pointer, size_t Amount);
// Globals -----------------------------
//     ---------------------------------------
// Functions ---------------------------
//     ---------------------------------------
void *WinAddMem(void *Pointer, size_t Amount)                {
            size_t OldSize, NewSize;
            if ((Pointer == NULL)&&(Amount == NULL))                            {
                        if (Output != NULL)                                        {
                                    char VerStr[500];
                                    sprintf(VerStr, "Windows Safe Memory Allocation Function\n Ver: %s", WIN_ADDMEM_VERID);
                                    MessageBox(NULL, VerStr, "Version", NULL);
                                    }
                            return NULL;
                            }
                    if (Pointer != NULL)                                    {
                                OldSize = _msize((void *) Pointer);        
                                Pointer = (void *)realloc((void *) Pointer, OldSize + Amount);
                                if (Pointer == NULL)                                                {
                                            if (Output != NULL)                                                            {
                                                        char ErrStr[500];
                                                        sprintf(ErrStr, "\nERROR %s in SafeAddMem()\n Failed to allocate additional memory.", WIN_ADDMEM_ERRPTRLOST);
                                                        MessageBox(NULL, ErrStr, "Error!", NULL);
                                                        }
                                                return NULL;
                                                }
                                        NewSize = _msize((void *) Pointer);
                                        if (OldSize >= NewSize)                                                        {
                                                    if (Output != NULL)                                                                    {
                                                                char ErrStr[500];
                                                                sprintf(ErrStr,"\nERROR %s in SafeAddMem()\n Failed to allocate additional memory.", WIN_ADDMEM_ERR_NOADD);
                                                                MessageBox(NULL, ErrStr, "Error!", NULL);
                                                                }
                                                        return NULL;
                                                        }
                                                }
                                        else                                                        {
                                                    OldSize = 0;
                                                    Pointer = (void *)malloc(Amount);
                                                    if (Pointer == NULL)                                                                    {
                                                                if (Output != NULL)                                                                                {
                                                                            char ErrStr[500];
                                                                            sprintf(ErrStr, "\nERROR %s in SafeAddMem()\n Failed to allocate additional memory.", WIN_ADDMEM_ERRPTRLOST);
                                                                            MessageBox(NULL, ErrStr, "Error!", NULL);
                                                                            }
                                                                    return NULL;
                                                                    }
                                                            
                                                            NewSize = _msize((void *) Pointer);
                                                            if (OldSize >= NewSize)                                                                            {
                                                                        if (Output != NULL)                                                                                        {
                                                                                    char ErrStr[500];
                                                                                    sprintf(ErrStr, "\nERROR %s in SafeAddMem()\n Failed to allocate additional memory.", WIN_ADDMEM_ERR_NOADD);
                                                                                    MessageBox(NULL, ErrStr, "Error!", NULL);
                                                                                    }
                                                                            return NULL;
                                                                            }
                                                                    }
                                                            return Pointer;
                                                            }          
mieng
中階會員


發表:12
回覆:97
積分:81
註冊:2003-10-31

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-12-01 23:54:59 IP:61.221.xxx.xxx 未訂閱
只是幫忙重整程式碼  
#include
#include
#include
// Defines -----------------------------
//     ---------------------------------------
#define WIN_ADDMEM_VERID                "0.01.001"
#define WIN_ADDMEM_ERRPTRLOST        "001"
#define WIN_ADDMEM_ERR_NOADD        "002"
// Structures --------------------------
//     ---------------------------------------
// Classes -----------------------------
//     ---------------------------------------
// Prototypes --------------------------
//     ---------------------------------------
void *WinAddMem(void *Pointer, size_t Amount);
// Globals -----------------------------
//     ---------------------------------------
// Functions ---------------------------
//     ---------------------------------------
void *WinAddMem(void *Pointer, size_t Amount)
{
  size_t OldSize, NewSize;
  if ((Pointer == NULL)&&(Amount == NULL))
  {
    if (Output != NULL)
    {
      char VerStr[500];
      sprintf(VerStr, "Windows Safe Memory Allocation Function\n Ver: %s", WIN_ADDMEM_VERID);
      MessageBox(NULL, VerStr, "Version", NULL);
    }
    return NULL;
  }
  if (Pointer != NULL)
  {
    OldSize = _msize((void *) Pointer);
    Pointer = (void *)realloc((void *) Pointer, OldSize   Amount);
    if (Pointer == NULL)
    {
      if (Output != NULL)
      {
        char ErrStr[500];
        sprintf(ErrStr, "\nERROR %s in SafeAddMem()\n Failed to allocate additional memory.", WIN_ADDMEM_ERRPTRLOST);
        MessageBox(NULL, ErrStr, "Error!", NULL);
      }
      return NULL;
    }
    NewSize = _msize((void *) Pointer);
    if (OldSize >= NewSize)
    {
      if (Output != NULL)
      {
        char ErrStr[500];
        sprintf(ErrStr,"\nERROR %s in SafeAddMem()\n Failed to allocate additional memory.", WIN_ADDMEM_ERR_NOADD);
        MessageBox(NULL, ErrStr, "Error!", NULL);
      }
      return NULL;
    }
  }
  else
  {
    OldSize = 0;
    Pointer = (void *)malloc(Amount);
    if (Pointer == NULL)
    {
      if (Output != NULL)
      {
        char ErrStr[500];
        sprintf(ErrStr, "\nERROR %s in SafeAddMem()\n Failed to allocate additional memory.", WIN_ADDMEM_ERRPTRLOST);
        MessageBox(NULL, ErrStr, "Error!", NULL);
      }
      return NULL;
    }
    NewSize = _msize((void *) Pointer);
    if (OldSize >= NewSize)
    {
      if (Output != NULL)
      {
        char ErrStr[500];
        sprintf(ErrStr, "\nERROR %s in SafeAddMem()\n Failed to allocate additional memory.", WIN_ADDMEM_ERR_NOADD);
        MessageBox(NULL, ErrStr, "Error!", NULL);
      }
      return NULL;
    }
  }
  return Pointer;
}
 
學習中請大家多多指導呦~~ ☆星霧☆(Mineg Chien WU)
系統時間:2024-05-05 6:48:34
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!