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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:03:55+00:00 2026-05-21T16:03:55+00:00

so I have a vector of pairs of pointers in c++: vector<pair<Move *,Piece *>

  • 0

so I have a vector of pairs of pointers in c++:

vector<pair<Move *,Piece *> > moveList;

where Move is an object and Piece is an object…
Piece has the class variables type and side

so I add stuff to moveList:

    pair <Move *, Piece *> pr (&m,&(p));


    moveList.push_back(pr);

where m is a Move object and p is a Piece object

but whenever I call the moveList.back(), method, for some reason it would modify the values of Piece

so I do

Move * j = moveList.back().first;

Piece should have its "type" variable’s value set to ‘X’

but when I debug, it turns out that right after the line above, for some reason, Piece’s "type" variable’s value gets set to some crazy number such as -56 ‘\310’…..

what am I doing wrong?

EDIT

also moveList is set as a class variable

and the pushing into moveList and the getting the back() of moveList were done on different methods in that class

  • 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-21T16:03:55+00:00Added an answer on May 21, 2026 at 4:03 pm

    As others have pointed out, it’s seems you may be holding pointers to objects on the stack. These objects will go out of scope once you exit the function/block. Since STL containers manage their memory internally, one approach could be to change the vector to hold objects directly instead of pointers.

    vector <pair <Move, Piece> > moveList;
    // To insert
    moveList.push_back (make_pair <Move, Pair> (move, pair));
    

    When the moveList object goes out of scope, it will automatically release the memory associated with the objects. In the case of pointers, you have to remember to manually deallocate the memory, else there will be a memory leak.

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

Sidebar

Related Questions

I have vector< pair<int, int>> myVec (N); I want to have all pairs initialized
I want to have a data structure of following type in c++. map<string,pair<string,vector>>. Any
If I have a vector of pairs: std::vector<std::pair<int, int> > vec; Is there and
I have a private attribute in a class that is defined as vector<pair<char *,
I have a vector of Key-Value pairs, where each Key-Value pair is also tagged
I have the following class which stores pairs of dates and prices: #include <vector>
I have a vector of pairs representing hops between nodes which I would like
I have a std::vector with the element as pair and need to find all
I have a vector class and I defined the __mul__ method to multiply a
I have a 3D vector defined like this... std::vector<std::vector<std::list<Object*> > > m_objectTiles; I have

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.