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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:16:18+00:00 2026-05-20T07:16:18+00:00

I am a computer science student with the task of creating a dynamic data

  • 0

I am a computer science student with the task of creating a dynamic data structure, linked lists. I am currently working on a singly linked list and have successfully built the functionality to add, remove and dump all node data.

However, remembering that my ‘advanced programming’ lecturer stated that in order to avoid confusion and other problems, when deleting nodes from a list, or releasing any object’s memory, you should have it happen inside its deconstructor. So I moved:

delete[] _del;

Which worked fine and moved it to the nodes’ deconstructor:

#include "Node.h"

// Node.cpp

Node::Node(const int &inData, const int &inId)
{
    _id = inId;
    _data = inData;
    nextNode = NULL;
}

// Deconstructor to delete the node when using List.Del()
Node::~Node()
{
    delete[] this;
}

In my List the node’s deconstructor is called via a pointer like so:

_del->~Node();

Which gives me an assertion error. I’m assuming it is my usage of ‘this’ in the node’s deconstructor?

Thanks for your time.

  • 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-05-20T07:16:19+00:00Added an answer on May 20, 2026 at 7:16 am

    First of all, you should not call an objects destructor directly, unless you’re writing an allocator and used placement new when creating it. Second, you should delete and not delete[] unless you also used new[]. And finally, delete this is a bad habit, but legal according to the standard. Why don’t you just call delete theNode instead of all of this?

    EDIT: Addressing some comments/additional questions.

    To allocate a single instance on heap, you use theNode = new Node. The returned pointer must be freed with delete theNode. Calling new will allocate memory and then call Node::Node(), the constructor, so that it can setup it’s internal state. Calling delete will call Node::~Node() and then free the allocated memory. The destructor is responsible for cleaning up anything Node uses, but not the memory used by Node itself.

    To allocate an array of nodes, you use theNodes = new Node[10];. You delete these with delete[] theNodes. Mixing new/delete with new[]/delete[] is undefined behaviour.

    Placement new is a method where you want to construct an object in already allocated memory. In this case, you have the only good reason for calling a destructor directly, you want to deconstruct an object (aka letting it clean itself up) without also freeing the memory allocated for it.

    Calling delete this is legal in e.g. a Suicide() function, as long as you do not refer to “this” or any members of the deleted instance after the call to delete this. This is a valid technique e.g. in reference counted objects, but is often considered something you should avoid unless you really need it.

    The correct solution for you is pretty plain, where you now call ~Node, simply call delete theNode instead.

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

Sidebar

Related Questions

I am currently working on a computer science project where I have to evaluate
I'm a first year computer science student. We are currently programming in java and
I am a newbie Computer Science high school student and I have trouble with
I'm a computer science student. Currently we are learning the Ada programming language. The
I am college student (computer science) and have just started a C# programming class.
I am a computer science student and taking my first C++ class. I have
I am a Computer Science Student and I am currently doing a 2d Game
first time poster here! I'm a Senior Computer Science Student and I'm currently developing
I'm a non-computer science student doing a history thesis that involves determining the frequency
I'm an undergrad Computer Science student and studying online. There is a lot 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.