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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:08:26+00:00 2026-06-16T09:08:26+00:00

im new to c++ and i’m finding hard to understand some vector behaviours. I

  • 0

im new to c++ and i’m finding hard to understand some vector behaviours.
I was trying to implement a function to return an array of int and i found many suggestions to use a vector like this:

vector<int> myFunc()
{
    vector<int> myVector;
    //add elements to vector here...
    return myVector;
}

But from what i know ‘myVector’ is an object created on the stack, so isnt it going out of scope when the function end? when does its destructor get called?
I know there are few other questions about returning vectors, but i need to clarify this specific point, hoping to not have duplicated a question.

  • 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-16T09:08:27+00:00Added an answer on June 16, 2026 at 9:08 am

    Yes because myVector is allocated on the stack, as soon as the function returns, it goes out of scope. But in this case that’s ok! Your function signature is

    vector<int> myFunc()
    

    which returns a copy of myVector so it doesn’t matter that it’s going out of scope since it’s already being copied for the return.

    However if you changed it to something like

    vector<int> & myFunc()
    

    now your telling it not to copy myVector and you’ll have a problem called a dangling reference since myVector will be destructed and you don’t copy it but still try to use it.

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

Sidebar

Related Questions

New to Node.js and Express, I am trying to understand the two seems overlapping
New programmer here, I am trying to understand and break down this code below
New to Android and OpenCV. Been trying to to implement code from new book,
New to linux and trying to escape doing this the hard way. I have
new to c#. I'm trying to make a simple system where I can search
New to wordpress and web design in general. I'm trying to setup a website
new to python here. I am trying to write a program that calculate the
New to bash scripting, The previous answers didn't helped me. I am trying to
New to Python and trying to figure out what went wrong here. Making a
New to java here. I'm trying to split a string into variables in an

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.