0

Is it possible to dynamically allocate a temporary variable in C++ ?
I want to do something like that :

#include <iostream> #include <string> std::string* foo() { std::string ret("foo"); return new std::string(ret); } int main() { std::string *str = foo(); std::cout << *str << std::endl; return 0; } 

This code works but the problem is I have to create an other string in order to return it as a pointer. Is there a way to put my temporary/local variable inside my heap without recreate an other object ?
Here is an illustration of how I would do that :

std::string* foo() { std::string ret("foo"); return new ret; // This code doesn't work, it is just an illustration } 
6
  • 5
    What is wrong with std::string foo() { return "foo"; }? The copy is all but guaranteed to be elided. Commented Feb 15, 2012 at 2:48
  • +1 to James, but avoid 'all but' it's confusing for us, non-native english speakers. Commented Feb 15, 2012 at 2:55
  • My code is much more complex than that, the pointer constraint is not avoidable. Commented Feb 15, 2012 at 2:59
  • 1
    Then, maybe, a smart pointer will help, especially since you need to be able to free that variable in many cases. Commented Feb 15, 2012 at 3:02
  • @kl94: If your example is not indicative of your issue, can you provide an example that shows why you absolutely must return heap-allocated memory? Commented Feb 15, 2012 at 5:04

2 Answers 2

3

Well yes there is, it's called smart pointers:

#include <memory> std::unique_ptr<std::string> foo() { return std::unique_ptr<std::string>("foo"); } // Use like this: using namespace std; auto s = foo(); // unique_ptr<string> instead of auto if you have an old standard. cout << *s << endl; // the content pointed to by 's' will be destroyed automatically // when you stop using it 

Edit: without changing the return type:

std::string* foo() { auto s = std::unique_ptr<std::string>("foo"); // do a lot of stuff that may throw return s.release(); // decorellate the string object and the smart pointer, return a pointer // to the string } 
Sign up to request clarification or add additional context in comments.

6 Comments

(I assume you want dynamically allocated memory, if you don't then return by value as mentionned by James McNellis)
It's a nice way, but is there a way to do that without change the return type ? (std::string *) ? I was thinking about dynamic_cast but I cannot get it to work properly..
Unfortunately, I cannot use your solution because I use an old standard. But thank you for your help.
In case you can use boost, boost provides boost::unique_ptr in the old standard which has inspired the new standard.
@J.N.: No, Boost does not provide unique_ptr. It provides shared_ptr and scoped_ptr, but unique_ptr requires move semantics, which didn't exist when Boost did most of its stuff. And even Boost.Move doesn't provide a unique_ptr implementation. You should suggest auto_ptr, which is a perfectly functional (though dangerous if you don't know what you're doing) smart pointer.
|
0

How about this:

std::string* foo() { std::string * ret = new std::string("foo"); // do stuff with ret return ret; } 

2 Comments

of course, this code works. At start, I've done this but I have to do some stuff with my var. But I have a lot of exception to check and I have to delete my var before each throw. So I've decided to create a local variable, then after test, return a pointer of it.
To complete my answer, smart pointers are guaranteed to be deleted even if you throw.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.