Thread: Questionable Object Deallocation

  1. #1
    Registered User
    Join Date
    Jan 2023

    Questionable Object Deallocation

    Hey guys!
    My objective is to implement a tree structure with C++ using a class named "node". During my work I noticed, that when deleting an object of this class, its attributes still have a memory address. Actually I was expecting an error here, because deleting the object should delete its attributes and therefore their reference to any memory address. Since the code is executable and returns addresses, my way of thinking must be wrong. Could you please enlighten me?


    using namespace std;
    node::node(const string& node_name){
        name = node_name;
        cout << "create new node \"" + name + "\"" << endl;
    node::~node() {
        cout << "delete node \"" + name + "\"" << endl;
    int main() {
        node* root = new node("root");
        delete root;
        cout << &root << endl;
        cout << &root->children << endl;
        return 0;
    using namespace std;
    class node {
        string name;
        vector<node*> children;
        node(const string& name);
        virtual ~node();

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Please edit your post and use copy-as-text in your IDE and/or paste-as-text in your browser.

    The washed out colour scheme and font you managed to post is unreadable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    As Salem mentioned, it is important to paste your code without formatting as that screws up the site's formatting. It even seems to remove some spacing.

    The pointer variable 'root' has an address in memory that is totally independent on the value that it contains, just like an int has an address in memory and a totally separate value.
    int n = 42;
    cout << &n << '\n';    // the address of n
    cout << n << '\n';     // the value of n (an integer)
    node *root = new node("root");
    cout << &root << '\n'; // the address of root
    cout << root << '\n';  // the value of root (address of a 'node' object)
    Furthermore, the delete operation does not modify the value of 'root'. All it does is mark the deallocated memory as ready for reuse. (This may actually change the value stored there as part of the allocation system's bookkeeping.)
    delete root;
    cout << &root << '\n'; // the address of root, obviously not changed
    cout << root << '\n';  // root's value has also not been changed, although
                           // it is an error to dereference it after delete
    The addresses of the members of an object are just offsets from the object's base address. So printing &root->children will just add children's offset to the value of root whether or not root points to valid memory (i.e., root is not actually dereferenced in that expression).

    Programmers will often set a pointer to nullptr after a delete to ensure that they get a segfault if it is dereferenced after deletion:
        delete root;
        root = nullptr;
    But even after this, &root->children will still give a value. That value will be 0 (nullptr) plus the offset of children.

    After the delete operation (but without setting root to nullptr) cout << root->name may still yield the "correct" value or a distorted value or possibly segfault. It is "undefined behavior" and could do "anything" (well, at least those three things).
    Last edited by john.c; 04-24-2023 at 01:43 PM.
    Ordinary language is totally unsuited for expressing what physics really asserts.
    Only mathematics can say as little as the physicist means to say. - Bertrand Russell

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Destructor and deAllocation
    By Dave11 in forum C++ Programming
    Replies: 4
    Last Post: 10-29-2014, 06:27 AM
  2. questionable logic
    By Chaplin27 in forum C++ Programming
    Replies: 7
    Last Post: 06-14-2005, 12:37 PM
  3. strange numbers, while loops questionable?
    By exluddite in forum C++ Programming
    Replies: 8
    Last Post: 05-06-2004, 11:11 AM
  4. Return Pointer Deallocation
    By vasanth in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2003, 12:15 PM
  5. Array deallocation
    By charisma in forum C Programming
    Replies: 3
    Last Post: 12-29-2002, 06:20 PM

Tags for this Thread