Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6768549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:05:17+00:00 2026-05-26T15:05:17+00:00

This is my first post, but I have been using this site for a

  • 0

This is my first post, but I have been using this site for a while now, been very useful.

I am in the process of writing a memory-pool implementation but I have run into a strange problem. Right now I have 2 memory pools, there is an odd problem that whenever I initialize both of them the first array will have 1 more element than it is supposed to have. For each pool I add beyond the first it gains an additional element. It is not supposed to and I have no idea why.

In my code the first pool has 32 elements (0 – 31) which works fine, but when I initialize the second pool it shows as having 33 elements (0 – 32).

Here is my code :

#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++;
  }
}

Also the compiler I am using is minGW.

I am still somewhat new to C so this is probably a stupid mistake, but I cannot seem to solve it. Any Help would be appreciated, Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:05:18+00:00Added an answer on May 26, 2026 at 3:05 pm

    Pool_Block_S[BLOCKNUM_POOL_S-1] has only 31 elements. n in array[n] is number of elements not index of last element. This is the source of your problem.

    What is the meaning of sizeof(memoryBlock)/sizeof(int)? It doesn’t look correctly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First post, but I've been lurking around this site for a long while now,
This is my first post, I'm new to this site, but I've been lurking
This is my first post here, but I've using this site regularly to help
Well, this is my first post here and really enjoying the site. I have
I have been using Entity Framework CTP with Code-First as in this tutorial by
I have been a big fan of this site for a long time, but
We have been having this issue pop up sporadically, but now I can reproduce
This is my first post to stack overflow. I've been lurking this site for
it's my first post in here, but you have been helping me indirectly in
this is my first post on this website, but I'm all the time getting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.