Jump to content
  • Advertisement
Sign in to follow this  
M4573R

reinterpret casting pointers not an l-value

This topic is 3465 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
Just do two casts on the right hand side of the assignment: one to GenericObject*, the other back to char*.

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
Personally, I'd store the correct pointer type rather than char*, and cast it only when calling new and delete on the char array.
That makes the larger part of your code much easier to read, and it's less typing.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!