From -5573826933315481866
X-Google-Thread: 7894ca11fe,e2347147b98ac8c4,start
X-Google-Attributes: gid7894ca11fe,public,usenet
X-Google-NewGroupId: yes
X-Google-Language: ENGLISH,ASCII
Path: g2news2.google.com!news1.google.com!news.glorb.com!news.alt.net!frodo.cs.rpi.edu!not-for-mail
From: =?ISO-8859-1?Q?Joaqu=EDn_M_L=F3pez_Mu=F1oz?= <joaquin@tid.es>
Newsgroups: comp.std.c++
Subject: Adding 0 to the null pointer
Date: Tue, 26 May 2009 10:37:43 CST
Organization: http://groups.google.com
Lines: 60
Sender: cppmods@cs.rpi.edu
Approved: stephen.clamage@sun.com
Message-ID: <7c71cedf-287b-4fed-a4b0-807160d2e59e@g1g2000yqh.googlegroups.com>
NNTP-Posting-Host: netlab.cs.rpi.edu
Content-Type: text/plain; charset=ISO-8859-1
To: (Usenet)
Return-Path: <cppmods@ruralroute.cs.rpi.edu>
X-Original-Date: Mon, 25 May 2009 12:54:18 -0700 (PDT)
X-Submission-Address: std-c++@netlab.cs.rpi.edu
Xref: g2news2.google.com comp.std.c++:725

Hello,

Is the following a legal C++ program (note we're passing 0 as
the third argument to std::copy)?

   #include <algorithm>

   int main()
   {
     int x,*p=&x;
     std::copy(p,p,(int*)0);
   }

The (C++03) standard does not say whether result in
std::copy(first,last,result)  must be a valid pointer, and only
states that the algorithm returns result+ (last-first), which in
our particular case (first==last) is result+0. Regarding this
latter expression, 5.7/8 [expr.add] says:

   "If the value 0 is added to or subtracted from a pointer value,
   the result compares equal to the original pointer value [...]"

without regard to whether the pointer being added or
subtracted 0 is the null pointer or not. So, it seems to me that
result+0 is indeed valid and yields result, i.e. the null value of
int*.

To summarize, I think the program above is indeed valid, but
some stdlib implementers dissent: the implementation of
std::copy for MSVC 9.0 has roughly the following checking code
(when parameter debugging is activated):

   _DEBUG_RANGE(first, last);
   if (first != last)
     _DEBUG_POINTER(result);

which accepts the program above as valid, whereas the
implementer changed her mind in MSVC 10.0 and modified
the checking code as

   _DEBUG_RANGE(first, last);
   _DEBUG_POINTER(result);

where  _DEBUG_POINTER triggers and assertion when
result is a null pointer, thus failing on the program above.

So, who's right, I (and MSVC 9.0) or MSVC 10.0?

Thank you,

Joaqu�n M L�pez Mu�oz
Telef�nica, Investigaci�n y Desarrollo


-- 
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]



