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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:06:16+00:00 2026-06-05T07:06:16+00:00

#include <iostream> #include <vector> #include <string> #include <algorithm> #include <unordered_map> #include <unordered_set> #include <cmath>

  • 0
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <cmath>

using namespace std;

template <class T>
class binary_node {
public:
    T data;
    binary_node<T> *left;
    binary_node<T> *right;

    binary_node(const T& data)
        :data(data), left(NULL), right(NULL) {
    }
};

int main() {
    binary_node<int>* node = new binary_node<int>(10);
    node->left = new binary_node<int>(1);
    node->right = new binary_node<int>(50);

    binary_node<int>* ptr = node->left;

    delete ptr;
    ptr = NULL;

    if (node->left == NULL) {
        cout << "????";
    }
    else {
        cout << node->left->data << endl;
    }   

    return 0;
}

I would expect node->left == NULL, but the result is totally unexpected even though the data of node->left is garbage. I’m using Visual C++ 2010, could anyone help me explain this behavior?

EDIT
On the other hand, it works just fine when traversing and delete node by node like this:

    ~linkedlist() {
#if DEBUG
        cout << "~linkedlist() called.\n";
#endif
        while (head != NULL) {
#if DEBUG
            cout << "delete node: " << head->data << '\n';
#endif
            node<T>* temp = head;
            head = head->next;
            delete temp;
            temp = NULL;
        }
    }
  • 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-05T07:06:16+00:00Added an answer on June 5, 2026 at 7:06 am

    You are deleting the the data allocated to node->left object ie. the new binary_node<int>(50) object.

    However you are deleting via another pointer. You then NULL that other pointer

    node->left is never set to null. As such the contents of whatever it points (deallocated memory) is is what it points to.

    try this:

    binary_node<int>** ptr = &(node->left); 
    
    delete *ptr; 
    *ptr = NULL; 
    

    Or this

    delete node->left; 
    node->left = NULL; 
    

    Here is an improved depiction I made to show what I’m saying:
    enter image description here

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

Sidebar

Related Questions

#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main(void) {
#include <boost/algorithm/string.hpp> #include <vector> #include <iostream> #include <string> using namespace std; using namespace boost;
#include <iostream> #include <string> #include <vector> using namespace std; struct Exmpl{ Exmpl() { cout
#include <stdlib.h> #include <iostream> #include <vector> #include <string> class A { public: std::string s;
#include <iostream> using namespace std; // This first class contains a vector and a
#include <algorithm> #include <iostream> #include <iterator> #include <string> #include <vector> #include <utility> using namespace
#include <iostream> #include <vector> #include <string> #include <ostream> #include <algorithm> #include <boost/function.hpp> using namespace
Compiling this example #include <boost/bind.hpp> #include <boost/algorithm/string.hpp> #include <algorithm> #include <iostream> #include <vector> using
Consider the following code: #include <cstdlib> #include <iostream> #include <string> #include <vector> #include <algorithm>
Please take a look at this example: #include <iostream> #include <vector> #include <string> using

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.