what happens in memory when I, say, do the following:
in C: char *c=NULL;
in java: MyClass mc=null;
what happens in memory, how is this null represented in memory in both of these languages? Thanks
- 529
- 2
- 6
- 18
-
In Java, you can't say "null character". Its a null value. – Lion Dec 17 '11 at 11:39
-
In C you need to include at least one of several headers for `NULL` to exist with the meaning you assume it has. `NULL` is a common identifier that can mean anything (**it shouldn't**) the programmer wants. – pmg Dec 17 '11 at 11:44
-
null character in C is a completely different concept, the title of your question goes completely wrong compared to your examples. – Jens Gustedt Dec 17 '11 at 12:30
4 Answers
In C you are basically setting the pointer to zero value. In fact
char *c = NULL;
is equivalent to
char *c = 0;
Zero pointer cannot be dereferenced since there is no memory mapping for this address. On platforms with virtual memory, an attempt to do so triggers a page fault transferring control to the operating system which then usually handles the situation by killing the offending process (in UNIX your process receives Segmentation Violation signal SIGSEGV).
In Java, all non-primitive type variables are references. null in Java is a literal denoting the only value of a special unnamed type. This type can be cast to any reference type allowing you to put null into any reference variable. An attempt to use such reference to access an object will throw an unchecked exception called NullPointerException. See JLS 3.10.7 and JLS 4.1 for details.
- 25,862
- 4
- 68
- 91
-
@Will, on most operating systems it will trigger a page fault. On some, nothing happens or something awful in general, but no page fault. – Prof. Falken Dec 17 '11 at 23:07
-
That's right. Some real-time and embedded operating systems don't use virtual memory, so no page fault can occur. Even some modern mainstream operating systems like Linux can even be configured not to use virtual memory, but this is employed in specialized embedded applications, often running on hardware without MMU. DSPs are a common example here. I made the text clearer. – Adam Zalcman Dec 17 '11 at 23:35
What you do here is assign a char pointer to NULL.
It has basically the same effect, with the huge difference that dereferencing NULL in C will make your program die with a SIGSEGV (well, under Unix-like OSes, including Linux and Mac OS X), while Java will throw a NullPointerException.
Note that mc is really a reference, not a class by itself. It is nearly the same as a C pointer.
NULL has some practical value in both cases anyway: you can test if (mc == null) in Java. In C, that would be a simple if (!p) (but if (p == NULL) also works).
- 114,841
- 28
- 237
- 319
You can find your answer in the following posts:
What exactly is null in Java memory
Java - Does null variable require space in memory
So basically, the null itself does not take space but the object being put equal to null does take space
- 1
- 1
- 9,965
- 7
- 51
- 89
NULL is a special value indicating there's no data at that location, it's specific representation can vary. in C it's just 0, i'm note sure about java.
- 7,791
- 3
- 28
- 54
-
No, `NULL` is an address like any other. It is just very convenient for tests as you can test `if (!p)` where `p` is NULL. – fge Dec 17 '11 at 11:38
-
In C the concept of null-pointer can be represented in several ways: the most usual way is `NULL` (`NULL` isn't necessarily `0`); other ways are `0`, `(void*)0`. Two null-pointer values compare equal to each other regardless of how they got their value. – pmg Dec 17 '11 at 11:38
-