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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:41:00+00:00 2026-06-03T16:41:00+00:00

I am learning C, mainly by K&R, but now I have found an Object

  • 0

I am learning C, mainly by K&R, but now I have found an Object Oriented C pdf tutorial and am fascinated. I’m going through it, but my C skills/knowledge may not be up to the task.
This is the tutorial: http://www.planetpdf.com/codecuts/pdfs/ooc.pdf

My question comes from looking at many different functions in the first couple of chapters of the pdf. Below is one of them. (page 14 of pdf)

void delete(void * self){
     const struct Class ** cp = self;

     if (self&&*cp&&(*cp)->dtor)
                self = (*cp)->dtor(self);
     free(self);

 }

dtor is a destructor function pointer. But knowledge of this isn’t really necessary for my questions.

  • My first question is, why is **cp constant? Is it necessary or just being thorough so the code writer doesn’t do anything damaging by accident?
  • Secondly, why is cp a pointer-to-a-pointer (double asterisk?). The struct class was defined on page 12 of the pdf. I don’t understand why it can’t be a single pointer, since we are casting the self pointer to a Class pointer, it seems.
  • Thirdly, how is a void pointer being changed to a Class pointer (or pointer-to-a-Class-pointer)? I think this question most shows my lack of understanding of C. What I imagine in my head is a void pointer taking up a set amount of memory, but it must be less than Class pointer, because a Class has a lot of “stuff” in it. I know a void pointer can be “cast” to another type of pointer, but I don’t understand how, since there may not be enough memory to perform this.

Thanks in advance

  • 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-03T16:41:03+00:00Added an answer on June 3, 2026 at 4:41 pm

    Interesting pdf.

    My first question is, why is **cp constant? Is it necessary or just
    being thorough so the code writer doesn’t do anything damaging by
    accident?

    It’s necessary so the writer doesn’t do anything by accident, yes, and to communicate something about the nature of the pointer and its use to the reader of the code.

    Secondly, why is cp a pointer-to-a-pointer (double asterisk?). The
    struct class was defined on page 12 of the pdf. I don’t understand why
    it can’t be a single pointer, since we are casting the self pointer to
    a Class pointer, it seems.

    Take a look at the definition of new() (pg 13) where the pointer p is created (the same pointer that’s passed as self to delete()):

    void * new (const void * _class, ...)
    {
        const struct Class * class = _class;
        void * p = calloc(1, class —> size);
        * (const struct Class **) p = class;
    

    So, ‘p’ is allocated space, then dereferenced and assigned a pointer value (the address in class; this is like dereferencing and assigning to an int pointer, but instead of an int, we’re assigning an address). This means the first thing in p is a pointer to its class definition. However, p was allocated space for more than just that (it will also hold the object’s instance data). Now consider delete() again:

     const struct Class ** cp = self;
     if (self&&*cp&&(*cp)->dtor)
    

    When cp is dereferenced, since it was a pointer to a pointer, it’s now a pointer. What does a pointer contain? An address. What address? The pointer to the class definition that’s at the beginning of the block pointed to by p.

    This is sort of clever, because p’s not really a pointer to a pointer — it has a larger chunk of memory allocated which contains the specific object data. However, at the very beginning of that block is an address (the address of the class definition), so if p is dereferenced into a pointer (via casting or cp), you have access to that definition. So, the class definition exists only in one place, but each instance of that class contains a reference to the definition. Make sense? It would be clearer if p were typed as a struct like this:

    struct object {
      struct class *class;
        [...]
    };
    

    Then you could just use something like p->class->dtor() instead of the existing code in delete(). However, this would mess up and complicate the larger picture.

    Thirdly, how is a void pointer being changed to a Class pointer (or
    pointer-to-a-Class-pointer)? I think this question most shows my lack
    of understanding of C. What I imagine in my head is a void pointer
    taking up a set amount of memory, but it must be less than Class
    pointer, because a Class has a lot of “stuff” in it.

    A pointer is like an int — it has a small, set size for holding a value. That value is a memory address. When you dereference a pointer (via * or ->) what you are accessing is the memory at that address. But since memory addresses are all the same length (eg, 8 bytes on a 64-bit system) pointers themselves are all the same size regardless of type. This is how the magic of the object pointer ‘p’ worked. To re-iterate: the first thing in the block of memory p points to is an address, which allows it to function as a pointer to a pointer, and when that is dereferenced, you get the block of memory containing the class definition, which is separate from the instance data in p.

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

Sidebar

Related Questions

I'm learning about ASP.NET, mainly by following through a book, but also making an
I'm developing a Grails web application (mainly as a learning exercise). I have previously
I have a folder. With folders. With files (mainly .pngs). For learning purposes, I
I've recently been learning about functional programming (specifically Haskell, but I've gone through tutorials
I have mainly a C++ background and I am learning C#. So, I need
I am a Flex developer and now I have started learning the fundamentals of
I've been learning programming for some months now, mainly with java and C#. They
I'm learning Corona SDK and am new to lua as well (i mainly do
I'm working on building a pretty simple site mainly as an exercise in learning
Learning xml, Can anyone help me? I have following XML code: **<book lang=en>name 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.