gpt4 book ai didi

c - 二维数组获得不需要的额外元素

转载 作者:太空宇宙 更新时间:2023-11-04 00:09:28 25 4
gpt4 key购买 nike

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

我正在编写内存池实现,但遇到了一个奇怪的问题。现在我有 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 还是有些陌生,所以这可能是一个愚蠢的错误,但我似乎无法解决它。任何帮助将不胜感激,谢谢!

最佳答案

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

sizeof(memoryBlock)/sizeof(int) 是什么意思?它看起来不正确。

关于c - 二维数组获得不需要的额外元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7939721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com