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

  • Home
  • SEARCH
  • 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 9201955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:08:34+00:00 2026-06-17T23:08:34+00:00

Hi i have few doubt related to heap variable… I want to write a

  • 0

Hi i have few doubt related to heap variable…

I want to write a function as below ->

struct test
{
   int x;
   int y;
};

test* fun()
{
    test *ptr = new test;

    return ptr;
}
  1. My doubt is returning a heap variable once it will go out of scope it will lost its value.

  2. There is definitely a memory leak.(As heap variable is not deleted.)

So how can i design such kind of function.

  • 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-06-17T23:08:36+00:00Added an answer on June 17, 2026 at 11:08 pm

    Dynamically allocated objects (what you call heap variables) do not get destroyed at the end of the scope in which they were created. That’s the whole point of dynamic allocation. While the test object is dynamically allocated, the pointer ptr is not – it will be destroyed when it goes out of scope. But that’s okay! You copy the value of the pointer out of the function and this copy still points at the test object. The test object is still there. The function you’ve written is fine, but it isn’t exactly good style.

    Yes, there is a memory leak if nobody ever does delete on a pointer to the test object you created. That’s a problem with returning raw pointers like this. You have to trust the caller to delete the object you’re creating:

    void caller()
    {
      test* p = fun();
      // Caller must remember to do this:
      delete p;
    }
    

    A common C-style way of doing this (which is definitely not recommended in C++) is to have a create_test and destroy_test pair of functions. This still puts the same responsibility on the caller:

    void caller()
    {
      test* p = create_test();
      // Caller must remember to do this:
      destroy_test(p);
    }
    

    The best way to get around this is to not use dynamic allocation. Just create a test object on the stack and copy it (or move it) out of the function:

    test fun()
    {
        test t;
        return t;
    }
    

    If you need dynamic allocation, then you should use a smart pointer. Specifically, the unique_ptr type is what you want here:

    std::unique_ptr<test> fun()
    {
        return std::unique_ptr<test>(new test());
    }
    

    The calling function can then handle the unique_ptr without having to worry about doing delete on it. When the unique_ptr goes out of scope, it will automatically delete the test object you created. The caller can however pass it elsewhere if it wants some other function to have the object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few questions about endian-ness that are related enough that I warrant
I have a table of items which change status every few weeks. I want
I have a doubt with a few lines of PHP. I have the following
I have a few doubts about this robots file. User-agent: * Disallow: /administrator/ Disallow:
I have seen the following question but I still have a few doubts. Sending
Few basic doubts I have: 1. Is primary key column automatically indexed? 2. What
I have few questions about ssis transction isolation levels. consider a scenario:I have an
i have few confusion regarding ORM(Object Relational Mapping) i am thinking about to use
I have few basic questions on session management in GWTP. Now, we create a
I have few controls (Label, Custom Textbox, Datagridview) docked on a form. When I

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.