Sign in to follow this  
ishiitea

Storing data in a preset memory space

Recommended Posts

ishiitea    100
Hey everyone,


First post here, so I know alot of people are gonna throw "do your own HW!!!! LOL1" at me for this question, but i'm just having trouble grasping something.
I'm currently working on a class meant to hand a space of memory designated as:

const int mPoolsize = 65536;
char mPool[mPoolsize];

Now a part of the implementation for handling this memory cannot dynamically assign memory during execution, and if anything is declared it must be saved in mPool.
Now this is where my problem comes in. I'm having trouble grasping the concept that I can store anything other then characters inside of a character array, and have those stored values work with the
data itself. For example, in this instance, I know a pointer is needed to designate where the next open spot in the memory is so that it can be allocated to an incoming call/message. But the pointer has to be stored inside mPool, and nowhere else in memory.

My current design has me treating mPool[] as a circular array, so that when we get to the end, it doubles back to the beginning and starts again, allocating space that may have been deallocated. I just need to understand this fundamental part.

Thank you in advance for anyone willing to give me some assistance in trying to understand this. :)

Share this post


Link to post
Share on other sites
KulSeran    3267
You can cast a piece of memory as any other type you want. It may not be safe, and there are many ways to get yourself in trouble, but for POD types you are fairly ok. For instance;

char foo[ sizeof( int ) ];
int *bar = reinterpret_cast<int *>(foo);

You can even do this with POD only structs, like:
struct MemHeader
{
char *pNextFree;
size_t space_used;
size_t space_free;
};

But, even with POD types, you may have issues due to the fact that not all platforms support unaligned reads for all data types, so you have to properly align each type in the buffer.
With non-POD types, you need placement new to initialize them, and have to manually call the destructors.

Share this post


Link to post
Share on other sites
ishiitea    100
Thanks for the help KulSeran. Just a few things. I know I can change the cast of a specific data type in order to store something in another data type, just like you pointed out:

[color=#1C2837][size=2]char foo[ sizeof( int ) ];
int *bar = reinterpret_cast<int *>(foo);
[/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[color=#1C2837][size=2]Now with this, I know I just reinterpreted the character foo into an int for the purpose of having bar point to it. I could just make a char pointer (char *bar) and have it point to foo[0].. My question is really, how do I ensure that the 4 bytes that *bar is taking up are a part of the bytes that the compiler reserved for foo[sizeof(int)];[/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[color=#1C2837][size=2]Once I can figure that out, I can come up with how I can store other pieces of data into those memory spaces.[/size][/color]

Share this post


Link to post
Share on other sites
Buckeye    10747
[quote]how do I ensure that the 4 bytes that *bar is taking up are a part of the bytes that the compiler reserved for foo[sizeof(int)];[/quote]
If you mean the 4 bytes that the variable bar [i]itself[/i] is assigned, it's not part of foo. The variable bar has it's own memory to store the pointer value.

If you mean being assured that bar points to foo, that's what the cast does for you.

Share this post


Link to post
Share on other sites
ishiitea    100
[quote name='Buckeye' timestamp='1302557495' post='4797279']
[quote]how do I ensure that the 4 bytes that *bar is taking up are a part of the bytes that the compiler reserved for foo[sizeof(int)];[/quote]
If you mean the 4 bytes that the variable bar [i]itself[/i] is assigned, it's not part of foo. The variable bar has it's own memory to store the pointer value.

If you mean being assured that bar points to foo, that's what the cast does for you.
[/quote]

Thats just it, I need to make it so that the 4 bytes that the pointer would take up, are instead a part of the memory assigned to foo. That would be storing the variable of *bar to the first 4 elements in the foo array.

Share this post


Link to post
Share on other sites
KulSeran    3267
Overall, seems like a convoluted asignment.
You can just forgo making a variable, and just use everything as pointers:

char foo[ sizeof( int ) * 3];
// pretend foo is really int bar[3]....
*reinterpret_cast<int *>(foo + 0 * sizeof(int) ) = 5; // bar[0]
*reinterpret_cast<int *>(foo + 1 * sizeof(int) ) = 6; // bar[1]
*reinterpret_cast<int *>(foo + 2 * sizeof(int) ) = 7; // bar[2]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this