473,320 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

NULL Pointer Dereference

Hello eveyone.

What exactly is a NULL Pointer Dereference? I tried searching google but
the only results that returned were bug reports. Some example code, if
possible, would also be appreciated.

Thanks in advance.
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----
Nov 13 '05 #1
10 25970

"Denis Palmeiro" <en*******@gta.igs.net> wrote in message news:pa****************************@gta.igs.net...
Hello eveyone.

What exactly is a NULL Pointer Dereference? I tried searching google but
the only results that returned were bug reports. Some example code, if
possible, would also be appreciated.


int *p = 0;
*p; // dereferencing a null pointer
--
Jun, Woong (my******@hanmail.net)
Dept. of Physics, Univ. of Seoul

Nov 13 '05 #2
Yes, that's how we dereferenc pointer. Dereferencing them, yields tha
actual data they point to.

Generally, NULL is considered to be a error condition. Like the
pointer is not pointing anywhere.

To declare NULL pointer use,

int * intPtr = (int *) NULL; /* NULL is defined as 0 */

Dereferncing NULL pointer may cause illegal operation...I am not sure

sumit

"Jun Woong" <my******@hanmail.net> wrote in message news:<be**********@news.hananet.net>...
"Denis Palmeiro" <en*******@gta.igs.net> wrote in message news:pa****************************@gta.igs.net...
Hello eveyone.

What exactly is a NULL Pointer Dereference? I tried searching google but
the only results that returned were bug reports. Some example code, if
possible, would also be appreciated.


int *p = 0;
*p; // dereferencing a null pointer

Nov 13 '05 #3
On 8 Jul 2003 07:13:19 -0700, su**********@wipro.com wrote:
Yes, that's how we dereferenc pointer. Dereferencing them, yields tha
actual data they point to.

Generally, NULL is considered to be a error condition. Like the
pointer is not pointing anywhere.

To declare NULL pointer use,

int * intPtr = (int *) NULL; /* NULL is defined as 0 */

Dereferncing NULL pointer may cause illegal operation...I am not sure

sumit


Please do not top post. I actually thought that Jun's post was a
correct answer. Your post has some odd constructions. For example,
you do not need to cast NULL explicitly.

NULL is a macro defined in stddef.h. to be the null pointer constant.
A null pointer constant is an integer constant expression with the
value 0 or such an expression casted to void*. A null pointer
constant converted to a pointer type is called a null pointer.

#include <stddef.h>

int* ip = NULL;
*ip; // undefined behavior

would be sufficient. The problem with derefencing a null pointer is
that it invokes undefined behavior. It is impossible to know how any
particular implementation will handle it.

Best wishes,

Bob
Nov 13 '05 #4
If the pointer is not initialised expicitly, does'nt it get
initialised to NULL by default.I have written the following program
which shows that by default any pointer will be intialised to
NULL.Have a look at the program.

#include <stdio.h>

int main()
{
int *p;
if (p ==NULL)
printf("pointer is null %p\n",p);
else
printf("pointer is not null\n");

}

and here is the output,
$ cc dereference_null.c
$ ./a.out
pointer is null 0

or does it depends on the system that is being used?I executed this
program on AIX.
Nov 13 '05 #5
Denis Palmeiro <en*******@gta.igs.net> wrote:
Hello eveyone.

What exactly is a NULL Pointer Dereference? I tried searching google but
the only results that returned were bug reports.
No wonder - dereferencing a NULL po9inter *is* usually a bug. If you try to
READ the stuff to which the null pointer points then you may get away with
garbage values, but protected mode OSes will stop the program ('segfault').
If you WRITE to it, then either the OS catches the attempt, or you have
good chances of locking up the machine.
Some example code, if
possible, would also be appreciated.

Thanks in advance.
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----


Nov 13 '05 #6
va******@rediffmail.com (prashna) writes:
If the pointer is not initialised expicitly, does'nt it get
initialised to NULL by default.
NO. The only time this will happen without an explicit initializer is
when it is statically allocated.
I have written the following program
which shows that by default any pointer will be intialised to
NULL.Have a look at the program.

