Question: ```
struct node
{
    int data;
    node *next;
} * save;

```

Can anyone explain what's the difference between below declarations, does stack & heap-based memory allocation is the only difference between them or any other?
Can we call both of them as object pointers?

```
node *temp;

```

and

```
node *tmp = new node;

```

Answer: 
The new operator denotes a request for memory allocation on the Free Store. If sufficient memory is available, new operator initializes the memory and returns the address of the newly allocated and initialized memory to the pointer variable.

Syntax to use new operator: To allocate memory of any data type, the syntax is:

```
pointer-variable = new data-type;

```

Here, pointer-variable is the pointer of type data-type. Data-type could be any built-in data type including array or any user defined data types including structure and class.
Example:

```
// Pointer initialized with NULL
// Then request memory for the variable
int *p = NULL; 
p = new int;   

            OR

// Combine declaration of pointer 
// and their assignment
int *p = new int;

```
Well, they are both pointers to objects, specifically `node`. Now, the pointer itself i.e. both `*temp` and `*tmp` are stack allocated variables that are capable of holding a memory address that can point to an object of type `node`.

Both of these pointers are capable of holding addresses of both dynamically allocated `node` objects created at runtime on the heap as well as local objects on the stack created at compile-time .

```
node stack_alloc_node; 
node* n;

```

Here, `n` is allocated on the stack at compile time and it is capable of holding the address of any `node` object;
I can do this:

```
n = &stack_alloc_node;

```

`stack_alloc_node` is created on the stack at compile time and now `n` holds a reference to it.
Now, let us talk about `new`. When you want to dynamically allocate memory for an object at runtime, you do this:

```
new node;

```

`new` asks the operating system for memory and is given a block of memory by the OS. `new` then returns the address of this memory. Any memory created at runtime can only be referred to through pointers and hence for storing the address of that memory block somewhere, we need pointers:

```
node* temp = new node;

```

A pointer variable that is not assigned is called a **wild pointer** because it may contain an arbitrary address which is useless.

Here, are the 3 elements:

```
node n1;

```

`n1` is a stack-allocated `node` object created at compile time.

```
new node()

```

This is an instruction to the computer to ask for memory and to create a new `node` object on that memory by calling `node`'s constructor and then to return the address of that memory.

```
node* n2;

```

`n2` is a pointer variable allocated on the stack that is not yet assigned and hence is wild. This pointer is capable of holding addresses of both heap allocated and stack allocated `node`s. You can do both of the following:

```
n2 = new node();
n2 = &n1;

```

In the first scenario, the reference to the newly obtained memory has to be stored and that's what the purpose of `n2` is in the first statement. The second statement is also valid because `n2` is still assigned a pointer but to a stack allocated `node`.
However, the previous code block has a memory leak because all references to that newly created `node` using `new node()` on the heap is lost. You should always:

```
delete n2;

```

to free the associated memory before assigning the pointer to another address. You do not need to delete objects created on the stack because they are automatically destroyed when control goes out of scope. This is not the case with heap allocated objects, they need to be explicitly deleted. In languages like Java and C#, this is automatically handled by the garbage collection sub-system. But C++ does not have one. You can read about smart pointers [here](https://en.cppreference.com/book/intro/smart_pointers) if you want memory to be handled automatically.