malloc °ú reallocÀ» ÇÇÇÏ°í °¡´ÉÇÑ ÇÑ new and zap(delete)À» ¸¹ÀÌ ¾²µµ·Ï ÇÏÀÚ. ÇÏÁö¸¸ °¡²ûÀº "C++"¿¡¼ "C" ½ºÅ¸ÀÏÀÇ ¸Þ¸ð¸® ¾ó·ÎÄÉÀ̼ÇÀ» ½á¾ßÇÒ ÇÊ¿ä°¡ ÀÖÀ» ¼öµµ ÀÖ´Ù. ÇÔ¼ö my_malloc() , my_realloc() and my_free()¸¦ ½á¶ó. ÀÌ ÇÔ¼öµéÀº allocations °ú initialisationsÀ» ÀûÀýÈ÷ ÇÏ°í ¸Þ¸ð¸® ¹®Á¦µéÀ» ¹æÁöÇÑ´Ù. ¶ÇÇÑ ÀÌ ÇÔ¼öµéÀº (DEBUG ¸ðµå¿¡¼) ¾ó·ÎÄÉÀÌÆ®µÈ ¸Þ¸ð¸®¸¦ ÃßÀûÇÏ°í ÇÁ·Î±×·¥ÀÌ ½ÇÇàµÇ±â Àü°ú ÈÄÀÇÃÑ ¸Þ¸ð¸® »ç¿ëÀ» ÇÁ¸°Æ®ÇÑ´Ù. ÀÌ°ÍÀº ¿©·¯ºÐ¿¡°Ô ¸Þ¸ð¸® À¯ÃâÀÌ ÀÖ´ÂÁöÀÇ ¿©ºÎ¸¦ ¾Ë·ÁÁØ´Ù. my_malloc °ú my_realloc Àº ¾Æ·¡¿Í °°ÀÌ Á¤ÀǵȴÙ. ÀÌ°ÍÀº Á¶±Ý ´õ ¸¹Àº ¸Þ¸ð¸®(SAFE_MEM = 5) ¸¦ ÇÒ´çÇÏ°í °ø°£À» ÃʱâÈÇÑ´Ù. ±×¸®°í ¸¸¾à¿¡ ÇÁ·Î±×·¥¾È¿¡ ÀÌ¹Ì ÇÒ´çÀÌ µÇ¾î ÀÖÀ¸¸é ÇÒ´çÀÌ µÇÁö ¾Ê´Â´Ù. 'call_check(), remove_ptr()' ÇÔ¼ö´Â ¿ÀÁ÷ DEBUG°¡ makefile¿¡¼ Á¤ÀÇ µÇ¾úÀ»¶§¿¡¸¸ ÀÛµ¿ÀÌ µÇ°í ÀÌ ÇÔ¼öµéÀº ((void)0) ( Áï NULL for non-debug production release.) ¿¡ assigned µÈ´Ù. ±×µéÀº »ç¿ëµÈ ÃÑ ¸Þ¸ð¸®¸¦ ÃßÀûÇÏ´Â °ÍÀ» °¡´ÉÄÉ ÇÑ´Ù.
void *local_my_malloc(size_t size, char fname[], int lineno) { size_t tmpii = size + SAFE_MEM; void *aa = NULL; aa = (void *) malloc(tmpii); if (aa == NULL) raise_error_exit(MALLOC, VOID_TYPE, fname, lineno); memset(aa, 0, tmpii); call_check(aa, tmpii, fname, lineno); return aa; } char *local_my_realloc(char *aa, size_t size, char fname[], int lineno) { remove_ptr(aa, fname, lineno); unsigned long tmpjj = 0; if (aa) // aa != NULL tmpjj = strlen(aa); unsigned long tmpqq = size + SAFE_MEM; size_t tmpii = sizeof (char) * (tmpqq); aa = (char *) realloc(aa, tmpii); if (aa == NULL) raise_error_exit(REALLOC, CHAR_TYPE, fname, lineno); // do not memset!! memset(aa, 0, tmpii); aa[tmpqq-1] = 0; unsigned long kk = tmpjj; if (tmpjj > tmpqq) kk = tmpqq; for ( ; kk < tmpqq; kk++) aa[kk] = 0; call_check(aa, tmpii, fname, lineno); return aa; }
char *aa; int *bb; float *cc; aa = (char *) my_malloc(sizeof(char)* 214); bb = (int *) my_malloc(sizeof(int) * 10); cc = (float *) my_malloc(sizeof(int) * 20); aa = my_realloc(aa, sizeof(char) * 34); bb = my_realloc(bb, sizeof(int) * 14); cc = my_realloc(cc, sizeof(float) * 10);