#include <stdio.h>

int main()
{
int *p;
if (p ==NULL)
printf("pointer is null %p\n",p);
else
printf("pointer is not null\n");

}

and here is the output,
$ cc dereference_null.c
$ ./a.out
pointer is null 0

or does it depends on the system that is being used?I executed this
program on AIX.


You were simply unlucky.

-Micah
Nov 13 '05 #7
Hi,

I know that NULL is defined as 0. But there are chances that compiler
may generate warnings (invalid pointer assignments) when you do this
--

int * iPtr = NULL; /* maybe illegal */

So to be on SAFE side, I had typecasted it to int *, so to avoid
any compiler warnings...if it is INTELLIGENT.

So you are just complementing my answer abt de referencing NULL
pointer :):)
Please do not top post. I actually thought that Jun's post was a
correct answer. Your post has some odd constructions. For example,
you do not need to cast NULL explicitly.

NULL is a macro defined in stddef.h. to be the null pointer constant.
A null pointer constant is an integer constant expression with the
value 0 or such an expression casted to void*. A null pointer
constant converted to a pointer type is called a null pointer.

#include <stddef.h>

int* ip = NULL;
*ip; // undefined behavior

would be sufficient. The problem with derefencing a null pointer is
that it invokes undefined behavior. It is impossible to know how any
particular implementation will handle it.

Best wishes,

Bob

Nov 13 '05 #8
su**********@wipro.com writes:
I know that NULL is defined as 0.
It may also be defined as ((void *) 0) or other variations as
well.
But there are chances that compiler may generate warnings
(invalid pointer assignments) when you do this --

int * iPtr = NULL; /* maybe illegal */
That's not ever illegal (and not invalid unless you failed to
define NULL by including an appropriate header). It is perfectly
fine as specified by the language definition.
So to be on SAFE side, I had typecasted it to int *, so to avoid
any compiler warnings...if it is INTELLIGENT.


I'd be very surprised to have a compiler complain about the
above, but compilers are allowed to warn about anything.
--
"To get the best out of this book, I strongly recommend that you read it."
--Richard Heathfield
Nov 13 '05 #9
On 14 Jul 2003 10:47:49 -0700, in comp.lang.c , su**********@wipro.com
wrote:
Hi, please don't top post. Your response should go BELOW the text it
relates to.
I know that NULL is defined as 0. But there are chances that compiler
may generate warnings (invalid pointer assignments) when you do this
--
don't put two dashes and a space in the middle of your post. That
separates a sig from the body, and causes all good newsreaders to snip
all the text below. I had to force Agent to include it.
int * iPtr = NULL; /* maybe illegal */
thats never illegal in declaration.
So to be on SAFE side, I had typecasted it to int *,


Thats unnecessary. In C. Are you accidentally compilng in C++ mode?

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #10
>>may generate warnings (invalid pointer assignments) when you do this
--


don't put two dashes and a space in the middle of your post. That
separates a sig from the body, and causes all good newsreaders to snip
all the text below. I had to force Agent to include it.


Hmmm, there was no space after the two hyphens :)

Nov 13 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
by: BigMan | last post by:
Does the C++ standard define what should happen in case of NULL pointer dereferencing. If not, does it say that it is illegal? Where, if so, does it say it?
5
by: Angel Tsankov | last post by:
Does the standard define what happens when a NULL pointer is dereferenced? If so, where?
12
by: somenath | last post by:
Hi All , I have some doubts fro the following program. #include<stdio.h> int main(void) { char **p = 0; printf("%d\n", ++p); return 0;
11
by: Alan Woodland | last post by:
Hi, I'm fairly sure this is undefined behaviour, despite the fact that it compiles and 'runs' (prints "this doesn't exist") on all my platforms: #include <iostream> class foo { public:...
17
by: Tony Jackson | last post by:
Hi I'm quite new to C programming - I have more of a Java background: maybe someone here can advise me. I'm nearly finishing a little program but it has some hard-to-find bugs that basically...
20
by: prashant.khade1623 | last post by:
I am not getting the exact idea. Can you please explain me with an example. Thanks
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.