これは私の最初の投稿ですが、私はこのサイトをしばらく使用してきました。2d配列は不要な追加要素を得ます
私はメモリプールの実装を作成中ですが、私は奇妙な問題に遭遇しました。今私は2つのメモリプールを持って、奇妙な問題は、私がそれらの両方を初期化するたびに、最初の配列は持っているよりも1つ以上の要素があります。各プールについて、最初に追加する要素には追加の要素があります。それはないと私は理由が分からない。
私のコードでは、最初のプールには32個の要素(0〜31)がありますが、2番目のプールを初期化すると33個の要素(0〜32)があることがわかります。ここで
は私のコードです:
#include <stdio.h>
typedef struct memoryBlock {
/* Pointer to array */
int *Address;
struct memoryBlock *Next;
}memoryBlock;
/* Small Pool */
#define BLOCKNUM_POOL_S 32 //number of blocks
#define BLOCKSIZE_POOL_S 8 //ints per block
static memoryBlock *Pool_Head_S;
static memoryBlock *Pool_Tail_S;
/* The memory that will be dynamically allocated will be stored in this array */
static int Pool_Block_S[BLOCKNUM_POOL_S-1][BLOCKSIZE_POOL_S+sizeof(memoryBlock)/sizeof(int)];
/* This is a free list containing only pointers to free blocks in this pool */
static int Pool_Free_S[BLOCKNUM_POOL_S-1][sizeof(memoryBlock)/sizeof(int)];
/* Medium Pool */
#define BLOCKNUM_POOL_M 16 //number of blocks
#define BLOCKSIZE_POOL_M 16 //words per block
static memoryBlock *Pool_Head_M;
static memoryBlock *Pool_Tail_M;
/* The memory that will be dynamically allocated will be stored in this array */
static int Pool_Block_M[BLOCKNUM_POOL_M-1][BLOCKSIZE_POOL_M+sizeof(memoryBlock)/sizeof(int)];
/* This is a free list containing only pointers to free blocks in this pool */
static int Pool_Free_M[BLOCKNUM_POOL_M-1][sizeof(memoryBlock)/sizeof(int)];
void printS();
void printM();
void initPool_S();
void initPool_M();
void main(){
initPool_S();
initPool_M();
printS();
printM();
}
void initPool_S(){
int i;
Pool_Tail_S = NULL;
Pool_Head_S = NULL;
for(i=0;i<BLOCKNUM_POOL_S;i++){
//for each block setup the memory block and pointers
if(Pool_Tail_S){
Pool_Tail_S->Next = (memoryBlock *)&Pool_Free_S[i][0];
Pool_Tail_S->Next->Address = &Pool_Block_S[i][0];
Pool_Tail_S = Pool_Tail_S->Next;
Pool_Tail_S->Next = NULL;
/* There is nothing in this list yet */
}else{
Pool_Head_S = (memoryBlock *)&Pool_Free_S[i][0];
Pool_Head_S->Address = (int *)&Pool_Block_S[i][0];
Pool_Head_S->Next = NULL;
Pool_Tail_S = Pool_Head_S;
}
}
}
void initPool_M(){
int i;
Pool_Tail_M = NULL;
Pool_Head_M = NULL;
for(i=0;i<BLOCKNUM_POOL_M;i++){
//for each block setup the memory block and pointers
if(Pool_Tail_M){
Pool_Tail_M->Next = (memoryBlock *)&Pool_Free_M[i][0];
Pool_Tail_M->Next->Address = (int *)&Pool_Block_M[i][0];
Pool_Tail_M = Pool_Tail_M->Next;
Pool_Tail_M->Next = NULL;
/* There is nothing in this list yet */
}else{
Pool_Head_M = (memoryBlock *)&Pool_Free_M[i][0];
Pool_Head_M->Address = (int *)&Pool_Block_M[i][0];
Pool_Head_M->Next = NULL;
Pool_Tail_M = Pool_Head_M;
}
}
}
void printM(){
memoryBlock *tmpPtr2;
tmpPtr2 = Pool_Head_M;
int j=0;
while(tmpPtr2){
printf(">-------------------------------------------------<\n");
printf("%d\n",j);
printf("Pool_Med_Free: %d\n",tmpPtr2);
printf("Pool_Med_Free->Address: %d\n",tmpPtr2->Address);
printf("Pool_Med_Free->Next: %d\n",tmpPtr2->Next);
tmpPtr2 = tmpPtr2->Next;
j++;
}
}
void printS(){
memoryBlock *tmpPtr1;
tmpPtr1 = Pool_Head_S;
int j=0;
while(tmpPtr1){
printf(">-------------------------------------------------<\n");
printf("%d\n",j);
printf("Pool_Small_Free: %d\n",tmpPtr1);
printf("Pool_Small_Free->Address: %d\n",tmpPtr1->Address);
printf("Pool_Small_Free->Next: %d\n",tmpPtr1->Next);
tmpPtr1 = tmpPtr1->Next;
j++;
}
}
また、私が使用しているコンパイラはMinGWのです。
私はやや新しいCですので、これはおそらく愚かな間違いですが、解決できないようです。どんな助けもありがとう、ありがとう!
sizeof(memoryBlock)/ sizeof(int)は、メモリブロックのサイズ(バイト数)/サイズ(バイト数)が1であることを意味します。構造体は私のマシン上では8バイト、intは4です。これは配列内の構造体を保持するために8/4 = 2 ints分のメモリが必要であることを意味します。また、私の問題を解決した要素の数についての問題を指摘してくれてありがとう:) – Marius