Is there a way to use the new keyword to allocate on the stack (ala alloca) instead of heap (malloc) ?
I know I could hack up my own but I’d rather not.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To allocate on the stack, either declare your object as a local variable by value, or you can actually use alloca to obtain a pointer and then use the in-place new operator:
However, while using alloca and in-place new ensures that the memory is freed on return, you give up automatic destructor calling. If you’re just trying to ensure that the memory is freed upon exit from the scope, consider using
std::auto_ptr<T>or some other smart pointer type.