에은 calloc 원인 재귀 :최우선의 malloc, 무료 내가 아래에 리눅스 플랫폼의 기능 <code>malloc</code>, <code>calloc</code> 및 <code>free</code> 기능을 대체 한 리눅스
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
typedef void* (*MALLOCFN)(size_t);
typedef void* (*CALLOCFN)(size_t, size_t);
typedef void* (*CALLOCFN)(size_t, size_t);
typedef void (*FREEFN)(void *);
MALLOCFN real_malloc = (MALLOCFN) 0;
CALLOCFN real_calloc = (CALLOCFN) 0;
FREEFN real_free = (FREEFN) 0;
void *get_realfn(const char *fnm)
{
void *pfunc = (void *) NULL;
printf("searching for original %s\n", fnm);
pfunc = dlsym(RTLD_NEXT, fnm);
if (pfunc) printf("found original %s\n", fnm);
else printf("not found original %s\n", fnm);
return pfunc;
}
void *malloc(size_t s)
{
printf("called malloc\n");
if(real_malloc == NULL) real_malloc = (MALLOCFN) get_realfn("malloc");
if (real_malloc) return real_malloc(s);
else return NULL;
}
void *calloc(size_t s1, size_t s2)
{
printf("called calloc\n");
if(real_calloc == NULL) real_calloc = (CALLOCFN) get_realfn("calloc");
if (real_calloc) return real_calloc(s1, s2);
else return NULL;
}
void free (void * ptr)
{
printf("called free\n");
if(real_free == NULL) real_free = (FREEFN) get_realfn("free");
if (real_free) real_free(ptr);
}
int main()
{
char * c1 = (char *) malloc(400);
char * c2 = (char *) malloc(400);
free(c2); free(c1);
return 0;
}
C 프로그램 (testalloc.c)의 g를 사용하여 구축됩니다 ++ 4.9 컴파일러의 버전 0.2 :
: 도시 출력g++ -g testalloc.c -ldl -o testalloc;
처음 몇 줄은 무한 재귀와 충돌에 입사, 아래와 같다 0
called malloc
searching for original malloc
called free
searching for original free
called free
searching for original free
called free
searching for original free
called free
searching for original free
called free
. . . .
재귀를 피하는 방법을 제안하십시오.
gcc, 랩 옵션. – someuser