c++ - Why is assigning 0 to a pointer a solution to a dangling pointer? -
what os/debugger when pointer assigned 0?
the essential problem solves not cpu's implement same sorts of memory dereference semantics. in cases, it's not possible make dereferencing address after has been free() looks error. true on embedded processors. in other cases, allocators may lazy returning freed memory host operating system, performance reasons.
fundamentally, dereferencing such pointer lead seeing freed region, seeing zeroed out region, seeing memory has been returned subsequent allocation, or causing cpu exception. since such eventuality reasonable, c++ has assigned condition "undefined behavior".
to out of situation, want have way of distinguishing pointer values have been freed or allocated. such, c++ requires dereferencing pointer has been assigned 0 error, , converting such pointer integer return zero.
re: current edit.
pointers don't exist purposes of operating systems. @ lowest level, there no strings, integers, floats or pointers of sort. bytes. when write c++ program assigns 0 pointer value, operating system doesn't enter equation. that's totally compiler implementation.
on other hand, when dereference such pointer in program, c++ requires runtime error. on embedded systems, that's not practical, 0 valid memory address on such systems, sram near address. if compiler strictly implements standard, might insert check before every dereference see if null, , put mcu in error state, that's unusual, since slow slow system , increase program size.
on more featured systems, have memory management unit, 0 address in application not mapped physical page, operating system here, raising segfault in page program when tries access null pointer value.
Comments
Post a Comment