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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:59:20+00:00 2026-06-07T13:59:20+00:00

Consider the following example class Foo{ public: Foo(int i):x(i){} int x; }; void bar(Foo

  • 0

Consider the following example

class Foo{
  public:
    Foo(int i):x(i){}
    int x;
};

void
bar(Foo *p2)
{
  delete p2; 
  p2 = new Foo(2); 
}  

int
main()
{
  Foo *p1 = new Foo(1);
  cout<<p1->x;
  bar(p1);
  cout<<p1->x;
}

To my understanding, pointer variables are stored on the stack and contains an address to the dynamically allocated memory on the heap which it’s “pointing” to. Now, when I pass a pointer to the function a second pointer is created on the stack, pointing to the same memory address as the first pointer. When I delete p2 in bar() and allocate new memory, p1 and p2 should be pointing to different addresses, right?

However, if I compile this code I get 1 and 2 as output. Is this because p2 manage to allocate the same memory cell to which p1 is already pointing to, or is there something I have missed?

  • 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-07T13:59:22+00:00Added an answer on June 7, 2026 at 1:59 pm

    Is this because p2 manage to allocate the same memory cell to which p1 is already pointing to, or is there something I have missed?

    You missed nothing. You have correctly identified that this program’s behavior is undefined. By definition, it could print anything at all, including 2.

    In your run, it is only coincidence (and very likely, the coincidence that you identified) that causes it to print 2. You might disrupt that chain of coincidence by adding new int(47) after the delete and before the existing new.

    If your desire is for the program to behave the way it does, but through defined behaviors, you may make this small change to bar‘s definition:

    void
    bar(Foo*& p2)
    

    This passes the parameter by reference instead of by value. By doing this, changes to the variable p2 inside bar are reflected in p1 inside `main.

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

Sidebar

Related Questions

Consider the following example code: class Foo { }; class Bar : public Foo
Consider the following example: public interface ITask { void Execute(); } public class LoggingTaskRunner
Consider the following Java function: public void foo(Class<? extends Exception> cl, List<? extends Exception>
Consider the following code: abstract class Foo<T> where T : Foo<T>, new() { void
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider the following example: public class BaseClass { public string StringInBaseClass {get;set;} public int
consider the following simplified example: public class Ticket { public int Id; public TicketState
Consider the following example: int size = 10, *kk = new int[size]; for (int
Consider the following short code snippet. namespace B { public class Foo { public
Consider the following class: class Foo { enum Flags {Bar, Baz, Bax}; template<Flags, class

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.