Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C++ Programming

Address of vector question


I recently have upgraded compilers and my new one enables iterator
checking. When a vector is empty, &v[0] blows up, as it should I
guess. In replacing these I have been torn in using

&*v.begin() or &v.front()

Does anyone prefer one over the other?  Why?

On 29 Maj, 16:30, McGragger <shawn.gr@gmail.com> wrote:

> I recently have upgraded compilers and my new one enables iterator
> checking. When a vector is empty, &v[0] blows up, as it should I
> guess. In replacing these I have been torn in using

> &*v.begin() or &v.front()

> Does anyone prefer one over the other?  Why?

I'd use front() since you then don't create a new iterator on each
call. I hope you are aware of the fact that that &front() is no better
than &v[0] if the vector is empty, and remember the vector may
relocate after any insert.

--
Erik Wikstrm

On 29 Maj, 16:36, Erik Wikstrm <eri@student.chalmers.se> wrote:

> On 29 Maj, 16:30, McGragger <shawn.gr@gmail.com> wrote:

> > I recently have upgraded compilers and my new one enables iterator
> > checking. When a vector is empty, &v[0] blows up, as it should I
> > guess. In replacing these I have been torn in using

> > &*v.begin() or &v.front()

> > Does anyone prefer one over the other?  Why?

> I'd use front() since you then don't create a new iterator on each
> call. I hope you are aware of the fact that that &front() is no better
> than &v[0] if the vector is empty, and remember the vector may
> relocate after any insert.

Actually, on my platform (VS2005) front() is implemented as 'return
*begin();', so it's about as efficient as &*v.begin(). This, of
course, is implementation dependant.

--
Erik Wikstrm

On May 29, 10:40 am, Erik Wikstrm <eri@student.chalmers.se> wrote:

I am aware of the bigger issue here but I am simply trying to upgrade
code that has worked for a very long time. Thanks for your reply. I
think &v.front() is a little more natural.

"Erik Wikstrm"
< I hope you are aware of the fact that that &front() is no better
than &v[0] if the vector is empty,

There is 1 less function call parameter with front().

 Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------        
                http://www.usenet.com

McGragger wrote:
> I recently have upgraded compilers and my new one enables iterator
> checking. When a vector is empty, &v[0] blows up, as it should I
> guess. In replacing these I have been torn in using

> &*v.begin() or &v.front()

> Does anyone prefer one over the other?  Why?

When vector is empty v.begin() equals to v.end() and you know you should
never dereference v.end() right? (ie *v.begin() when v.empty() is UB).

v.front() has the same problem when v.empty() it's UB.

What is the correct version? Well something along the lines of:
"v.empty()?0:&v[0]" (that assuming that you want to get a 0/NULL pointer
when v is empty).

--
Dizzy

On 2007-05-29 07:30:31 -0700, McGragger <shawn.gr@gmail.com> said:

> I recently have upgraded compilers and my new one enables iterator
> checking. When a vector is empty, &v[0] blows up, as it should I
> guess.

If the vector is empty, then *all* of these invoke undefined behavior,
and should not be used.

> In replacing these I have been torn in using

> &*v.begin() or &v.front()

> Does anyone prefer one over the other?  Why?

Either check to make sure that the vector isn't empty first:
        v.empty()?NULL:&v.front()
or use &v.at(0) and deal with the exception that it can throw.

--
Clark S. Cox III
clarkc@gmail.com

On 29 Maj, 16:30, McGragger <shawn.gr@gmail.com> wrote:

> I recently have upgraded compilers and my new one enables iterator
> checking. When a vector is empty, &v[0] blows up, as it should I
> guess. In replacing these I have been torn in using

> &*v.begin() or &v.front()

> Does anyone prefer one over the other?  Why?

Use what Clark Cox suggests - or use v.data() which will do the same
thing. Unfortunately, this one is only available in C++ 0x unless I'm
mistaken, so your compiler might not yet support it.

/Peter

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc