C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

decrementing vector begin() iterator for sane code allowed?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
bert hubert
Guest





PostPosted: Tue Sep 21, 2004 6:30 pm    Post subject: decrementing vector begin() iterator for sane code allowed? Reply with quote



Hi people,

I have some code that I've been able to shorten dramatically by decrementing
the begin() iterator of a vector.

I have a vector of 'things' that need to be traversed in both directions.

To this end I have 'getNext' and 'getPrevious' methods. If getNext is called
at the end of the vector, the database is consulted to get more things, and
the vector is refilled. Same for getPrevious.

getNext() currently returns *(++d_pos), getPrevious returns *(--d_pos).
However, in order to keep things simple, the refill method does:

d_pos = d_events.begin() - 1;

So getNext does the right thing automatically. getPrev checks if it needs to
get the vector refilled with if(d_pos <= d_events.begin()), before
decrementing.

This all works like a charm with g++ 3.4 but I'm wondering if this is
allowed. From C I recall that this kind of thing with arrays was explicitly
allowed, as long as the user does not dereference array[-1].

But C is not C++ (unlike all those job vacancies asking for people fluent in
"C/C++"), so I'm not sure if what I'm doing is safe.

Thanks.

--
http://www.PowerDNS.com/ Open Source Database Driven Nameserver
http://lartc.org Linux Advanced Routing & Traffic Control HOWTO

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Alberto Barbati
Guest





PostPosted: Tue Sep 21, 2004 10:48 pm    Post subject: Re: decrementing vector begin() iterator for sane code allow Reply with quote



bert hubert wrote:

Quote:

This all works like a charm with g++ 3.4 but I'm wondering if this is
allowed.

It's not (it's implicit in 24.1.4/1).

Quote:
From C I recall that this kind of thing with arrays was explicitly
allowed, as long as the user does not dereference array[-1].

It's not allowed neither in C (6.5.6/8 of the C Standard) nor in C++
(5.7/5 of C++ standard, the wording is practically the same).

Sorry about that, you will have to use another trick.

Alberto

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
bert hubert
Guest





PostPosted: Wed Sep 22, 2004 9:51 am    Post subject: Re: decrementing vector begin() iterator for sane code allow Reply with quote



On 2004-09-21, Alberto Barbati <AlbertoBarbati (AT) libero (DOT) it> wrote:
Quote:
bert hubert wrote:


This all works like a charm with g++ 3.4 but I'm wondering if this is
allowed.

It's not (it's implicit in 24.1.4/1).

Hmm, well, I don't see this, but something like this would have to be
explicitly allowed, not implicitly possible. Thanks.

Quote:
It's not allowed neither in C (6.5.6/8 of the C Standard) nor in C++
(5.7/5 of C++ standard, the wording is practically the same).

I must have confused gcc and c again.

Quote:
Sorry about that, you will have to use another trick.

Thanks.

--
http://www.PowerDNS.com/ Open Source Database Driven Nameserver
http://lartc.org Linux Advanced Routing & Traffic Control HOWTO

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Wed Sep 22, 2004 6:25 pm    Post subject: Re: decrementing vector begin() iterator for sane code allow Reply with quote

bert hubert <ahu (AT) ds9a (DOT) nl> wrote


Quote:
I have some code that I've been able to shorten dramatically by
decrementing the begin() iterator of a vector.

I have a vector of 'things' that need to be traversed in both
directions.


Quote:
To this end I have 'getNext' and 'getPrevious' methods. If getNext is
called at the end of the vector, the database is consulted to get more
things, and the vector is refilled. Same for getPrevious.

getNext() currently returns *(++d_pos), getPrevious returns
*(--d_pos). However, in order to keep things simple, the refill method
does:

d_pos = d_events.begin() - 1;

This is undefined behavior. Anything may happen. With some
implementations, it WILL cause an assertion failure or an exception (I'm
not sure which). With some of my pre-standard containers, its
equivalent would have been a no-op.

Quote:
So getNext does the right thing automatically. getPrev checks if it
needs to get the vector refilled with if(d_pos <= d_events.begin()),
before decrementing.

This all works like a charm with g++ 3.4 but I'm wondering if this is
allowed. From C I recall that this kind of thing with arrays was
explicitly allowed, as long as the user does not dereference
array[-1].

You recall wrong. This was undefined behavior in C, and is undefined
behavior for C style arrays in C++.

Quote:
But C is not C++ (unlike all those job vacancies asking for people
fluent in "C/C++"), so I'm not sure if what I'm doing is safe.

Actually, for C style arrays, C and C++ have exactly the same rules.
Pointers into the array, or one past the end, are legal, but anything
which creates a pointer outside of that area is undefined behavior.

C++ containers simply generalized the same idiom.

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Fri Sep 24, 2004 11:26 am    Post subject: Re: decrementing vector begin() iterator for sane code allow Reply with quote

bert hubert <ahu (AT) ds9a (DOT) nl> wrote


Quote:
On 2004-09-21, Alberto Barbati <AlbertoBarbati (AT) libero (DOT) it> wrote:
bert hubert wrote:

This all works like a charm with g++ 3.4 but I'm wondering if this is
allowed.

It's not (it's implicit in 24.1.4/1).

Hmm, well, I don't see this, but something like this would have to be
explicitly allowed, not implicitly possible. Thanks.

It's not allowed neither in C (6.5.6/8 of the C Standard) nor in
C++ (5.7/5 of C++ standard, the wording is practically the same).

I must have confused gcc and c again.

I think that in this case, they are the same. The standard says
undefined behavior, which can mean anything. The "classical"
implementation is just to do whatever the hardward happens to give you.
On most hardware, there's no problem, and it will work.

At least with T[]. With vector<T>, it is a bit different, because there
exist debugging implementations of the standard library, which will
verify. Historically, the C standard was formulated intentionally in a
way to allow debugging implementations to use "fat" pointers, and trap
such accesses. I think that there was once an implementation,
Centerline, which actually did this. But it certainly isn't typical.
(One of the problems with such an implementation, of course, is
interfacing with external libraries.)

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.