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 8253807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:00:01+00:00 2026-06-08T01:00:01+00:00

Consider a function that returns a Type* , and so it looks like it

  • 0

Consider a function that returns a Type*, and so it looks like it could allocate a Type in its definition, but you can’t tell for sure (there are many functions, and you don’t have the time to read their definitions).

How can you tell if you should delete the pointer returned or not? for example, this is the type:

struct MyStruct
{
    MyStruct(void) { cout << "Created.\n"; }
    ~MyStruct(void) { cout << "Deleted.\n"; }
};

And this is the function:

MyStruct* Func1(void)
{
    return (new MyStruct());
}

Func1 allocated a pointer, and you should later de-allocate it. but maybe the definition is something else, and the pointer shouldn’t be deleted.

My question is: How can you tell if to delete the pointer or not? for example: maybe the pointer is static?

MyStruct* Func2(void)
{
    static MyStruct* ms = &MyStruct();
    return ms;
}

De-allocating this pointer would crash the entire program.

Thanks in advance.

  • 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-08T01:00:03+00:00Added an answer on June 8, 2026 at 1:00 am

    How can you tell if you should delete the pointer returned or not?

    You can’t, and that is one reason why it’s a very bad idea to use raw pointers to manage memory. If an object needs deleting, then that should always be done automatically, using a smart pointer.

    This also has the advantage that the dynamic object will always be deleted once you’ve finished with it, even if an exception gets thrown.

    std::unique_ptr<MyStruct> Func1()
    {
        return std::unique_ptr<MyStruct>(new MyStruct);
    }
    
    void do_something()
    {
        auto thing = Func1();
        do_something_with(thing);
    
        // The object is automatically deleted here
        // even if the function threw an exception.
    }
    

    If you’re forced to use a badly-designed library with functions return pointers that might or might not need deleting, then your only choice is to either read the documentation or find a better library. It would be a good idea to immediately assign any pointer that needs deleting to a smart pointer, so you at least have exception safety.

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

Sidebar

Related Questions

Consider the hypothetical function repeatcall , that takes as arguments a no-args callable func
for example if we consider document.createElement() function the parameter can be passed in 3
Consider: template < typename Something > boost::function<void()> f() { typedef typename Something::what type; return
I've two different template classes. One of them has a member function that returns
Consider the following code that takes the function f(), copies the function itself in
I frequently have code that looks something like this: if (itm != null) {
Consider the following scenario. I have a method which returns ISomething , but it
Consider: function Shape() { this.name = "Generic"; this.draw = function() { return "Drawing "
Consider: public function & get($name, $default = null) Why & ?
Consider some function foo : def foo(input) input * 2 end How to get

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.