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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:54:58+00:00 2026-06-13T06:54:58+00:00

Suppose: struct Foo { Obj* pObj; Foo() : pObj(NULL); }; Obj* CreateObj() { //do

  • 0

Suppose:

struct Foo
{
    Obj* pObj;
    Foo() : pObj(NULL);
};

Obj* CreateObj()
{
   //do some stuff and then
   return new Obj; //obj is a class
}

int main()
{
   Foo foo;
   foo.pObj = CreateObj();
   DoSomeOperationWithTheObj( foo.pObj );
   //suppose foo is a monster that should be 'killed' or deleted now
   delete foo.pObj;
   foo.pObj = NULL;
   //the question is can this pointer be 're-used' now like this:
   foo.pObj = CreateObj(); //create another object
}

Since the pointer was deleted, isn’t there a problem re-using it right?

  • 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-13T06:54:59+00:00Added an answer on June 13, 2026 at 6:54 am

    As for your original question: yes, you can reassign to pointers like that. A pointer holds just a memory address and nothing more.

    But you should not actually ever do this because handling raw pointers like this can lead to bugs, you already have a few of those in your code. Modern C++ allows you to do this way more nice and without concern. Suppose we start from this (compilable) code, i replaced Obj by an int, but the fact that it’s a native type instead of a class does not matter:

    #include <iostream>
    
    struct Foo
    {
        int* pObj;
        Foo() : pObj(NULL) {}
    };
    
    int* CreateObj()
    {
       return new int(42); //obj is a class
    }
    
    int main()
    {
       Foo foo;
       foo.pObj = CreateObj();
       std::cout << *foo.pObj << std::endl;
       delete foo.pObj;
       foo.pObj = new int(13);
       std::cout << *foo.pObj << std::endl;
       delete foo.pObj;
    }
    

    We can convert this to the following:

    #include <iostream>
    #include <memory>
    
    struct Foo
    {
        std::unique_ptr<int> pObj;
        Foo() : pObj(NULL) {}
    };
    
    std::unique_ptr<int> CreateObj()
    {
       return std::unique_ptr<int>(new int(42));
    }
    
    int main()
    {
       Foo foo;
       foo.pObj = CreateObj();
       std::cout << *foo.pObj << std::endl;
       foo.pObj = std::unique_ptr<int>(new int(13));
       std::cout << *foo.pObj << std::endl;
    }
    

    Note that the major change is that I removed raw pointers and replaced them with the unique_ptr wrapper. This has a few advantages:

    1. You clearly state ownership, a unique_ptr can only be owned by the current scope. While createObj creates the object, by returning the temporary (nameless) unique_ptr it releases ownership so the caller can delete it whenever it wants. This will avoid tricky memleaks.
    2. Deletes happen automatically for you, either when the unique_ptr goes out of scope or when it is overridden (e.g. by the assignment operator).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose: struct A { virtual int foo(const A& a) const { return 1; }
Suppose: struct Foo { int _b; std::string _name; }; class Bar { public: vector<Foo>hold;
I'm using LLVM-clang on Linux. Suppose in foo.cpp I have: struct Foo { int
Suppose I have: struct Foo: public Bar { .... } Foo introduces no new
Suppose I declare a new C++ struct type: struct my_struct { int a; int
Suppose I have such a struct: struct Foo { const int bar; const char
Suppose some type Foo has an overloaded operator-> that returns a Bar* : struct
Suppose I have some simple struct like this: public struct WeightedInt { public int
Suppose you have a class like template<class T> struct A { void foo() {
Suppose I have: struct Magic { Magic(Foo* foo); Magic(Bar* bar); }; Is there a

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.