ファイルdbg_malloc.h、dbg_malloc_p.h、dbg_malloc_sub.c,
があります。gcc -c dbg_malloc.h -o -Wall ,gcc -c dbg_malloc_p.h -o -Wall
,gcc -c dbg_malloc_sub.c -o -Wall と実行すると、
gcc -c dbg_malloc_sub.c -o -Wall で以下のエラーがでます。
何回もコードを見直したのですが、上手くいきません。
教えてください。
実行結果
naka@naka ~/sample
$ gcc -c dbg_malloc_sub.c -o -Wall
dbg_malloc_sub.c: In function 'dbg_realloc':
dbg_malloc_sub.c:29:10: error: 'dbg_memory_block_hash' undeclared (first use in this function)
hash=&(dbg_memory_block_hash[DBG_HASH_FUNC(ptr)]);
^~~~~~~~~~~~~~~~~~~~~
dbg_malloc_sub.c:29:10: note: each undeclared identifier is reported only once for each function it appears in
naka@naka ~/sample
$
コード メモリ管理用ライブラリ(dbg_malloc.h) #ifndef _DBG_MALLOC_H_INCLUDED_ #define _DBG_MALLOC_H_INCLUDED_ #include <stdio.h> #include <stdlib.h> //malloc(),free()などのプロトタイプ宣言が行われている #include <string.h> //必要な関数のプロトタイプ宣言 void *dbg_malloc(size_t size,const char *file,int line); void *dbg_calloc(size_t number,size_t size,const char *file,int line); void *dbg_realloc(void *ptr,size_t size,const char *file,int line); り char *dbg_strdup(const char *str,const char *file,int line); void dbg_free(void *ptr,const char *file,int line); //メモリ管理情報の出力用の3つの関数を宣言 void *dbg_print_alloc_count(FILE *fp); void *dbg_print_alloc_block(FILE *fp,const char *file,int line); void *dbg_print_all_alloc_block(FILE *fp); #ifndef DBG_MALLOC_NOCHANGE #define malloc(s) dbg_malloc((s),__FILE__,__LINE__) #define calloc(n,s) dbg_calloc((n),(s),__FILE__,__LINE__) #define realloc(p,s) dbg_realloc((p),(s),__FILE__,__LINE__) #define strdup(str) dbg_strdup((str),__FILE__,__LINE__) #define free(p) dbg_free((p),__FILE__,__LINE__) #endif #endif メモリ管理用ライブラリ(dbg_malloc_p.h) #ifndef _DBG_MALLOC_P_H_INCLUDED_ #define _DBG_MALLOC_P_H_INCLUDED_ #define DBG_MALLOC_NOCHANGE #include <time.h> //struct memory_block構造体のメンバにtime_t型を利用しているためです #include "dbg_malloc.h" //ソース中でdbg_malloc()を利用している場所ごとに作成される構造体 struct memory_count{ const char *file; int line; int number; unsigned int alloc_count unsigned int free_count; int size; unsigned int alloc_size; unsigned int free_size; struct memory_block *block_list; struct free_count *free_list; struct memory_count *prev; struct memory_count *next; }; //メモリ管理用ブロック struct memory_block{ int id; int size; time_t t; struct memory_count *mcp; struct memory_block *prev; struct memory_block *next; struct memory_block *hash_prev; struct memory_block *hash_next; void *data; }; //free()管理用のブロック struct free_count{ const char *file; int line; unsigned int count; unsigned int size; struct free_count *next; }; //malloc()統計情報となる変数の型宣言 extern unsigned int dbg_malloc_count; //malloc()の呼び出し回数 extern unsigned int dbg_free_count; //free()の呼び出し回数 extern unsigned int dbg_malloc_number; //現在のメモリ取得量 extern unsigned int dbg_malloc_size; //現在のメモリ取得量 extern struct memory_count *dbg_memory_count_head; //リストの先端 extern struct memory_count *dbg_memory_count_tail; //リストの終端 #define DBG_HASH_NUM 997 #define DBG_HASH_FUNC(p)\ ((\ (((unsigned long int)(p) >>4) & 0xf)+\ ((unsigned long int)(p) >>8)\ ) %DBG_HASH_NUM ) // ハッシュ用配列 extern struct memory_block *dbg_malloc_block_hash[DBG_HASH_NUM]; // 初期化用関数のプロトタイプ宣言 void dbg_init(); #endif //_DBG_MALLOC_P_H_INCLUDED_ ライブラリ本体 dbg_malloc_sub.c #include "dbg_malloc_p.h" // "dbg_malloc.h"は"dbg_malloc_p.h"からインクルードされるので // ここではインクルードしない void *dbg_calloc(size_t number,size_t size,const char *file,int line) { void *p; p=dbg_malloc(number *size,file,line); if(p){ memset(p,0,number *size); } return (p); } void *dbg_realloc(void *ptr,size_t size,const char *file,int line) { void *p; struct memory_block *mbp; struct memory_block **hash; p=dbg_malloc(size,file,line); if((p != NULL) && (ptr != NULL)){ hash=&(dbg_memory_block_hash[DBG_HASH_FUNC(ptr)]); for(mbp=*hash;mbp;mbp=mbp->hash_next){ if(mbp->data==ptr) break; } if((mbp != NULL) && (size > mbp->size)){ size = mbp->size; } memcpy(p,ptr,size); dbg_free(ptr,file,line); } return (p); } char *dbg_strdup(const char *str,const char *file,int line) { int len; void *p; len = strlen(str)+1; p=dbg_malloc(len,file,line); if(p==NULL) return(NULL); memcpy(p,str,len); return (p); }
回答1件
あなたの回答
tips
プレビュー