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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:35:38+00:00 2026-06-08T21:35:38+00:00

have this code: void set(list<Person*>* listP){ Person timmy = Person(10); listP->push_back(&timmy); } int main()

  • 0

have this code:

void set(list<Person*>* listP){
    Person timmy = Person(10);
    listP->push_back(&timmy);
}
int main()
{
    list<Person*> listP;
    set(&listP);
    Person* timmy  = listP.back();
}

If i understand correct (please correct me) timmy is allocated on the stack , so i cannot count on the values of timmy when i use them in the main. Am i correct? do i need to create timmy like this:

Person* timmy = new Person(10);

in order to create it on the heap and not on the stack ,so it will not be destroyed after method return?

Thank you

  • 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-08T21:35:40+00:00Added an answer on June 8, 2026 at 9:35 pm
    void set(list<Person*>* listP){
        Person timmy = Person(10); // create timmy on automatic storage (stack)
        listP->push_back(&timmy); //push timmy's address
    } //timmy is destroyed. pushed address points to deallocated memory
    

    Yes, you need to use Person* timmy = new Person(10); to allocate on heap.

    void set(list<Person*>* listP){
        Person *timmy = new Person(10); // timmy is a pointer now
        listP->push_back(timmy); //push timmy's copy (copy of pointer)
    } //timmy (pointer) is destroyed, but not the memory it points to
    

    Also prefere to use smart_pointers such as std::shared_ptr, std::unique_ptr or boost smart pointers. It will simplify memory management and writing exception-safe code

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

Sidebar

Related Questions

I have this code: void Main() { List<Employee> employeeList; employeeList = new List<Employee> {
I have this code : void Main() { System.Timers.Timer t = new System.Timers.Timer (1000);
Lets say I have this code: public void MyMethod(string Data, List<string> InputData) { //I
I have this code: //choosed a picture public void onActivityResult(int requestCode, int resultCode, Intent
I have this code: hubSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int
I have this JSON response string: {d:{\ID_usuario\:\000130\,\Nombre\:null,\Vipxlo\:0,\Provmun\:null,\Descuentos\:null,\Listaviplocal\:null}}` With this code: - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
I have this code: private void timer1_Tick(object sender, EventArgs e) { #region BaseAddress Process[]
I have this code private void writeReport(IReport report, string reportName) { string reportString =
I have this code: @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { GLES20.glClearColor(0, 0,
I have this segment of code: - (void)setTableView:(UITableView *)tableView { tableView = tableView; }

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.