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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:38:37+00:00 2026-06-11T11:38:37+00:00

I am trying to understand the new operator more closely. I do understand the

  • 0

I am trying to understand the new operator more closely. I do understand the fact that it allocated memory from the heap and returns a pointer to the memory. My question is that once I get the pointer and use it to store another pointer to another declared variable, how does the value copy or the value pointed to by happen? So for instance

i declare a variable

int x = 4;

and say

int* ptr = new int;
ptr = &x;

ptr points a chunk of memory from the heap. x is defined in a stack owning a separate chunk of memory. ptr and the address of x are the same. If I delete ptr, x is still valid as it still exists in the memory. When I say *ptr, I am looking for the value pointed to by ptr, which in this case is 4. My question is that 4, where does that reside. Does it live in two separate chunks of memory. One is represented by x and other I just got from new. How does the process happen? How does 4 get transmitted across the two chunks, or I am missing sthg? Please help.

Also when I say ptr = &x, is that a bitwise copy. In other words, do I forever loose the memory I just got access to through the heap?

  • 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-11T11:38:38+00:00Added an answer on June 11, 2026 at 11:38 am
    int* ptr = new int;
    ptr = &x;
    

    ptr points a chunk of memory from the heap

    No, it doesn’t. After the assignment, it points at the address of variable x, which has automatic storage. You have lost the handle to the dynamically allocated int initially pointed at by ptr, so you can no longer delete it. 4 does not get transmitted across any “chunks” of memory. When you de-reference, ptr, you get the variable referred to by x, which holds the value 4.

    Try this example:

    #include <iostream>
    
    int main()
    {
    
      int x = 4;
      int* ptr = new int;
      ptr = &x;  // lose handle on dynamically allocated int: MEMORY LEAK
      std::cout << (*ptr) << "\n";
      x += 1;
      std::cout << (*ptr) << "\n";
      (*ptr) += 1;
      std::cout << x << "\n";
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new in objective-c and I am trying to understand memory management to
Trying to understand the new async/await pattern, I have one question which I can't
I am new to Dojo and trying to understand how to handle events. There
I'm new to iOS development and I'm trying to understand if is it's possible
I'm new to programming with linux and I was trying to understand how fork()
I'm new to git and I'm trying to understand if it can solve my
I am new to mutli-threading in java and trying to understand the keyword synchronization
I'm rather new to C++ and I'm trying to understand the code over on
I'm relatively new to Java and I'm still trying to understand the fundamentals. I
I'm really new to OOP and C# and I'm trying to understand, how I

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.