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

  • SEARCH
  • Home
  • 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 8383127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:05:11+00:00 2026-06-09T17:05:11+00:00

I am attempting to use iterators to walk over a vector<char*> in c++. I

  • 0

I am attempting to use iterators to walk over a vector<char*> in c++. I have built a dummy program that is supposed to start at the end, and step backward (toward the beginning, or rend()) on a number >0, and forward (toward the end, or rbegin()) on a number <0, and exit on 0. if the iterator has reached either of the ends and the user attempts to step further, it should repeat the element at that end and not move the iterator. My problem is that, rather than doing that, if the user tries to run over the end, I just get a segfault. here’s my code:

#include <iostream>
#include <vector>
#include <stdio.h>

using namespace std;

int main(){
    vector<char*> vect;
    char* tmp;
    for (int i=1; i<=5; i++){
        tmp = new char[7];
        sprintf(tmp, "hello%d", i);
        vect.push_back(tmp);
    }

    vector<char*>::const_reverse_iterator it = vect.rbegin();

    int a;
    cin >> a;

    while (a!=0){
        if (a>0){
            if (it < vect.rend()){
                cout << *(++it) << endl;
            } else{
                cout << *it << endl;
            }
        } else{
            if (it > vect.rbegin()){
               cout << *(--it) << endl;
            } else{
                cout << *it << endl;
            }
        }
        cin >> a;
    }

    return 0;
}

Can anyone identify the problem?

EDIT

I forgot that I made a minor change. my previous code did not populate tmp in the initializing for loop. that has been fixed

  • 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-06-09T17:05:13+00:00Added an answer on June 9, 2026 at 5:05 pm

    The problem is that the rend iterator points one item past the (reversed) end of sequence. Dereferencing it causes a segfault:

        if (it < vect.rend()){
            cout << *(++it) << endl;
        } else{
            cout << *it << endl;    // <---- segfault
        }
    

    A minimal fix could be

    if (it+1 < vect.rend())
    {
        cout << *(++it) << endl;
    } else{
        cout << *it << endl;   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to make a small program that processes INI files, for use in
In attempting to use std::select1st from <functional> in a VS2008 project I found that
I have been attempting to use ASP.NET MVC remote validation for username login access
I am attempting to write a interpreter that will turn a string like: vector(random(0,
I am attempting to use XMLParse against content that is not valid xhtml. In
Attempting to use asp.net mvc's Action Result of File. So it would seem that
I'm attempting to use std::find_if on a std::map, looking for a particular object that
I am attempting use Java Pathfinder and I have pathfinder working. import gov.nasa.jpf.jvm.Verify; user.java:2:
While attempting to use the CopyFile() function I have encountered a strange error. It
I am attempting to use Cloud9 IDE for the first time, and start a

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.