c# - Null reference exception vs zero pointer access violation -


recently read book c# , quite confused statement null reference exception not same access violation in native code when pointer dereferenced. meaning of quite unclear me, couldn't explain it, please?

in native code access memory locations directly using pointers. if pointer 32 bit can access around 4 billion virtual memory locations (2^32). however, not virtual memory locations mapped physical memory , of locations mapped read-only (e.g. code). if native code tries access virtual memory location inaccessible access violation on cpu.

so access violations happens when use invalid pointer (either reading or writing location pointed pointer). protection of hardware memory manager helps discover these errors trapping invalid access on cpu , raising form of error condition or exception. however, can have invalid pointers results in access memory locations accessible. though access invalid not discovered cpu , can lead memory corruption , other hard fix bugs.

a common error in native code forget initialize pointer before using it. pointer point address 0 (also known null pointer). strategy catching kind of error make first page in virtual address space (starting @ address 0) inaccessible. when invalid pointer dereferenced access violation. in native code null pointer error reported access violation because address 0 made inaccessible.

.net executes on virtual machine , not provide pointers (unless write unsafe code). concept of access violation not apply .net code. however, .net reference can uninitialized (e.g. null). virtual machine throw nullreferenceexception when null reference dereferenced. conceptually, similar how native code null pointer errors reported access violations described above concepts , mechanisms different.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -