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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:17:12+00:00 2026-05-28T16:17:12+00:00

I am doing something like: struct ABC{ int p,q,r; }; struct X{ ABC *abc;

  • 0

I am doing something like:

struct ABC{
int p,q,r;
};

struct X{
ABC *abc;
X(ABC &abc) : abc(&abc) {}
};

std::vector<ABC> vec;
... //populate vec

X x(vec[2]);

When I debug, x.abc looks correct directly after the assignment but then shortly afterwards the data in x.abc is garbage. It’s making me think the pointer is to a local variable… but vector::operator[] returns a reference so is that possible?

  • 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-28T16:17:14+00:00Added an answer on May 28, 2026 at 4:17 pm

    Internally, the std::vector usually maintains a dynamic array of the elements it contains. If the vector’s size grows too large and exceeds its old capacity, it allocates a new array, copies the old elements over, then deallocates the old array. As a result, any references or pointers into that old array become invalid and will result in undefined behavior if used.

    If you want to store a pointer into a vector, you should be sure that the vector does not end up reallocating its internal buffer. You can do this either by waiting until you’ve added all the elements you’re going to add to the vector before taking references, or by calling vector::reserve to ensure that the capacity is large enough.

    Even better, though, would be to instead store a reference to the vector object itself along with the index, then look up the element at that index each time. That way, if the vector resizes its internal buffer, your pointers don’t become garbage because you’re re-indexing into the vector each time.

    Hope this helps!

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

Sidebar

Related Questions

So, I tried doing something like this : void place(struct node * list, int
I need help with gcc warning,looks like im doing something wrong here unsigned long
I have a C++ struct that looks like this: struct unmanagedstruct { int flags;
I would like to write something like this, which cannot be compiled: std::vector<A> as;
Assume you are doing something like the following List<string> myitems = new List<string> {
Right now I'm doing something like this: RewriteRule ^/?logout(/)?$ logout.php RewriteRule ^/?config(/)?$ config.php I
Not that I'm doing something like that, but I am kind of interested how
I'm doing something like the following: SELECT * FROM table WHERE user='$user'; $myrow =
Currently I'm doing something like this in markup <input type=text ONKEYPRESS=InputNumeric(event); id=txtNumber /> But
I'm doing something like this: @protocol CallbackDelegate -(void) performCallback; @end @interface MyObject : NSObject

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.