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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:02:03+00:00 2026-06-11T12:02:03+00:00

Background: While reading about the smart pointers I came across the following sample implementation

  • 0

Background:
While reading about the smart pointers I came across the following sample implementation for Smart Pointer in C++

template < typename T > class SP
{
    private:
    T*    pData; // Generic pointer to be stored
    public:
    SP(T* pValue) : pData(pValue)
    {
    }
    ~SP()
    {
        delete pData;
    }

    T& operator* ()
    {
        return *pData;
    }

    T* operator-> ()
    {
        return pData;
    }
};

class Person
{
    int age;
    char* pName;

    public:
        Person(): pName(0),age(0)
        {
        }
        Person(char* pName, int age): pName(pName), age(age)
        {
        }
        ~Person()
        {
        }

        void Display()
        {
            printf("Name = %s Age = %d \n", pName, age);
        }
        void Shout()
        {
            printf("Ooooooooooooooooo",);
        } 
};

void main()
{
    SP<Person> p(new Person("Scott", 25));
    p->Display();
    // Dont need to delete Person pointer..
}

Questions:

  1. This smart pointer gives the benefit that it deletes the Person class object once it goes out of scope. But then do we need to specifically add the code “delete p;” in the main function so that smart pointer class do not leak itself?

  2. Since the Person class also has the destructor function, do we really need to call the delete on person’s object? The destructor would be automatically called when Person object goes out ofscope

  • 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-11T12:02:04+00:00Added an answer on June 11, 2026 at 12:02 pm

    [1] You do not need to delete p as you allocate it on the stack. If you did:

    SP<Person>* p = new SP<Person>(new Person("Scott", 25));
    

    then you would have to delete it.

    [2] You do not need to call delete on the Person object. That is done by the smart pointer – it is its purpose after all.

    So your code does not have any memory leaks at its current state.
    The smart pointer implementation, however, lacks a copy constructor, assignment operator, etc. so they should be added to avoid some future undesired behaviour.

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

Sidebar

Related Questions

I want to fix the Background while only the ListFields scrolls. Current Problem: Scrolling
On LoginClick I want to run the user validation in the background while my
My application downloads and caches photos into coreData in background threads while still allowing
Some background info: I am currently writing a bootloader in protected mode while learning
I am facing background color change problem while using jquery. When the page loads
I have to do a background process which will perform some operations.. while this
I want replicate the effect obtained by the following CSS code: background: white url(./img/background.png)
Okay, to give a little background, I learned WPF about 3 years ago and
Background While running benchmark tests this morning, my colleagues and I discovered some strange
[ edit ] For background reading, and to be clear, this is what 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.