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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:04:36+00:00 2026-05-23T08:04:36+00:00

I have a General question about memory management in my c++ code. The compiler

  • 0

I have a General question about memory management in my c++ code. The compiler complains that there is a potential memory leak when I replace a pointer to an object with an new pointer to an object that I have initialized dynamically on the stack. Example:

int* foo(int* h){
 int* b = new int(5);
 b = h;  //Memory leak here. How do I delete b without creating a dangling pointer?
 return b;
}

I then use this function to change the state of a pointer

int i = 1;
int* c = &i;
foo(c);

So my question is I have a class that has a function similar to the one above.
when can I delete b from the foo function?

delete b;

would this go into the destructor (which would not help me, as I am using function foo loads of times. so the heap would get used up possibly….? )

If I have not provided enough info above. Please let me know.

  • 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-23T08:04:36+00:00Added an answer on May 23, 2026 at 8:04 am

    Since this is a general question, my preferred general approach would be to use a smart pointer handled using RAII within your class.

    The smart pointer encapsulates the raw pointer, and handles required memory management for you – when your class is destructed, whatever pointer value is held therein at the time will get cleaned up without you having to manually delete it. When you assign a new value to the smart pointer, it auto-deletes the existing value for you, if any.

    boost::scoped_ptr as a class member ought to work well for you here.

    There also seems to be confusion here about what’s on the stack and what’s on the heap. You will not use up the stack simply because you are calling foo over and over (unless you recurse into foo unbounded, in which case you will eventually run out of stack space). Your local variable b is on the stack, but it points to memory that is allocated using new on the heap. When you exit foo, the stack is reset for the calling function – the stack memory used for b is now available for the next function it calls – but you still need to retain ownership (and eventually release) the heap memory to which b pointed.

    EDIT: some sample code should clarify smart pointer approach

    class MyClass {
    public:
      MyClass() {}  // no explicit init for myPointer needed
      ~MyClass() {} // frees up any pointer held in myPointer, no explicit delete needed
    
      void Foo(int* c)
      {
         myPointer.reset(c);  // releases any existing value, takes ownership of c
      }
    
    private:
      boost::scoped_ptr<int> myPointer;  // initially empty
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a general question about the way that database indexing works, particularly in
I have a general question about rails controllers/models: I have a model Providers, that
Hi I have a general question about Lists in C#. Here is my Code:
I have a general question about jQuery. Lets say I have the following code:
I have a general question about objects, memory, and retaining. I am not a
I have a general question about large in-memory objects and distributed computing. I have
i have a general question about how sql server evaluates the joins.The query is
I have a general question for the group. I am about to start a
I have a general question that is rather open-ended (i.e. depends on platform, application
I have a specific question, that could use a general answer... When building Multi-Tier

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.