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 7899931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:50:13+00:00 2026-06-03T08:50:13+00:00

I have this piece of code: while(fileStream>>help) { MyVector.push_back(help); } …and lets say that

  • 0

I have this piece of code:

    while(fileStream>>help)
    {
        MyVector.push_back(help);
    }

…and lets say that in file “fileStream” is 1 sentence: Today is a sunny day. Now when i do this:

    for(int i=0;i<MyVector.size();i++)
    {
         printf("%s\n", MyVector[i]);
    }

, the resoult is “day day day day day”. And, of course, it shouldnt be like that. Variables are declared like this:

    char *help;
    vector<char*> MyVector;

EDIT:i understand the answers, thank you…but is there any way to store words from file in vector<char*> MyVector(like i wanted in the first place). it would be great for the rest of my program.

  • 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-03T08:50:15+00:00Added an answer on June 3, 2026 at 8:50 am

    When you do fileStream>>help, that’s not changing the value of the pointer, it is overwriting the contents of the string that the pointer is pointing at. So you are pushing the same pointer into the vector over and over again. So all of the pointers in the vector point to the same string. And since the last thing written into that string was the word “day”, that is what you get when you print out each element of your vector.

    Use a vector of std::string instead:

    string help;
    vector<string> MyVector;
    

    If, as you say, you must stick with vector<char*>, then you will have to dynamically allocate a new string for each element. You cannot safely use a char* with operator>>, because there is no way to tell it how much space you actually have in your string, so I will use std::string for at least the input.

    std::string help;
    vector<char*> MyVector;
    
    while (fileStream>>help) {
        char * p = new char[help.size() + 1];
        strcpy(p, help.c_str());
        MyVector.push_back(p);
    }
    

    Of course, when you are done with the vector, you can’t just let it go out of scope. You need to delete every element manually in a loop. However, this is still not completely safe, because your memory allocation could throw an exception, causing whatever strings you have already allocated and put in the vector to leak. So you should really wrap this all up in a try block and be ready to catch std::bad_alloc.

    This is all a lot of hassle. If you could explain why you think you need to use vector<char*>, I bet someone could show you why you don’t.

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

Sidebar

Related Questions

I was wondering about something. Lets say I have this piece of code: if
I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();
I have this piece of code that works fine in subsonic 2.2, I migrated
I have been struggling with this piece of code for a while and I
I have this piece of code that is supposed to insert data in a
I have this piece of code, but the values may change while someone is
I have this piece of code $.get('/proc.php?proc=get_color', function(data){ $('#offer_color').html(data); }); that is hosted on
I have this piece of code (summarized)... AnsiString working(AnsiString format,...) { va_list argptr; AnsiString
I have this piece of code #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h>
I have this piece of code: $(#faq).click(function () { var url = $.get(faq, {

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.