Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

vector

65 views
Skip to first unread message

Rosario1903

unread,
Nov 28, 2013, 4:59:07 AM11/28/13
to

why the below goes to seg fault?

#include <iostream>
#include <vector>
using namespace std;

void f(vector<unsigned>& v)
{v[3]=4; v[4]=9;}


int main(void)
{vector <unsigned> mv;

mv[0]=1; mv[1]=2;
f(mv);
cout<<"mv[0]="<<mv[0]<<", mv[1]="<<mv[1]<<"\n";
cout<<"mv[3]="<<mv[3]<<", mv[4]="<<mv[4]<<" mv[5]="<<mv[5]<<"\n";

return 0;
}

guinne...@gmail.com

unread,
Nov 28, 2013, 5:18:22 AM11/28/13
to
On Thursday, 28 November 2013 09:59:07 UTC, Rosario1903 wrote:
> why the below goes to seg fault?
>
> #include <iostream>
> #include <vector>
> using namespace std;
>
> void f(vector<unsigned>& v)
> {v[3]=4; v[4]=9;}
>
>
> int main(void)
> {vector <unsigned> mv;

vector<unsigned> mv(6); // or more

>
> mv[0]=1; mv[1]=2;
> f(mv);
> cout<<"mv[0]="<<mv[0]<<", mv[1]="<<mv[1]<<"\n";
> cout<<"mv[3]="<<mv[3]<<", mv[4]="<<mv[4]<<" mv[5]="<<mv[5]<<"\n";
>
> return 0;
> }

When you declare a vector, with no construction arguments, it contains
exactly zero elements. Elements mv[0], mv[1], etc. do not yet exist.
This is why you get a seg-fault when trying to access them.

You can (as in my example, above) pre-allocate a number of
default-initialised elements. Or you can "manually" change the
size of the vector (using its resize() member function) or use
any of the insert()/push_back() member functions to add elements
to the vector.

K. Frank

unread,
Nov 28, 2013, 11:27:00 AM11/28/13
to
Hello Rosario!

On Thursday, November 28, 2013 4:59:07 AM UTC-5, Rosario1903 wrote:
> why the below goes to seg fault?
> ...
> void f(vector<unsigned>& v)
> {v[3]=4; v[4]=9;}
> ...
> {vector <unsigned> mv;
> mv[0]=1; mv[1]=2;
> f(mv);

As guinne explained, vector mv starts off with no
elements, so you're accessing non-existent elements,
which can lead to a seg fault.

As a side note, for additional safety, you can access
elements using the "at" member function:

v.at(3)=4; v.at(4)=9;

"At" throws a std::out_of_range exception if the vector
doesn't contain the index being accessed. (For maximum
efficiency, "[]" doesn't perform this check.)


Good luck!


K. Frank

Rosario1903

unread,
Nov 28, 2013, 1:12:32 PM11/28/13
to
On Thu, 28 Nov 2013 08:27:00 -0800 (PST), "K. Frank" wrote:
>Hello Rosario!
>
>On Thursday, November 28, 2013 4:59:07 AM UTC-5, Rosario1903 wrote:
>> why the below goes to seg fault?
>> ...
>> void f(vector<unsigned>& v)
>> {v[3]=4; v[4]=9;}
>> ...
>> {vector <unsigned> mv;
>> mv[0]=1; mv[1]=2;
>> f(mv);
>
>As guinne explained, vector mv starts off with no
>elements, so you're accessing non-existent elements,
>which can lead to a seg fault.

yes i thought that index allocate mem...

>As a side note, for additional safety, you can access
>elements using the "at" member function:
>
> v.at(3)=4; v.at(4)=9;

thank you

Rosario1903

unread,
Nov 28, 2013, 1:18:03 PM11/28/13
to
On Thu, 28 Nov 2013 02:18:22 -0800 (PST), guinness.tony wrote:

ok thanks
0 new messages