I'm sorry, but this is wrong. What happens if an exception is thrown between your new and the matching delete? Or you refactor your code months later and you insert a return statement somewhere in the middle. Your pointer will go out of scope, the delete will never be executed. Result: Memory leak. If you use new without a smart pointer, you most like do not know, what you are doing, thus must not use it. There may be a few exceptions to this, but you will have to think really hard before doing it.
Yes, you can just allocate it on the stack which is a much better idea, if it fits the rest of your program (like you can't return the memory from a function then).
Show differencesHistory of post edits
#3rnlf
Posted 12 July 2012 - 02:49 AM
I'm sorry, but this is wrong. What happens if an exception is thrown between your new and the matching delete? Your pointer will go out of scope, the delete will never be executed. Result: Memory leak. If you use new without a smart pointer, you most like do not know, what you are doing, thus must not use it. There may be a few exceptions to this, but you will have to think really hard before doing it.
Yes, you can just allocate it on the stack which is a much better idea, if it fits the rest of your program (like you can't return the memory from a function then).
Yes, you can just allocate it on the stack which is a much better idea, if it fits the rest of your program (like you can't return the memory from a function then).
#2rnlf
Posted 12 July 2012 - 02:48 AM
I'm sorry, but this is wrong. What happens if an exception is thrown between your new and the matching delete? Your pointer will go out of scope, the delete will never be executed. Result: Memory leak. If you use new without a smart pointer, you most like do not know, what you are doing, thus must not use it.
Yes, you can just allocate it on the stack which is a much better idea, if it fits the rest of your program (like you can't return the memory from a function then).
Yes, you can just allocate it on the stack which is a much better idea, if it fits the rest of your program (like you can't return the memory from a function then).
#1rnlf
Posted 12 July 2012 - 02:47 AM
I'm sorry, but this is wrong. What happens if an exception is thrown between your new and the matching delete? Your pointer will go out of scope, the delete will never be executed. Result: Memory leak.
Yes, you can just allocate it on the stack which is a much better idea, if it fits the rest of your program (like you can't return the memory from a function then).
Yes, you can just allocate it on the stack which is a much better idea, if it fits the rest of your program (like you can't return the memory from a function then).