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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:13:50+00:00 2026-06-10T11:13:50+00:00

this is the first time I ask a question. I’m a foreigner, so it’s

  • 0

this is the first time I ask a question. I’m a foreigner, so it’s a little bit hard to explain my question. Maybe my title is wrong too… Let’s see the code:

Suppose I have defined a class:

class Test
{
    public:
        Test();
};

int main()
{
     Test *pointer = new Test(); //what's the difference between these two ways, 
     Test test;                  //if  the two ways are the same, which one is better under what
     pointer = &test;            //circumstance? 
}

I hope that you guys can understand what I’m saying and help me.

  • 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-10T11:13:51+00:00Added an answer on June 10, 2026 at 11:13 am

    Test test; will create an object on the stack. It’s local to the function main, and will be automatically de-allocated when main exits. Use this when you only need to use an object in the current block.

    Test *pointer = new Test(); will create an object on the heap, with a lifetime that isn’t limited to the block in which it is declared. When you declare an object this way – using new – then at some point you’ll need to call delete on the object or you’ll leak memory, so you assume the additional burden of handling the memory management. Use this when you need to create an object that needs to stick around, i.e. in other parts of your code, past the current function.

    Given these points, the following code has a problem:

    Test *pointer = new Test();
    Test test;
    pointer = &test;
    

    When you assign to pointer the reference to test, you lose the pointer to the object that you’ve allocated, and leaked memory. Furthermore, if you do this in a context where you might use pointer elsewhere, e.g. in a function that returns a reference to a Test object, it will point to a memory address that isn’t valid after the function exits.

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

Sidebar

Related Questions

Sorry, this's my first time to ask a question here. So, I don't have
It is the first time I ask a question on this forum. Sorry if
This is the second time I ask this question because in my first try
This is the first time I ask something, so if there is something wrong
This is my first time on this site. I will ask you to help
this is my first time asking a question here. I tried to be well
Okay, this is like the 5th time I have had to ask this question,
This is my first time using stackoverflow for a question that I need to
First time I ever ask a question here so correct me if I´m doing
This question may sound weird, but it's first time I do it and cannot

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.