2d阵列获得不需要的额外元素

时间:2011-10-29 15:12:12

标签: c arrays pointers

这是我的第一篇文章,但我现在已经使用这个网站了一段时间,非常有用。

我正在编写内存池实现,但我遇到了一个奇怪的问题。现在我有2个内存池,有一个奇怪的问题,每当我初始化它们时,第一个数组将有比它应该具有的多1个元素。对于每个池我添加超过第一个它获得一个额外的元素。它不应该,我不知道为什么。

在我的代码中,第一个池有32个元素(0 - 31)可以正常工作,但是当我初始化第二个池时,它显示为有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仍然有些新意,所以这可能是一个愚蠢的错误,但我似乎无法解决它。任何帮助将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:0)

Pool_Block_S[BLOCKNUM_POOL_S-1]只有31个元素。 n中的array[n]是元素的数量,而不是最后一个元素的索引。这是你问题的根源。

sizeof(memoryBlock)/sizeof(int)的含义是什么?它看起来不正确。