Possible Duplicate:
What is difference between instantiating an object using new vs. without
Creating an object: with or without `new`
What is the difference between these two statements
HttpUtil httpUtil;
and
HttpUtil *net = new HttpUtil();
Which one is better to be used?
The first statement creates a variable called
httpUtilon the ‘stack’ – this means that, as soon as the function containing that line finishes, the variable goes ‘out of scope’ and gets released (the memory it uses becomes free to use for other stuff).The second statement creates a variable on the ‘heap’ – this means that the variable will remain in memory until you call
deleteon it. When allocating variables on the heap you need to make sure that you alwaysdeleteit at some point, otherwise you’ll get memory leaks – this is where you can no longer see your*netvariable, but the memory is still allocated.