On 6/9/05, Erik de Castro Lopo <erikd-lad(a)mega-nerd.com> wrote:
David Cournapeau wrote:
eviltwin69(a)cableone.net wrote:
I was under the impression that there was bounds checking going on with
vectors. Is this not the case?
Not necesserally: if you are using operator (), yes, if you use operator
[], no.
I think you are all guessing.
No, I am not. I cannot find the information on the C++ faq right now,
but If m pretty sure that it is written in the book of Stroustrup.
I did a small test mself: if you check the assembly code of g++ for
the functions
void c_array(int * a)
{
a[5]
}
void vector(vector<int> a)
{
a[5]
}
void vector(vector<int> a)
{
a.at(5)
}
you will see that the assembly code for c_array and vector are quite
similar ( I don't know much about assembly, but here is the output of
g++ -O3 -S for c_array :
.file "c_array.cpp"
.text
.align 2
.p2align 4,,15
.globl _Z7c_arrayPi
.type _Z7c_arrayPi, @function
_Z7c_arrayPi:
.LFB529:
pushl %ebp
.LCFI0:
movl %esp, %ebp
.LCFI1:
movl 8(%ebp), %edx
movl $0, 20(%edx)
popl %ebp
ret
.LFE529:
.size _Z7c_arrayPi, .-_Z7c_arrayPi
.weak pthread_mutex_unlock
.weak pthread_mutex_trylock
.weak pthread_mutex_lock
.weak pthread_create
.weak pthread_setspecific
.weak pthread_getspecific
.weak pthread_key_delete
.weak pthread_key_create
.weak pthread_once
.section .note.GNU-stack,"",@progbits
.ident "GCC: (GNU) 3.3.6 (Debian 1:3.3.6-5)"
and for void vector(vector<int> v)
.file "vector.cpp"
.text
.align 2
.p2align 4,,15
.globl _Z6vectorSt6vectorIiSaIiEE
.type _Z6vectorSt6vectorIiSaIiEE, @function
_Z6vectorSt6vectorIiSaIiEE:
.LFB539:
.L2:
.L7:
pushl %ebp
.LCFI0:
movl %esp, %ebp
.LCFI1:
popl %ebp
ret
.LFE539:
.size _Z6vectorSt6vectorIiSaIiEE, .-_Z6vectorSt6vectorIiSaIiEE
.weak pthread_mutex_unlock
.weak pthread_mutex_trylock
.weak pthread_mutex_lock
.weak pthread_create
.weak pthread_setspecific
.weak pthread_getspecific
.weak pthread_key_delete
.weak pthread_key_create
.weak pthread_once
.section .note.GNU-stack,"",@progbits
.ident "GCC: (GNU) 3.3.6 (Debian 1:3.3.6-5)"
The assembly code using at() instead of [] is much longer.
Cheers,
David