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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:45:15+00:00 2026-06-07T16:45:15+00:00

I was wondering, if I create an object in main using the new keyword,

  • 0

I was wondering, if I create an object in main using the new keyword, then set that object (pointer) to be pointed to by a member of another class, setting the original pointer to null, where would I delete the allocated memory? I could do it in the second class’s destructor but what if that object wasn’t dynamically allocated?

Some code as an example:

World world;
Player* newPlayer = new Player("Ted");
world.setPlayer(newPlayer);
newPlayer = 0;

So now the ‘player’ member variable in World is pointing to the memory allocated by newPlayer and newPlayer is pointing to null. How should I deallocate this memory when I’m done with the World object?

  • 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-07T16:45:17+00:00Added an answer on June 7, 2026 at 4:45 pm

    You just need to call delete when you are finished. To avoid the need for this though look into making use of shared pointers as they will relieve you of the need to do this.

    Edit: To answer your comment, where you call delete depends on your application but ensure that you

    1. Dont delete it before you have allocated it.
    2. Dont delete it until you are sure nothing else needs to access it.

    A possible place would be in the destructor of your World object, but it is impossible to say without knowing details of the rest of your application. This is why boost::shared_ptr is preferable.

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

Sidebar

Related Questions

I have set up a object class that I am using to create my
I was wondering, when you create an object you can often set multiple attributes
I'm wondering whether I should create extension methods that apply on the object level
I was wondering it is a good idea to creat one object (one class)
I was wondering how to: create a droplet that saves to a relative filepath.
Hi I am wondering how to create dynamic drop down lists that get refined
Hi there i am wondering if i create a webapp that uses Spring AMQP.
My main question is does the Threading lock object create atomic locks? It doesn't
New to the object orientated method of doing things and was just wondering what
I'm trying to create a global (singleton) class that can associate any type of

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.