Thread: Using a void*

  1. #1
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256

    Using a void*

    Can someone explain to me how to use a void* as a function argument that can accept any type of pointer, like realloc() and free()? I'm guessing you can't dereference it directly, since that's where my error is coming in.
    Code:
    void function(void)
     {
      function();
     }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Correct, you can't dereference a pointer to void, nor can you use pointer arithmetic on one because it lacks size information. You'll most likely want to convert the void pointer to a char pointer, and also pass the size of the actual type so that you can do block accesses. The only downside is that you have to do the tricky size calculations manually:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void rotate_at ( void *a, int n_elem, int n, int at )
    {
      char *p = a;
      char *save = malloc ( n * at );
      int inc = n * at;
    
      if ( save == NULL )
        return;
    
      /* Swap blocks */
      memcpy ( save, p, inc );
      memmove ( p, p + inc, n * ( n_elem - at ) );
      memcpy ( p + inc, save, inc );
    
      free ( save );
    }
    
    int main ( void )
    {
      int a[4] = {1,2,3,4};
      int i;
    
      for ( i = 0; i < 4; i++ )
        printf ( "%d ", a[i] );
      printf ( "\n" );
    
      rotate_at ( a, 4, sizeof a[0], 2 );
    
      for ( i = 0; i < 4; i++ )
        printf ( "%d ", a[i] );
      printf ( "\n" );
    
      return 0;
    }
    My best code is written with the delete key.

  3. #3
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Well, I'm trying to write a library function for myself that sets each member of an array to an argued value. The only way I can think of to do this is to copy the actual binary data of the argued value into each array member. I notice the memcpy() function you used. Should this be the focus of my research? And how would I designate how many bytes to access at a time, if I also argue the size of each element?
    Code:
    void function(void)
     {
      function();
     }

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I'm trying to write a library function for myself that sets each member of an array to an argued value
    Basically a generic version of memset? The principle is the same. Convert the void pointer to a char pointer, copy N bytes of the value to a, then increment a by N bytes until you get to the end of the array. The function is pretty trivial; there's no real funky size stuff to deal with, and memcpy basically removes any complexity that there might be:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    /*
     * Fill p with n_elem copies of x
     */
    void gset ( void *p, void *x, size_t n_elem, size_t n )
    {
      char *a = p;
    
      while ( n_elem-- > 0 ) {
        memcpy ( a, x, n );
        a += n;
      }
    }
    
    int main ( void )
    {
      int a[5] = {0};
      int init = 10;
      int i;
    
      for ( i = 0; i < 5; i++ )
        printf ( "%d ", a[i] );
      printf ( "\n" );
    
      gset ( a, &init, 5, sizeof init );
    
      for ( i = 0; i < 5; i++ )
        printf ( "%d ", a[i] );
      printf ( "\n" );
    
      return 0;
    }
    >I notice the memcpy() function you used. Should this be the focus of my research?
    memcpy is for copying a block of bytes that don't overlap. If you're doing something like shifting part of an array into the other part, you need to use memmove, which is designed to handle overlapping memory.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed