Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 786161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:59:43+00:00 2026-05-14T20:59:43+00:00

I am writing a test program to understand vector ‘s better. In one of

  • 0

I am writing a test program to understand vector‘s better. In one of the scenarios, I am trying to insert a value into the vector at a specified position. The code compiles clean. However, on execution, it is throwing a Segmentation Fault from the v8.insert(..) line (see code below). I am confused. Can somebody point me to what is wrong in my code?

#define UNIT_TEST(x) assert(x)
#define ENSURE(x) assert(x)

#include <vector>
typedef std::vector< int >                     intVector;
typedef std::vector< int >::iterator           intVectorIterator;
typedef std::vector< int >::const_iterator     intVectorConstIterator;


intVectorIterator find( intVector v, int key );
void test_insert();

intVectorIterator
find( intVector v, int key )
{
    for( intVectorIterator it = v.begin(); it != v.end(); ++it )
    {
        if( *it == key )
        {
            return it;
        }
    }

    return v.end();
}

void
test_insert()
{
    const int values[] = {10, 20, 30, 40, 50};
    const size_t valuesLength = sizeof( values ) / sizeof( values[ 0 ] );
    size_t index = 0;
    const int insertValue = 5;

    intVector v8;
    for( index = 0; index < valuesLength; ++index )
    {
        v8.push_back( values[ index ] );
    }

    ENSURE( v8.size() == valuesLength );
    for( index = 0; index < valuesLength; ++index )
    {
        printf( "index = %u\n", index );

        intVectorIterator it = find( v8, values[ index ] );
        ENSURE( it != v8.end() );
        ENSURE( *it == values[ index ] );

        // intVectorIterator itToInsertedItem = 
        v8.insert( it, insertValue );                      // line 51
        // UNIT_TEST( *itToInsertedItem == insertValue );
    }
}

int main()
{
    test_insert();
    return 0;
}

$ ./a.out
index = 0
Segmentation Fault (core dumped)

(gdb) bt
#0  0xff3a03ec in memmove () from /platform/SUNW,T5140/lib/libc_psr.so.1
#1  0x00012064 in std::__copy_move_backward<false, true, std::random_access_iterator_tag>::__copy_move_b<int> (__first=0x23e48, __last=0x23450, __result=0x23454)
    at /local/gcc/4.4.1/lib/gcc/sparc-sun-solaris2.10/4.4.1/../../../../include/c++/4.4.1/bits/stl_algobase.h:575
#2  0x00011f08 in std::__copy_move_backward_a<false, int*, int*> (__first=0x23e48, __last=0x23450, __result=0x23454)
    at /local/gcc/4.4.1/lib/gcc/sparc-sun-solaris2.10/4.4.1/../../../../include/c++/4.4.1/bits/stl_algobase.h:595
#3  0x00011d00 in std::__copy_move_backward_a2<false, int*, int*> (__first=0x23e48, __last=0x23450, __result=0x23454)
    at /local/gcc/4.4.1/lib/gcc/sparc-sun-solaris2.10/4.4.1/../../../../include/c++/4.4.1/bits/stl_algobase.h:605
#4  0x000119b8 in std::copy_backward<int*, int*> (__first=0x23e48, __last=0x23450, __result=0x23454) at /local/gcc/4.4.1/lib/gcc/sparc-sun-solaris2.10/4.4.1/../../../../include/c++/4.4.1/bits/stl_algobase.h:640
#5  0x000113ac in std::vector<int, std::allocator<int> >::_M_insert_aux (this=0xffbfeba0, __position=..., __x=@0xffbfebac)
    at /local/gcc/4.4.1/lib/gcc/sparc-sun-solaris2.10/4.4.1/../../../../include/c++/4.4.1/bits/vector.tcc:308
#6  0x00011120 in std::vector<int, std::allocator<int> >::insert (this=0xffbfeba0, __position=..., __x=@0xffbfebac)
    at /local/gcc/4.4.1/lib/gcc/sparc-sun-solaris2.10/4.4.1/../../../../include/c++/4.4.1/bits/vector.tcc:126
#7  0x00010bc0 in test_insert () at vector_insert_test.cpp:51
#8  0x00010c48 in main () at vector_insert_test.cpp:58
(gdb) q
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:59:44+00:00Added an answer on May 14, 2026 at 8:59 pm

    You’re passing v8 by value to your find function. The iterator returned thus points into the copy of the vector, which is out of scope after the find call returns. Try passing by (const) reference, or better yet, just use std::find.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.