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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:27:40+00:00 2026-05-27T05:27:40+00:00

I am practicing using pointers and stumbled upon something I don’t understand. The program

  • 0

I am practicing using pointers and stumbled upon something I don’t understand. The program does this:

  1. Create a vector
  2. Pass vector’s address to a function
  3. That function has a for-loop
  4. In that for-loop a user is asked for a movie name
  5. Upon receiving the movie name, a new movie object is made (from struct)
  6. For each movie a new boost thread is created, passing the title the user made up and a pointer for both the new movie object and the vector.
  7. In the boost thread, the movie object’s “title” variable is assigned the title the user made up and the movie is then added to the vector
  8. When all threads are done, a for-loop inside the “main” function shows all movie titles stored in the vector.

The problem occurs when I swap these two

//Get info about new movie from user
string movieTitle;
int movieYear; //Not used at the moment
cout << "Enter title for movie " << (i+1) << endl;
getline(cin,movieTitle);

and

//Create new movie
movies_t amovie;
movies_t * pmovie;
pmovie = &amovie;

When I put the “user input part” above the “create a new movie” part, like it is right now, I get this:

good

But when I swap them:

bad

I don’t understand why because they aren’t affecting each other at all.

The data is shown like this:

for(i=0;i<movieVector.size();i++)
{
    cout << movieVector[i].title << endl;
}

These are the relevant functions (main and newThreads)

void newThreads(vector<movies_t> *movieVectorPointer)
{
    boost::thread_group group; //Start thread group

    int i;
    for(i=0; i<2; i++) //Make X amount of threads (2 in this case)
    {
        //Get info about new movie from user
        string movieTitle;
        int movieYear; //Not used at the moment
        cout << "Enter title for movie " << (i+1) << endl;
        getline(cin,movieTitle);

        //Create new movie
        movies_t amovie;
        movies_t * pmovie;
        pmovie = &amovie;

        //Let user know we are now starting on this thread
        cout << "Doing thread " << i << endl;

        //Start new thread
        newThreadStruct startNewThread(movieTitle,movieYear,pmovie,movieVectorPointer);
        group.create_thread(startNewThread);
    }
    group.join_all(); //Wait for all threads in group to finish
}

int main()
{
    cout << "Hello world!" << endl; //I am born.

    vector<movies_t> movieVector; //Create vector to store movies in
    newThreads(&movieVector); //Start making new threads. Pass movieVector's address so it can be used within threads.

    /* The below code will not be executed until all threads are done */

    cout << "Amount of movies " << movieVector.size() << endl; //Let user know how many movies we made

    //Show all movies we made
    int i;
    for(i=0;i<movieVector.size();i++)
    {
        cout << movieVector[i].title << endl;
    }

    cout << "Bye world!" << endl; //This life has passed.
    return 0;
}

And here is the full code, in case it matters:

#include <iostream>
#include <boost/thread.hpp>

using namespace std;

//A movie will hold a title and a year. Only title is used in this code.
struct movies_t {
    string title;
    int year;
};

//This function is where the data is added to our new movie,a fter which our finished movie will be added to the vector. It is called from within the "newThreadStruct" operator.
int doMovieWork(string movieTitle,int movieYear,movies_t *moviePointer,vector<movies_t> *movieVector)
{
    moviePointer->title = movieTitle; //Add title to new movie
    movieVector->push_back(*moviePointer); //Add movie to vector
    return 0;
};

//This struct will be used to create new Boost threads. It accepts various arguments.
struct newThreadStruct
{
    newThreadStruct(string movieTitle,int movieYear,movies_t *moviePointer,vector<movies_t> *movieVector) : movieTitle(movieTitle),movieYear(movieYear),moviePointer(moviePointer),movieVector(movieVector) { }

    void operator()()
    {
    doMovieWork(movieTitle,movieYear,moviePointer,movieVector);
    }
    string movieTitle;
    int movieYear;
    movies_t *moviePointer;
    vector<movies_t> *movieVector;
};

void newThreads(vector<movies_t> *movieVectorPointer)
{
    boost::thread_group group; //Start thread group

    int i;
    for(i=0; i<2; i++) //Make X amount of threads (2 in this case)
    {
    //Get info about new movie from user
    string movieTitle;
    int movieYear; //Not used at the moment
    cout << "Enter title for movie " << (i+1) << endl;
    getline(cin,movieTitle);

    //Create new movie
    movies_t amovie;
    movies_t * pmovie;
    pmovie = &amovie;

    //Let user know we are now starting on this thread
    cout << "Doing thread " << i << endl;

    //Start new thread
    newThreadStruct startNewThread(movieTitle,movieYear,pmovie,movieVectorPointer);
    group.create_thread(startNewThread);
    }
    group.join_all(); //Wait for all threads in group to finish
}

int main()
{
    cout << "Hello world!" << endl; //I am born.

    vector<movies_t> movieVector; //Create vector to store movies in
    newThreads(&movieVector); //Start making new threads. Pass movieVector's address so it can be used within threads.

    /* The below code will not be executed until all threads are done */

    cout << "Amount of movies " << movieVector.size() << endl; //Let user know how many movies we made

    //Show all movies we made
    int i;
    for(i=0;i<movieVector.size();i++)
    {
    cout << movieVector[i].title << endl;
    }

    cout << "Bye world!" << endl; //This life has passed.
    return 0;
}
  • 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-27T05:27:41+00:00Added an answer on May 27, 2026 at 5:27 am

    The order in which you get user input and initialize the movies_t object is a red herring. The actual problem is here:

    //Create new movie
    movies_t amovie;
    movies_t * pmovie;
    pmovie = &amovie;
    
    /* ... */
    
    //Start new thread
    newThreadStruct startNewThread(movieTitle,movieYear,pmovie,movieVectorPointer);
    group.create_thread(startNewThread);
    

    You are passing the address of a local variable (amovie) to a thread. You have no direct control over when this thread starts, when it tries to access the pointer you’ve passed it, or when it exits. But you don’t wait in the main thread loop for the worker thread to use the local. As soon as the loop loops, the variable you’ve passes falls out of scope. When the worker thread tries to use it, you invoke undefined behavior. This is very bad.

    Probably the simplest (and most naive) way to fix this, is to dynamically create the amovie object:

    //Create new movie
    movies_t * pmovie = new movies_t;
    

    …and then when you’re done using it, delete it somewhere. From my initial inspection of your code, where to delete it was not immediately obvious — but it could be at the end of main.

    This naive approach opens a huge can of worms with respect to pointer ownership, memory management and potentially deadlocking and race conditions. You have now entered the realm of multithreadded programming — one of the most difficult things to do correctly there is in C++ programming. The naieve approach above will “work” (as in keep your code from crashing), albiet not without flaws — but if you’re going down the road of multithreadded programming, it’s time to start studying how to do it right. That is way beyond the scope of my little post here.

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

Sidebar

Related Questions

I'm practicing some XSL and using this XML document as a simple example: <?xml
I am practicing using pointers. I have a pointer to an array of 3
im trying to create a basic profile for someone and am practicing using the
I am practicing this RoR tutorial project of Michael Hartl: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book I am using
Alright I am practicing using cURL to login to different webservices. For this pariticular
I just started practicing TDD in my projects. I'm developing a project now using
I've been practicing for an upcoming programming competition and I have stumbled across a
I'm practicing Jquery and I've written this simple Jquery statement: var someText = $(table
I'm using g++ on fedora linux 13. I'm just practicing some exercises from my
I'm practicing on Django and using some online tutorial to build a web blog.

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.