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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:55:06+00:00 2026-06-05T07:55:06+00:00

In C++, I have two block of codes like this: Base *base = new

  • 0

In C++, I have two block of codes like this:

Base *base = new Base();
base->showName();

And:

Base base;
base.showName();

I don’t know when do we use pointer and when not? And what’s different and what is better?

  • 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-05T07:55:09+00:00Added an answer on June 5, 2026 at 7:55 am

    The first code you showed is a memory leak.

    The second snippet is Java, not C++. The question has been edited to use my suggested code.

    Generally though, in C++ you should avoid new unless you really NEED dynamic lifetime. Instead, write:

    Base base;
    base.showName();
    

    This is better because

    • It’s faster, no heap allocation needed.
    • You don’t have to remember to free the memory, the compiler does that automatically at the end of the scope.
    • It’s automatically exception safe. The destructor will be called, and the memory will be recovered, during stack unwinding.

    If the object needs to live past the end of the scope, you should be using:

    unique_ptr<Base> base(new Base());
    base->showName();
    

    Now unique_ptr will free the memory for you when the unique_ptr dies, and it’s also exception-safe. When you return a unique_ptr, ownership is transferred to the caller, and he can reap the benefits of automatic cleanup.

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

Sidebar

Related Questions

I have the same block of code repeated with two different variables. This block
I know this may seem like a strange question, but say I have two
I have two block (like Block1 and Block2) and I have to display one
I have usercontrols which inherit a base class which looks like this: BasePage.cs: using
I have two handlers for a mouseover function, I would like to know what
I have two DIV elements. HTML looks like this: <div id=first><div id=second></div></div> The CSS
I have two ways I might need to call some code using a block.
I have the following two code blocks. Code block 1 var checkboxes = $(div.c1
I have an abstract entity called Block which contains two attributes: column and order
I have two types of buttons that I want to use the same code

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.