473,396 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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?

May 29 '07 #1
7 1476
On 29 Maj, 16:30, McGragger <shawn.gr...@gmail.comwrote:
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 Wikström

May 29 '07 #2
On 29 Maj, 16:36, Erik Wikström <eri...@student.chalmers.sewrote:
On 29 Maj, 16:30, McGragger <shawn.gr...@gmail.comwrote:
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 Wikström

May 29 '07 #3
On May 29, 10:40 am, Erik Wikström <eri...@student.chalmers.sewrote:
On 29 Maj, 16:36, Erik Wikström <eri...@student.chalmers.sewrote:
On 29 Maj, 16:30, McGragger <shawn.gr...@gmail.comwrote:
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 Wikström
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.

May 29 '07 #4

"Erik Wikström"
< 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
May 29 '07 #5
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

May 29 '07 #6
On 2007-05-29 07:30:31 -0700, McGragger <sh*********@gmail.comsaid:
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
cl*******@gmail.com

May 29 '07 #7
On 29 Maj, 16:30, McGragger <shawn.gr...@gmail.comwrote:
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

May 29 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to...
14
by: Dennis | last post by:
Hi I have a little problem as stated above (haven't found any solution on the www). I have a list of objects (particles) where I have a member of a template vector (array1d from TNT). I...
3
by: Kai Wu | last post by:
Hello, It turns out that STL vector does not assure the constness of its stored element address (memory location), i guess by the time it dynamicly allocates new memory as new element get...
15
by: dandelion | last post by:
Hi, Just another question for the standards jockeys... Suppose I have an Interrupt Vector Table located at address 0x0000 (16-bit machine). I want to dump the context of the IVT, by treating...
7
by: Stein Gulbrandsen | last post by:
Is this legal C++? or am I dereferencing posssibly invalid addresses here? #include <vector> struct C{}; int main () { C* c; C* a;
2
by: noelloen | last post by:
hi, I have the following code, int ret; char ** vector; //vector = "ls" vector ="-al" ..... //fork a child ......
2
by: Dinesh P | last post by:
Hello , I am writing a interrupt service routine for a vectored interrupt(vectored address 0x0056). Now I want to load this subroutine to start from the address(0x0056). Please help me out ...
36
by: Ajay | last post by:
Hi All, I got a segmentation fault while accessing a structure element. I pasted the output from gdb and the backtrace. However if I am able successfully access the structure using the...
7
by: e2point | last post by:
hi, i got a program that is suppose to run 24x7x365. However after functioning for around 15 minutes, it crashes due to a segmentation fault. program is written in c++ and runs in RH Linux 4. When...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.