Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Pointer arithmetic involving NULL pointers

Reply
Thread Tools

Pointer arithmetic involving NULL pointers

 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      09-10-2004
Is adding 0 to a pointer to non-void that is equal to NULL legal?

int *p=NULL;
p+=0;

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      09-10-2004
Christopher Benson-Manica wrote:
> Is adding 0 to a pointer to non-void that is equal to NULL legal?
>
> int *p=NULL;
> p+=0;


Never considered it before, but I think it's undefined.

6.5.6 Additive operators
/8/ [...] If both the pointer operand and the result
point to elements of the same array object, or one past
the last element of the array object, the evaluation
shall not produce an overflow; otherwise, the behavior
is undefined. [...]

Since `p' above does not point to any element of any array
object, the "otherwise" holds.

If you don't mind my asking, what are you trying to do?
I've added zero to a pointer value plenty of times, as in

char *p = string_with_possible_leading_spaces;
p += strspn(p, " \t\f\n\r";

.... but I can't recall being tempted to do any such thing
with a NULL value. What's up?

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Dave Vandervies
Guest
Posts: n/a
 
      09-10-2004
In article <chsudn$ssj$(E-Mail Removed)>,
Christopher Benson-Manica <(E-Mail Removed)> wrote:
>Is adding 0 to a pointer to non-void that is equal to NULL legal?
>
>int *p=NULL;
>p+=0;


No; pointer addition is only defined for pointers to or one past a valid
object, so adding anything (even 0) to a null pointer is undefined.
(N869 6.5.6#8, see also #7.)

(But the sigmonster made a good choice today.)


dave

--
Dave Vandervies (E-Mail Removed)
I would lodge a serious QOI complaint with the compiler vendor if either of
these reformatted my nose or caused Scott Nudds to fly out of my hard drive.
--Jack Klein in comp.lang.c
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-10-2004
Christopher Benson-Manica <(E-Mail Removed)> writes:
> Is adding 0 to a pointer to non-void that is equal to NULL legal?
>
> int *p=NULL;
> p+=0;


No, it invokes undefined behavior. C99 6.5.6p4 says:

When an expression that has integer type is added to or subtracted
from a pointer, the result has the type of the pointer operand.
[...]
If both the pointer operand and the result point to elements of
the same array object, or one past the last element of the array
object, the evaluation shall not produce an overflow; otherwise,
the behavior is undefined.

There's no stated exception for adding 0. The C90 standard has
similar or identical wording.

On most implementations, the operation won't cause a trap, and the
result will compare equal to NULL -- which is, of course, a valid
consequence of undefined behavior.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Spoofed Existence (astalavista.net)
Guest
Posts: n/a
 
      09-10-2004

Christopher Benson-Manica wrote:
> Is adding 0 to a pointer to non-void that is equal to NULL legal?
>
> int *p=NULL;
> p+=0;
>
> --
> Christopher Benson-Manica | I *should* know what I'm talking about - if
> I
> ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
>


A pointer is nothiner else than a piece of memory with the right size to
store a memory address. Normally this is 4 bytes, but this may be different
on other different computers. You can do anything with this memory you can
do with other memory. This includes adding things to it - in this case 0.
The problem which make things illegal isn't that the pointer *is* NULL (=
0), but that the data the pointer points to is accessed. The pointer points
to the memory address 0, which can't be accessed.

So p += 0; would be valid, but it would keep the pointer to 0 (NULL). So
accessing this memory still is invalid. But with the pointer itself, you
can do anything. As long as the data the pointer points to isn't accessed.

But why not just use an "int" for example. Should you ever need the data it
points to, you can simply use:
(int*)theinteger




Hope that helps,
Spoofed Existence

--
This message has been sent using the astalavista.net newsreader
webinterface.
http://www.astalavista.net/
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      09-10-2004
Eric Sosman <(E-Mail Removed)> spoke thus:

> ... but I can't recall being tempted to do any such thing
> with a NULL value. What's up?


Well, the situation is something like the following:

void foo( char *buf, int bufsize )
{
bar( buf, buf?bufsize:0 );
/* Call other functions like bar, with the same parameters */
}

void bar( char *buf, int bufsize )
{
int bytes_remaining=bufsize;

bytes_remaining-=snprintf( buf+(bufsize-bytes_remaining), bytes_remaining, "Hello, world!\n" );

/* Call snprintf in the same way several more times */
}

snprintf() is C99, correct?

Assuming I've made no other errors here, this strikes me as being
fairly concise code. Of course, I could call bar as

bar( buf?buf:"", buf?bufsize:0 );

and eliminate the probable undefined behavior, but presumably nothing
catastrophic will occur on the particular real implementation I'm
using with the code as is.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Christian Bau
Guest
Posts: n/a
 
      09-10-2004
In article <chsudn$ssj$(E-Mail Removed)>,
Christopher Benson-Manica <(E-Mail Removed)> wrote:

> Is adding 0 to a pointer to non-void that is equal to NULL legal?
>
> int *p=NULL;
> p+=0;


It is undefined behavior.
 
Reply With Quote
 
Christian Bau
Guest
Posts: n/a
 
      09-10-2004
In article <cht1bu$set$(E-Mail Removed)-plus.net>,
"Spoofed Existence (astalavista.net)"
<(E-Mail Removed)> wrote:

Warning to every reader: What follows is absolutely wrong.

> A pointer is nothiner else than a piece of memory with the right size to
> store a memory address. Normally this is 4 bytes, but this may be different
> on other different computers. You can do anything with this memory you can
> do with other memory. This includes adding things to it - in this case 0.
> The problem which make things illegal isn't that the pointer *is* NULL (=
> 0), but that the data the pointer points to is accessed. The pointer points
> to the memory address 0, which can't be accessed.
>
> So p += 0; would be valid, but it would keep the pointer to 0 (NULL). So
> accessing this memory still is invalid. But with the pointer itself, you
> can do anything. As long as the data the pointer points to isn't accessed.


Please stop giving incorrect advice.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-10-2004
"Spoofed Existence (astalavista.net)"
<(E-Mail Removed)> writes:
> Christopher Benson-Manica wrote:
>> Is adding 0 to a pointer to non-void that is equal to NULL legal?
>>
>> int *p=NULL;
>> p+=0;

>
> A pointer is nothiner else than a piece of memory with the right size to
> store a memory address. Normally this is 4 bytes, but this may be different
> on other different computers.


4 bytes is a common size for pointers, but there's nothing abnormal
about other sizes.

> You can do anything with this memory you can
> do with other memory. This includes adding things to it - in this case 0.


You can do anything you want to with the *memory* (e.g., if you treat
it as an array of unsigned char), but the language limits what you can
do with it as a pointer. In particular, performing arithmetic on a
null pointer invokes undefined behavior, as has been explained
elsewhere in this thread.

> The problem which make things illegal isn't that the pointer *is* NULL (=
> 0), but that the data the pointer points to is accessed. The pointer points
> to the memory address 0, which can't be accessed.


No, the problem is precisely that it's a null pointer. As far as the
C language is concerned, a null pointer doesn't point to "memory
address 0"; it doesn't point to *anything*. (A null pointer is not
necessarily represented as all-bits-zero; see the FAQ.)

> So p += 0; would be valid, but it would keep the pointer to 0 (NULL). So
> accessing this memory still is invalid. But with the pointer itself, you
> can do anything. As long as the data the pointer points to isn't accessed.


That's incorrect. On many systems, p += 0; will result in p being
equal to NULL; that's just one of the many possible consequences of
undefined behavior.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      09-10-2004
Christopher Benson-Manica <(E-Mail Removed)> spoke thus:

> bar( buf?buf:"", buf?bufsize:0 );


Or, to post code that's actually legal,

if( !buf ) {
buf="";
bufsize=0;
}

Maybe I should A) go home, and B) never use the trinary operator in
function calls again. Sorry.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
2D arrays, pointers, and pointer arithmetic: question gdotone@gmail.com C Programming 32 09-07-2012 01:28 PM
pointer to pointer intialize to NULL but still point to NULL Christopher C++ 4 07-09-2011 12:35 AM
Arithmetic will null pointer Urs Thuermann C Programming 19 06-20-2010 03:53 AM
Null pointer (NULL array pointer is passed) aneuryzma C++ 3 06-16-2008 05:48 AM
confusion involving pointer to pointer when converting from derived to base Kavya C++ 2 12-23-2006 07:31 PM



Advertisments