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

  • Home
  • SEARCH
  • 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 942899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:20:50+00:00 2026-05-15T22:20:50+00:00

I have the following class: class Stack { struct Link { void* data; Link*

  • 0

I have the following class:

class Stack {
  struct Link {
    void* data;
    Link* next;
    void initialize(void* dat, Link* nxt);
  }* head;
public:
  void initialize();
  void push(void* dat);
  void* peek();
  void* pop();
  void cleanup();
};

The pop method is:

void* Stack::pop() {
  if(head == 0) return 0;
  void* result = head->data;
  Link* oldHead = head;
  head = head->next;
  delete oldHead;
  return result;
}

oldHead is a pointer to a struct Link, which has a void pointer as member. So by deleting oldHead I’m implicitly deleting that void pointer, right?

I’m reading Thinking in C++ by Bruce Eckel, and it says that deleting void pointers doesn’t clean things up properly because delete needs to know the type of the pointer.

This code is implicitly deleting the void pointer data, so: Can someone explain why is this (implicit) way of deleting a void pointer different from deleting with delete <void pointer>?

  • 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-15T22:20:51+00:00Added an answer on May 15, 2026 at 10:20 pm

    Your terminology is causing ambiguity, but let me explain. Let’s say you have:

    struct foo
    {
        void* bar;
    };
    

    Whenever a foo ends its lifetime, bar simply stops existing too. So if you have:

    {
        foo f = { new int; }
    }
    

    You’ve leaked, as new int is never deleted. Likewise, when you do:

    {
        foo* f = new foo;
        f->bar = new int;
        delete f;
    }
    

    You’ve still leaked, since when delete f is run, you simply end the lifetime of what f is pointing to (just like what happened automatically above), ergo bar simply ceases to exist and the new int is not deleted.

    To summarize, when an object’s lifetimes ends, delete is not called on the members that are a pointer.

    So when you call delete on a Link, it’s the same situation as bar in foo above: you’re deleteing the memory for a Link causing data to stop existing, but not actually deleting what it’s pointing at.

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

Sidebar

Related Questions

I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
I have encountered the following code: class a { public: void * operator new(size_t
I have the following code: import java.net.InetAddress; public class lookup { public static void
I have following class (as seen through reflector) public class W : IDisposable {
I have following test class @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {/services-test-config.xml}) public class MySericeTest { @Autowired
I have the following class public class MenuVeiculo { public string Nome { get;
I have the following class: #include <string> #include <stack> #include <queue> #include map.h using
We have the following shared component: public class OurServiceBase : System.ServiceProcess.ServiceBase This class has
I have a base class with the following (trimmed for brevity) declaration: public abstract
I want to have something like this below: template <class T> struct Container{ public:

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.