views:

223

answers:

9

Instead of

int *i = new int;
...
delete i;

could I just do this?

int i;
...
delete &i;
+10  A: 

You don't want to do that. The int declared on the stack will be cleaned up when the code goes out of scope.

Therealstubot
+5  A: 

Just do:

int i;

i = 5 + 5;
Martin York
What is with the downvote here? OP clearly doesn't get heap-based allocation vs stack variables.
John Dibling
A: 

Just do

int i;

After the closing brace for the code block the integer is declared in, it'll be removed from the stack anyway.

mingos
+4  A: 

If you declare a variable on the stack, its destructor will be called and the memory reclaimed when it goes out of scope.

If you allocate space from a heap, you have to free that space explicitly.

You cannot mix these two strategies.

Adrian McCarthy
+2  A: 

Short answer: no

Long answer:

int i;

declares a int on the stack. This int will be removed when the int falls out of scope.

int *i = new int;

declares an int pointer on the stack, then creates a int on the heap (the new) and assigns the address of the heap value to the stack value. Again, the int pointer is removed when it falls out of scope, but the heap value sticks around unless it's deleted.

Joel
A: 

If you want to allocate it on the stack, do this:

int i;
i=5;

The int will be remove when the stack will go out of scope (when you quit the function for instance).

If you want to allocate it on the heap, do this:

int* i = new int; 
*i = 5;
delete i;

This will create you a pointer to a int on the heap. Delete it when you don't need it anymore.

Jean-Philippe Jodoin
No. Don't do memory management like that. There is no exception safety. All pointers (dynamically allocated) should be wrapped in a smart pointer (there are several available in the standard library). In real production code there should hardly ever be any delete's in the code.
Martin York
+2  A: 

You should only dynamically allocate variables if you need them on the heap.

A common misconception (or habit) from people who program in other languages, such as java, is to use the new operator to create every variable. This is not necessary and may cause fragmentation in your code.

In C++, for small variables, just declare them:
int i;
instead of:
int * i = new int;
By not declaring them using the new operator, you are allowing the compiler to automatically dispose of them when necessary (a.k.a. when leaving scope). This saves the burden of allocation and deallocation from the programmer.

Thomas Matthews
+2  A: 

No. In fact, NO! You can only use delete to free memory that was allocated by new. If you call delete on a pointer to a local variable, or a pointer to memory allocated by malloc(), your program will likely crash.

Also, be sure to understand the difference between delete and delete [].

Not to mention that there is no need to delete a local variable. It is allocated on the stack, and will be destroyed automatically when it goes out of scope (e. g. when the function returns).

Dima
A: 

Don't do this

int i; 
...
delete &i;

But if You really must allocate an int on the heap and You absolutely want your variable to look like a normal int variable, You could try this:

int &i = *(new int); 
...
delete &i;
Maciej Hehl
But of course this last example would neither be exception-safe or especially time efficient, and probably confuse everyone, even the original coder.
daramarak
Could You elaborate on the lack of exception safety please? Do you suggest the pointer is safer?
Maciej Hehl
@MaciejH: It means if an exception is thrown or the function is otherwise left early without a chance to delete, you've leaked memory. Pointers should be put in automatically (stack) allocated variables that will delete in their destructor (like `auto_ptr` or Boost `shared_ptr`) so you don't have to worry about memory management.
GMan