Sign in to follow this  
M4573R

reinterpret casting pointers not an l-value

Recommended Posts

I'm creating a memory manager and my casting is giving me errors. I have an internal block of memory stored in a char pointer. The memory is split up into blocks with padding on each side of blocks of a specific size. The first 4 bytes of each block points to another free block. This sets up a linked list inside my memory. To do this, different address are cast to a generic object in order to point the memory to somewhere else. Here is the generic object: struct GenericObject { GenericObject *Next; }; My internal memory is stored as: char* FreeList_; This does not work. It says left operand is not an l-value. reinterpret_cast<GenericObject*>(FreeList_) = reinterpret_cast<GenericObject*>(FreeList_)->Next; But this works: char* tempPtr = FreeList_; reinterpret_cast<GenericObject*>(tempPtr)->Next = reinterpret_cast<GenericObject*>(FreeList_); Long story short: Why is casting a char* to a GenericObject* not an L-Value??

Share this post


Link to post
Share on other sites
You actually can make it work like that; you simply have to have the right-hand reinterpret_cast cast to a reference to a pointer, rather than to a pointer. IMHO, the result is rather ugly, though, and doesn't speak to the code's true functionality. What your code is actually doing is "decoding" the pointer (to a pointer to your actual type), messing with it, then re-encoding it to a char*.

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