Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Wrong

Reply
Thread Tools

Re: Wrong

 
 
Joshua Maurice
Guest
Posts: n/a
 
      04-17-2010
On Apr 17, 12:49*pm, "Leigh Johnston" <(E-Mail Removed)> wrote:
> A certain regular of this newsgroup thinks the following code is not wrong,
> discuss.
>
> void foo()
> {
> * * std::vector<int> v;
> * * v.reserve(2);
> * * v.push_back(41);
> * * *(&v[0]+1) = 42;
>
> }
>
> The actual definition of "wrong" may vary from individual to individual as
> does common sense so it seems.
>
> This may help:


If by wrong, you mean undefined behavior, then yes. The push_back is
fine, but the next line writes to an area which has been reserved but
not in the size. I know of several debug implementations of the
standard library which will crash horribly and report the error of the
code.
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      04-17-2010
Joshua Maurice wrote:

> On Apr 17, 12:49 pm, "Leigh Johnston" <(E-Mail Removed)> wrote:
>> A certain regular of this newsgroup thinks the following code is not
>> wrong, discuss.
>>
>> void foo()
>> {
>> std::vector<int> v;
>> v.reserve(2);
>> v.push_back(41);
>> *(&v[0]+1) = 42;
>>
>> }
>>
>> The actual definition of "wrong" may vary from individual to individual
>> as does common sense so it seems.
>>
>> This may help:

>
> If by wrong, you mean undefined behavior, then yes. The push_back is
> fine, but the next line writes to an area which has been reserved but
> not in the size.


a) Huh? At

*(&v[0]+1) = 42

the size is already 1, because of the previous push_back. So, I don't see a
write to something not in the size.

> I know of several debug implementations of the
> standard library which will crash horribly and report the error of the
> code.


That would be interesting. Even replacing 1 by 2:

*( &v[0] + 2 ) = 42

I am aware only of implementations that would catch v[2] = 42. The &v[0]+2
is effectively bypassing the library implementation since &v[0] returns a
plain pointer, which then is subject to ordinary pointer arithmetic. If an
implementation catches an out-of-bounds error here, it would not be because
of the way the _library_ is implemented.


Also: the pointer arithmetic is actually not out of bounds.


Upshot: I don't see undefined behavior in the code.


Best

Kai-Uwe Bux


 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      04-17-2010
Kai-Uwe Bux wrote:

> Joshua Maurice wrote:
>
>> On Apr 17, 12:49 pm, "Leigh Johnston" <(E-Mail Removed)> wrote:
>>> A certain regular of this newsgroup thinks the following code is not
>>> wrong, discuss.
>>>
>>> void foo()
>>> {
>>> std::vector<int> v;
>>> v.reserve(2);
>>> v.push_back(41);
>>> *(&v[0]+1) = 42;
>>>
>>> }
>>>
>>> The actual definition of "wrong" may vary from individual to individual
>>> as does common sense so it seems.
>>>
>>> This may help:

>>
>> If by wrong, you mean undefined behavior, then yes. The push_back is
>> fine, but the next line writes to an area which has been reserved but
>> not in the size.

>
> a) Huh? At
>
> *(&v[0]+1) = 42
>
> the size is already 1, because of the previous push_back. So, I don't see
> a write to something not in the size.


Oops, my fault: I cannot count


Best

Kai-Uwe Bux
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-18-2010
* Joshua Maurice:
> On Apr 17, 12:49 pm, "Leigh Johnston" <(E-Mail Removed)> wrote:
>> A certain regular of this newsgroup thinks the following code is not wrong,
>> discuss.
>>
>> void foo()
>> {
>> std::vector<int> v;
>> v.reserve(2);
>> v.push_back(41);
>> *(&v[0]+1) = 42;
>>
>> }
>>
>> The actual definition of "wrong" may vary from individual to individual as
>> does common sense so it seems.
>>
>> This may help:

>
> If by wrong, you mean undefined behavior, then yes. The push_back is
> fine, but the next line writes to an area which has been reserved but
> not in the size. I know of several debug implementations of the
> standard library which will crash horribly and report the error of the
> code.


Example?


Cheers,

- Alf
 
Reply With Quote
 
Joshua Maurice
Guest
Posts: n/a
 
      04-18-2010
On Apr 17, 8:36*pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> * Joshua Maurice:
>
>
>
> > On Apr 17, 12:49 pm, "Leigh Johnston" <(E-Mail Removed)> wrote:
> >> A certain regular of this newsgroup thinks the following code is not wrong,
> >> discuss.

>
> >> void foo()
> >> {
> >> * * std::vector<int> v;
> >> * * v.reserve(2);
> >> * * v.push_back(41);
> >> * * *(&v[0]+1) = 42;

>
> >> }

>
> >> The actual definition of "wrong" may vary from individual to individual as
> >> does common sense so it seems.

>
> >> This may help:

>
> > If by wrong, you mean undefined behavior, then yes. The push_back is
> > fine, but the next line writes to an area which has been reserved but
> > not in the size. I know of several debug implementations of the
> > standard library which will crash horribly and report the error of the
> > code.

