Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7509291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:51:47+00:00 2026-05-29T22:51:47+00:00

Is it possible to dynamically allocate a temporary variable in C++ ? I want

  • 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
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:51:49+00:00Added an answer on May 29, 2026 at 10:51 pm

    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to subclass dynamically? I know there's ____bases____ but I don't want
Possible Duplicate: Dynamically Growing an Array in C++ I want to create a loop
Is it possible to dynamically determine the name of a variable in Python? For
Is it possible to dynamically allocate memory on a GPU's Global memory inside the
Is it possible to dynamically generate an assign xpath from a variable and an
How is is that it is possible for us to delete dynamically allocated arrays,
Is it possible to dynamically create and modify images on a per pixel level
Does someone knows if it's possible to dynamically create a call chain and invoke
I am curious if it is possible to dynamically build a list comprehension in
I was wondering if it were possible to dynamically create an XML layout file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.