Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Invalid pointer values [closed]

Tags:

c++

pointers

I would like to know which pointer values are invalid so i would not have to allocate new memory just to mark special chunk states(Memory consumption is critical). So i could use them for special states like

  • 0x00000000 - would mean chunk is not loaded
  • 0x00000001 - would mean chunk is empty
  • 0x00000002 - chunk is full. And when some real stuff needs to be saved to the memory i would do new Chunk(...);
like image 352
BlackCat Avatar asked Apr 22 '26 06:04

BlackCat


2 Answers

I would suggest just using a struct that contains a pointer and an enum. But if for some reason that's inconvenient, just allocate some small structures and use their addresses just to indicate magic pointer values. (Of course, don't ever free them.)

You can also use the address of static objects. Like this:

static int chunk_not_loaded_i, chunk_empty_i, chunk_full_i;
void *chunk_not_loaded = &chunk_not_loaded_i;
void *chunk_full = &chunk_full_i;

if (some_chunk == chunk_not_loaded)
    ...
like image 80
David Schwartz Avatar answered Apr 24 '26 18:04

David Schwartz


Assigning exact values to the pointer is quite unstable and error-prone. That way, your code would be tight to exact hardware architecture(s). For example, some platforms have 0x00000000 as absolutely valid address. So the fact that address is assigned or not is not related to numeric value of the pointer (at common case).

like image 44
Yury Schkatula Avatar answered Apr 24 '26 20:04

Yury Schkatula



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!