>
> Example?


My mistake. I am incorrect. I thought visual studios debug iterators
would catch it. However, the undefined behavior line in question was
not using iterators but raw pointers. I was thinking of the following
example, which visual studios debug iterators do catch.

#include <vector>
using namespace std;
int main()
{
std::vector<int> v;
v.reserve(2);
v.push_back(41);
*(v.begin()+1) = 42;
}
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      04-18-2010
Alf P. Steinbach <(E-Mail Removed)> wrote:
>> If by wrong, you mean undefined behavior, then yes. The push_back is
>> fine, but the next line writes to an area which has been reserved but
>> not in the size. I know of several debug implementations of the
>> standard library which will crash horribly and report the error of the
>> code.

>
> Example?


MS Visual C++, when compiling in debug mode.

gcc when using the _GLIBCXX_DEBUG preprocessor macro.
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-18-2010
* Joshua Maurice:
> On Apr 17, 8:36 pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
>> * Joshua Maurice:
>>
>>
>>
>>> On Apr 17, 12:49 pm, "Leigh Johnston" <(E-Mail Removed)> wrote:
>>>> A certain regular of this newsgroup thinks the following code is not wrong,
>>>> discuss.
>>>> void foo()
>>>> {
>>>> std::vector<int> v;
>>>> v.reserve(2);
>>>> v.push_back(41);
>>>> *(&v[0]+1) = 42;
>>>> }
>>>> The actual definition of "wrong" may vary from individual to individual as
>>>> does common sense so it seems.
>>>> This may help:
>>> If by wrong, you mean undefined behavior, then yes. The push_back is
>>> fine, but the next line writes to an area which has been reserved but
>>> not in the size. I know of several debug implementations of the
>>> standard library which will crash horribly and report the error of the
>>> code.

>> Example?

>
> My mistake. I am incorrect. I thought visual studios debug iterators
> would catch it. However, the undefined behavior line in question was
> not using iterators but raw pointers.


Yes. I don't think it's undefined behavior (as I see it it's guaranteed and
well-defined, because the standard guarantees that there is a raw array of size
2 or larger, and, since it can't be put in place after the reserve call, that
that array is the buffer accessible via &v[0], and the standard also guarantees
pointer access of raw arrays). I think it's just bad practice, although like
"goto" there may be circumstances where it's The Lesser Evil(TM).


> I was thinking of the following
> example, which visual studios debug iterators do catch.
>
> #include <vector>
> using namespace std;
> int main()
> {
> std::vector<int> v;
> v.reserve(2);
> v.push_back(41);
> *(v.begin()+1) = 42;
> }


Yes.

For other readers: here the +1 advances the iterator to end(), instead of being
raw pointer arithmetic advancing a raw pointer.


Cheers,

- Alf
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-18-2010
* Juha Nieminen:
> Alf P. Steinbach <(E-Mail Removed)> wrote:
>>> If by wrong, you mean undefined behavior, then yes. The push_back is
>>> fine, but the next line writes to an area which has been reserved but
>>> not in the size. I know of several debug implementations of the
>>> standard library which will crash horribly and report the error of the
>>> code.

>> Example?

>
> MS Visual C++, when compiling in debug mode.
>
> gcc when using the _GLIBCXX_DEBUG preprocessor macro.


Would you care to present a complete little example, with actual result?


Cheers,

- Alf
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      04-18-2010
Alf P. Steinbach <(E-Mail Removed)> wrote:
> * Juha Nieminen:
>> Alf P. Steinbach <(E-Mail Removed)> wrote:
>>>> If by wrong, you mean undefined behavior, then yes. The push_back is
>>>> fine, but the next line writes to an area which has been reserved but
>>>> not in the size. I know of several debug implementations of the
>>>> standard library which will crash horribly and report the error of the
>>>> code.
>>> Example?

>>
>> MS Visual C++, when compiling in debug mode.
>>
>> gcc when using the _GLIBCXX_DEBUG preprocessor macro.

>
> Would you care to present a complete little example, with actual result?


Ok, you got me. I was thinking about actually indexing the vector past
its size, but the original code actually takes a raw pointer to the first
element and then uses pointer arithmetic to get past the size. Those
compilers I mentioned won't catch that as any kind of error, even in debug
mode.

Ok, I can't see why the given example code would misbehave (when using
basic types) in any system with any compiler with any compiler settings.
 
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
Have I bought wrong product? enquirer Wireless Networking 2 06-10-2005 10:59 PM
Zero Config keeps connecting to the wrong AP =?Utf-8?B?ZGdyaWZmaXRo?= Wireless Networking 2 03-04-2005 05:52 PM
Is XML Doc wrong or is Schema wrong? (or both) Matthew XML 7 01-07-2005 10:05 PM
wrong connection status Peter Welk Wireless Networking 0 12-22-2004 03:26 PM
XP SP2 Wrong IP on connection D Wells Wireless Networking 3 12-09-2004 03:35 AM



Advertisments