0

Let's say we have a char array:

char pool[1000];

and a pointer

char* ptr;

the pointer stores an address to a block of data in Pool. I want to store this address in the pool and retrieve it as well.

Basically, what I want to do is a linked-list that is embedded in the char array Pool. The reason is that i'm not allowed to create any new variables (globally), and I cant include new headers, among other restrictions.

So the question is : how to I segment and fit a 4 byte address in (let's say) the first 4 elements of pool[] , and how do I retrieve it again for the purpose of modification.

This operation will happen frequently so it needs to be fast...and of course not rely of external functions.

Nawar
  • 1
  • 1
  • 1
  • sounds like you want something similar to a stack allocator. how do you want to keep track of used portions of the pool? – kmdreko May 18 '16 at 04:14
  • Ref this: http://stackoverflow.com/q/20945439/1870232 – P0W May 18 '16 at 04:15
  • @P0W this is helpful. But I can't declare global variables, hence storing in pool. – Nawar May 18 '16 at 05:01
  • @vu1p3n0x Yes, I'm making an allocator. Stack allocator won't do it for my scenario, so it's going to be some form of freestore allocator. I didn't get to the point of keeping track of the blocks yet. As you can see, I want to get past the storing and retrieving date first :) . But i'm open for suggestions in that regard. – Nawar May 18 '16 at 05:05

2 Answers2

1

To store the value of ptr in pool, use:

memcpy(pool, &ptr, sizeof(ptr));

To retrieve the value of the pointer from pool, use:

memcpy(&ptr, pool, sizeof(ptr));

Re:

This operation will happen frequently so it needs to be fast...and of course not rely of external functions.

memcpy is exactly what you need. If you cannot use memcpy, you'll need to implement the exact functionality in your code.

Unless you are able to verify that the calls to memcpy is a performance bottleneck, I would advise using it.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • I cant use it since it needs string.h/cstring . I will try writing it – Nawar May 18 '16 at 04:59
  • so I have used this implementation for memcpy: `code` void myMemCpy(void *dest, void *src, size_t n) { // Typecast src and dest addresses to (char *) char *csrc = (char *)src; char *cdest = (char *)dest; // Copy contents of src[] to dest[] for (int i = 0; i < n; i++) { cdest[i] = csrc[i]; std::cout << "i = " << i << std::endl; } } `code` it works but it doesn't store the pointer in the array. It just set the pointer to the array address. – Nawar May 18 '16 at 20:39
0

If you want to store ptr in pool then should you have a pool of addresses instead?

char *pool[1000];

Then

pool[0] = ptr;
ptr = pool[0];
etc.

If you have to deal with a buffer of characters, that is

char pool[1000];

but treat it as a buffer of addresses then bend it by creating a local variable:

char **pp = reinterpret_cast<char**>(&pool);
*pp = ptr;

The later stores ptr at the start of the buffer pool. To retrieve it:

ptr = *pp;

You may move along the pool by incrementing the pointer pp

pp++ // next address

And don't make an assumption that address is 4 bytes!

dmitri
  • 3,183
  • 23
  • 28