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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:34:24+00:00 2026-06-15T10:34:24+00:00

If a pointer is set to NULL wouldn’t any references to it or through

  • 0

If a pointer is set to NULL wouldn’t any references to it or through it also be NULL. Here is a compilable example that will bomb when you try to run it:

#include <string>
#include <iostream>

#define NULL 0

class Seedcoat {
public:
    //Seedcoat();
    std::string Name;
    int Weight;
};

class Seed {
public:
    //Seed();
    std::string Name;
    int Weight;
    Seedcoat* ItsSeedcoat;
};

class Apple {
public:
    //Apple();
    std::string Name;
    int Weight;
    Seed* ItsSeed;
};

int main()
{
///////Apple Objects Begin///////
    Apple       MyApple;
    Seed        MySeed;
    Seedcoat    MySeedCoat;

    MyApple.ItsSeed = &MySeed;
    MyApple.ItsSeed->ItsSeedcoat = &MySeedCoat;
    MyApple.ItsSeed->ItsSeedcoat->Weight = 2;

    if ( MyApple.ItsSeed != NULL) {
        std::cout << "The weight of the apple seed's seedcoat is " << MyApple.ItsSeed->ItsSeedcoat->Weight <<".\n";
    }

    MyApple.ItsSeed = NULL;

    if ( MyApple.ItsSeed->ItsSeedcoat != NULL) {
        std::cout << "The weight of the apple seed's seedcoat is " << MyApple.ItsSeed->ItsSeedcoat->Weight <<".\n";
    }

    return 0;
}

So my question is: Why does this

MyApple.ItsSeed->ItsSeedcoat != NULL

return true. I would think it would not because ItsSeed is set to NULL – but it still tries to reference the weight value of ItsSeedcoat – and then it bombs I presume because ItsSeed does not exist. I realize there are easy ways to get around this – this example was just to show the behavior I am observing. Is this anything to be concerned about? – or is this normal behavior? What is/are the reason(s) it was done this way? Thanks.

  • 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-15T10:34:25+00:00Added an answer on June 15, 2026 at 10:34 am

    C++ does not hold your hand in such a way. Setting a pointer to null sets only that pointer to null. It doesn’t clear any subobjects or free any memory as you might expect in Java or Python.

    Once you set the pointer to null, it’s no longer legal to access MyApple.ItsSeed->ItsSeedcoat so anything could happen.

    In your particular problem I think composition would probably be a better solution. However if you do need to manage memory allocation/deallocation in C++ I highly suggest using an appropriate smart pointer which gives you power approximately equal to that of garbage collectors in other languages.

    I do also suggest not defining NULL yourself as some headers already define it for C. In C++03 I typically recommend using the literal 0 while in C++11 you can use the nullptr keyword.

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

Sidebar

Related Questions

I have a structure that contains a pointer to a byte array. To set
Is there any compiler independent and syntactically elegant way to set a vtable pointer
I'm trying to set a static pointer variable in a class but I'm getting
I'd like to set up a function pointer as a member of a class
If I have set<Pointer*> , Pointer* is pointer to custom class, set is from
If ViewController 's view is set to MyView And ViewController maintains a pointer to
I have a disappearing pointer db ; the value is properly set during creation
I have a set of shared pointers: std::set<boost::shared_ptr<T>> set; And a pointer: T* p;
Many times I needed a set of pointers. Every time that happens, I end
I know from reading parashift.com that using delete on a pointer frees the memory

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.