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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:35:23+00:00 2026-05-28T23:35:23+00:00

For example if I do something like this: vector<int> myvector; myvector.push_back(100); int * ptr

  • 0

For example if I do something like this:

vector<int> myvector;
myvector.push_back(100);
int * ptr = &(myvector[0]);
myvector.clear();
myvector.push_back(10);

Will ptr still be valid? Or could it now be pointing to garbage?

  • 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-28T23:35:23+00:00Added an answer on May 28, 2026 at 11:35 pm

    23.2.3 §4 says:

    a.clear() […] invalidates all references, pointers, and iterators referring to the elements of a and may invalidate the past-the-end iterator.

    Since there is no such thing as “un-invalidation”, using ptr after clear results in undefined behavior.

    On a side note, the parenthesis in &(myvector[0]) are not needed. Postfix operators always have higher precedence than prefix operators in C++, so writing &myvector[0] is just fine.

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

Sidebar

Related Questions

It is valid JavaScript to write something like this: function example(x) { Here is
For example can i have something like this: public static void SAMENAME (sameparameter n)
I'm trying something like this, but this example does not work. jsObj = {};
For example, I try to do something like this: - (BOOL)compare:(NSDecimal)leftOperand greaterThan:(NSDecimal)rightOperand { BOOL
For example, I have a struct which is something like this: struct Test {
Ideally I want a function something like the following (this example doesn't compile): void
Have a vector of vectors that looks something like this 3 1 2 0
The common example for C++11 range-based for() loops is always something simple like this:
I've seen several examples of code like this: if not someobj: #do something But
I usually do something like the example below when I need to return error

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.