0

I have a large char array which is functioning as a memory pool and want to store a pointer in the first position of the array which points to whatever the next open position in the pool is, so every time something is allocated to the pool the pointer would point to the byte that follows the ones which were just allocated. My only problem is I am not quite sure how to store the pointer in the array and then be able to modify it in other functions since the only place the pointer will exist is in the array[0] position. Can anyone point me in the right direction on this?

the char array is declared like:

char pool[size];
William Pursell
  • 204,365
  • 48
  • 270
  • 300
zfetters
  • 447
  • 2
  • 11
  • Why are you (a) implementing a memory pool and (b) implementing a memory pool as a `char array`? – johnsyweb Feb 25 '13 at 05:22
  • it is a project which is meant to just be a basic implementation which supplies a small amount of code which i am required to use and it uses a char array – zfetters Feb 25 '13 at 05:24

4 Answers4

1

What you really want is an index into that array that tells you where to insert.

You could declare a structure:

struct pool
{
    char poolData[size];
    int insertIndex;
};

So that you always have the pool memory and index where you want to insert to together. Or, just have a separate variable and pass them together to whoever needs to use it.

char pool[size];
int insertIndex;

There's no need to "hijack" the first element of the array and use it differently than the rest of the array; just declare another variable to track the state of the pool.

  • that was an initial thought going into the project but it requires that all variables i declare myself for use must be stored within the memory pool as well which is why my next thought was to keep a pointer in the first array position and have it point to the next free byte in the pool – zfetters Feb 25 '13 at 05:28
0

I think what you need is an index to remember where is the next empty spot on the pool (initially will be zero).

char pool[size];
int index = 0;

Then everytime you insert a new element, you just increment it:

if(index < size) {
  pool[index] = 123;
  index++;
}
gerrytan
  • 40,313
  • 9
  • 84
  • 99
0

If you can't follow the recommendations of the other answers because you absolutely must use the pool to store all information in it, the safest way to store the integer information in the char-array is to use memcpy (I am using C++11 syntax):

#include <cstring>
#include <iostream>

int main()
{
  using size_type = std::size_t;
  static constexpr size_type size = 1000;
  char pool[size];

  /* Store 12 as integer information at the beginning
     of the pool: */    
  size_type next_free = 12;
  std::memcpy(pool,&next_free,sizeof(size_type));

  /* Retrieve it: */
  size_type retrieved = 0;
  std::memcpy(&retrieved,pool,sizeof(size_type));

  /* This will output 12: */    
  std::cout << retrieved << std::endl;

  return 0;
}

Of course this implies that the first sizeof(size_type) entries of the pool must not be used to store any actual characters. The lowest entry you can actually use is pool[sizeof(size_type)].

jogojapan
  • 68,383
  • 11
  • 101
  • 131
0
char **pPoolEnd = (char **) pool;

to initialize the pointer you want.

*pPoolEnd = pool + sizeof(char **);

to make it point to its own end (e.g. when there's nothing else in the pool).

However, why would you want to do this? It's confusing, error prone and probably unnecessary. Others have pointed to much better alternatives. Assuming I had such a pool in the first place, I would probably go with one of those, or simply use a separate pointer, char *poolEnd along with pool.

Also, it's bad style to expose your implementation details to users ("pool end pointer is at pool[0]") and even worse to expect them to deal with them ("please update pool[0] whenever you'd like to allocate from the pool"). Think malloc and free; expose simple function interfaces to your users.

aib
  • 45,516
  • 10
  • 73
  • 79