Thread: casting struct pointer to void**

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    82

    casting struct pointer to void**

    Hello,

    This might be silly but I just came across a strange code. I don't have any problem understanding most of the code except the part where the author casts a structs pointer to void** before derefencing it.

    I have written some small test to see what effect this has but it doesn't look me like it is having any effect.

    Can someone shed some light on what exactly this the casting to void** is trying to achieve?

    Code:
    static void free_dc_attr( DC_ATTR *dc_attr )
    {
        struct dc_attr_bucket *bucket;
    
    
        pthread_mutex_lock( &dc_attr_lock );
    
    
        LIST_FOR_EACH_ENTRY( bucket, &dc_attr_buckets, struct dc_attr_bucket, entry )
        {
            if (bucket->entries > dc_attr || dc_attr >= bucket->next_unused) continue;
            *(void **)dc_attr = bucket->next_free;
            bucket->next_free = dc_attr;
            break;
        }
    
    
        pthread_mutex_unlock( &dc_attr_lock );
    }
    edit:

    DC_ATTR is just a struct and dc_attr is a valid pointer.

  2. #2
    Registered User
    Join Date
    Oct 2019
    Posts
    82
    Quote Originally Posted by ghoul View Post

    Can someone shed some light on what exactly this the casting to void** is trying to achieve?
    Okay - just disregard this. It seems to like I might not be providing enough information to figure out what is happen.

    But, yes, this is Wine code, if someone wants to investigate this further.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    It is reinterpreting a pointer to DC_ATTR into a pointer to void* and then storing an address at that location to make it part of a linked list.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    #define prn(x) printf(#x ": %p\n", (void*)x)
     
    struct Stuff {
        int a, b, c, d;
    };
     
    int main() {
        void *list = NULL;
     
        struct Stuff *a = malloc(sizeof *a);
        *(void**)a = list;
        list = a;
     
        struct Stuff *b = malloc(sizeof *b);
        *(void**)b = list;
        list = b;
     
        prn(a);
        prn(b);
        prn(list);
     
        struct Stuff *x = list;
        list = *(void**)list;
     
        x->a = 1;
        x->b = 2;
     
        prn(list);
     
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    Oct 2019
    Posts
    82
    Quote Originally Posted by john.c View Post
    It is reinterpreting a pointer to DC_ATTR into a pointer to void* and then storing an address at that location to make it part of a linked list.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    #define prn(x) printf(#x ": %p\n", (void*)x)
     
    struct Stuff {
        int a, b, c, d;
    };
     
    int main() {
        void *list = NULL;
     
        struct Stuff *a = malloc(sizeof *a);
        *(void**)a = list;
        list = a;
     
        struct Stuff *b = malloc(sizeof *b);
        *(void**)b = list;
        list = b;
     
        prn(a);
        prn(b);
        prn(list);
     
        struct Stuff *x = list;
        list = *(void**)list;
     
        x->a = 1;
        x->b = 2;
     
        prn(list);
     
        return 0;
    }
    Thanks, John C, as always.

    I guess that's about all I can say.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. casting from pointer to void
    By django in forum C Programming
    Replies: 7
    Last Post: 09-04-2011, 05:54 AM
  2. Casting to pointer to pointer to void
    By Sharke in forum C Programming
    Replies: 13
    Last Post: 05-12-2009, 08:40 PM
  3. Casting a pointer to a function to (void*)
    By genter in forum C++ Programming
    Replies: 12
    Last Post: 01-18-2009, 10:05 PM
  4. casting a void* to a pointer to a member function
    By Sebastiani in forum C++ Programming
    Replies: 13
    Last Post: 10-15-2002, 08:57 AM
  5. casting void*
    By Seron in forum C++ Programming
    Replies: 5
    Last Post: 12-25-2001, 08:02 AM

Tags for this Thread