-1

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.

Code-Apprentice
  • 69,701
  • 17
  • 115
  • 226
user1629199
  • 21
  • 1
  • 4

3 Answers3

5
Test *pointer = new Test();

Creates the Test object on freestore(popularly known as Heap).It persists in memory until you explicitly call delete on the address returned by new.It is known as a dynamically allocated object.

Test test;

Creates the Test object on Automatic storage and it gets deallocated once the scope({, }) in which it was created ends.The memory is automatically(implicitly) deallocated hence the name Automatic.This is the basis of the widely used concept of Resource Acquisition is Initialization(RAII) in C++

When to use which one?

  • If you want your object to persist beyond the scope in which it was created you use #1 or you use #2.
  • Usually, stack sizes are limited and hence if your object occupies a lot of memory then you might run out of stack space in such cases one would usually go for #1.

So which one to use depends on the situation you have at hand.However, note that in C++ usually, one would want to limit the use of new to a minimum and only when required.

Good Read:
Why should C++ programmers minimize use of 'new'?

Community
  • 1
  • 1
Alok Save
  • 190,255
  • 43
  • 403
  • 518
2
Test *pointer = new Test(); 

Is a memory leak because you never release the memory. The object is created on the heap.

Test test;

The object is created on the stack. When the last curly brace is reached, the object is removed. It goes 'out of scope'.

They are not the same, which is 'better' depends on what your need.

Steve Wellens
  • 20,160
  • 2
  • 24
  • 64
1

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.

pb2q
  • 54,061
  • 17
  • 135
  • 139