Tech Support Guy banner
Status
Not open for further replies.
1 - 2 of 2 Posts

· Registered
Joined
·
68 Posts
Discussion Starter · #1 ·
Hi everybody :)

Quick example.

Code:
int i = 5 x = 0;

x = i + 1;

// now I don't need the i anymore and I want to free the momory from it.. !

cout<<y;
return 0;
using delete ? -- examples plzzzzz :confused:
 

· Registered
Joined
·
431 Posts
It's really not advisable to try to manage memory for scalar variables. It's generally done more for data structures such as arrays and linked lists.

Keep in mind that any variable declared within a function exists only for the duration of that function's execution, unless it is declared as "static." So memory management for a variable declared within a function (unless it's the main function) is something you don't worry about.

However, if your heart is set on managing memory yourself, "new" and "delete" will do it for you. These are C++'s extensions of C's malloc() and free().

So,

new int i ;
...
delete i ;

Don't try to delete a variable you haven't allocated with "new."
 
1 - 2 of 2 Posts
Status
Not open for further replies.
Top