Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6984259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:31:35+00:00 2026-05-27T18:31:35+00:00

I was playing with unsafe code for a problem on Code Golf, and I

  • 0

I was playing with unsafe code for a problem on Code Golf, and I found something I can’t explain. This code:

unsafe
{
    int i = *(int*)0;
}

Crashes with an access violation (Segfault), but this code:

unsafe
{
    *(int*)0=0;
}

Throws a NullReferenceException. It appears to me that the first is performing a read and the second is performing a write. An exception tells me that something, somewhere in the CLR is intercepting the write and stopping it before the OS kills the process. Why does this happen on the write, but not on the read? It does segfault on a write if I make the pointer value sufficiently large. Does that mean there is a block of memory that the CLR knows is reserved and will not even attempt to write to? Why then, does it allow me to try and read from that block? Am I completely misunderstanding something here?

Edit:

Interestingly enough: System.Runtime.InteropServices.Marshal.WriteInt32(IntPtr.Zero, 0); Gives me an access violation, not a NullReference.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:31:35+00:00Added an answer on May 27, 2026 at 6:31 pm

    The first exception makes sense, of course – you’re trying to read from memory address 0. The second one’s a little more interesting. 😛 In C++, there is a macro/constant called NULL which has a value of 0. It is used for pointer addresses that are invalid – much like the null value in C# for reference types. Since C# references are pointers internally, a NullReferenceException occurs when you try to read/write from that address – the address NULL or 0; in fact, the addresses from 0 to 64K are invalid in all processes (in Windows) so as to catch programer mistakes. The exact exception or error may vary slightly by computer hardware or version of Windows/.NET Framework, but you should get an error with both code snippets.

    As for the segfault when reading/writing to random addresses, that is due to the OS’s isolation of each process. You can’t fiddle around with other processes’ code or data – at least not legitimately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While playing around with regexps in Scala I wrote something like this: scala> val
I was playing around with anonymous subclasses and I found a problem that I
Recently playing around with the open source iphone app code, and found it uses
Playing a little with coffeescript and Rails 3.1.0.rc4. Have this code: yourMom = (location)
Im playing a little bit with heavy-client app. Imagine I have this model: class
Still playing with Chrome Extension and have a little problem when I want to
Just playing around with java trying to learn it etc. Here is my code
Playing around with jQuery a bit (sorry, complete noob) I was wondering why this
Im playing about with the django auth library, and ive come across something i
Playing with Git and GitHub,I found that sometimes a git commit -a is needed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.