views:

311

answers:

1

One of job interview questions on C pointers here is the following: what is null pointer assignment error?

I've googled for a while and don't see any reasonable explanation. What is that? Trying to write through a null pointer? Something architecture- or environment-specific? What exactly is that error?

+5  A: 

http://www.faqs.org/qa/qa-3786.html

A NULL pointer assignment is a runtime error It occurs due to various reasons one is that ur program has tried to access an illegal memory location. Illegal location means either the location is in the operating systems address space or in the other processes memory space. In stdio.h NULL is defined as 0 So whenever your program tries to access 0th location the operating system kills your program with runtime assignment error because the 0th location is in the operating systems address space and operating system doesnt allow access to its address space by user program .

Example code:
int* ptr = NULL;
*ptr = 3;

Explanation:
On almost every system, address 0 is reserved. System won't allow you to write to that location. If you try, you will get a runtime exception (access violation, segmentation fault, etc.).

Paja
I've seen this. Is that accurate? Does it just boil down to writnig through a null pointer?
sharptooth
Yes. As name of the error suggests ("Null pointer assignment error"), it is thrown when you try to assign value (not address) to a pointer, which has been initialized to NULL. (But NULL is not necessarily zero (although almost always is), as pointed out by wiki: http://en.wikipedia.org/wiki/Pointer_(computing)#Null_pointer)
Paja