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

  • Home
  • SEARCH
  • 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 7541353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:52:12+00:00 2026-05-30T07:52:12+00:00

I have been stumbling over this problem for a while now. I am trying

  • 0

I have been stumbling over this problem for a while now. I am trying to return a pointer to an object to I can say

MyObject* obj = Manager::Create(int i, int j);

How do I correctly allocate memory so there are no leaks? I thought I was supposed to call new to make the memory on the heap but I have recently been told otherwise.

  • 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-30T07:52:14+00:00Added an answer on May 30, 2026 at 7:52 am

    I thought I was supposed to call new to make the memory on the heap but I have recently been told otherwise.

    Well… I think you’re a bit confused. Yes, you do call new to dynamically allocate memory. However, there are common patterns (see; RAII) that are used to avoid it at all costs as it is an easy way to shoot yourself in the foot (read; write bugs).

    At some point, somethign has to call new in order to dynamically allocate memory. first though you need to ask yourself; does this need to be dynamically allocated? If the answer is no, then declare it like so and move on.

    void Foo() { 
        MyObject obj;  // automatic storage space, will be cleaned up when the scope is left
    }
    

    Next, why not store the pointer in a std::unique_ptr or something equivalent? That will take care of management for you, again, you don’t manage the memory;

    // calls delete in the destructor
    std::unique_ptr pointer( new MyObject() );
    

    The BOOST library has an equivalent class (unique_ptr is C++11).

    The point is that memory is being managed by the class via its constructor and destructor. You allocate memory dynamically when you create the object and you deallocate it (i.e., call delete) in its destructor. You simply stack allocate these in as tight a scope as possible and you don’t have to worry about memory leaks.

    I write C++ at work every day and I almost never call new or delete. As a small example, let’s examine this class, which is a trivial implementation of a scoped pointer (note; this is not a “correct” implementation and is overly trivial! This problem is harder to solve than this, but I am using it solely as an example of managing the lifetime of a dynamically allocated object).

    template<class T>
    class ScopedPointer {
    public:
        ScopedPointer(T* obj) {
            m_pointer = obj;
        }
    
        ~ScopedPointer() {
            delete m_pointer;
        }
    
        inline T* operator->() 
        { 
            return( m_pointer );
        }
    
        inline bool IsValid() const {
            return m_pointer != NULL;
        }
    
    private: 
        T* m_pointer;
    };
    

    You may use this class as a wrapper around a pointer to dynamically allocated memory. When it leaves its scope its destructor will be called and the memory will be cleaned up. Again, this is not production quality code! It is not correct in that it lacks several mechanisms that a real world class would need (copy/ownership semantics, a more advanced deallocater, etc.)

    void Foo() {
        ScopedPointer<MyObject> ptr( new MyObject() );
        ptr->whatever();
    }  // destructor is called, dynamic memory is freed
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have been struggling with Javascript closure for a while trying to wrap brain around
Been stumbling me for over an hour now and that means time to ask
I have a problem that has been stumping me for a while, I wrote
I have been trying to figure out this hoverintent plugin and its stumping me.
I've been exploring WPF and XAML for a while now, but have hit a
This has been stumping me for a few days now. I have a stored
I'm stumbling over myself trying to get a seemingly simple thing accomplished. I have
Have been searching over the Graph API docs, looking for the answer to this.
I have been stumbling through some different steps to do this. I ran the
Have been using it for a while with CodeIgniter and I can't remember if

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.