Thread: Assigning to Void Pointer

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    65

    Assigning to Void Pointer

    Hello All,

    I'm using a C API (for MySQL) and there's some example code. What I don't understand is why the examples always typecast an int pointer to a char pointer, before assigning it to a void pointer. Here's an example:

    Code:
    /* Extract From mysql.h */
    
    typedef struct st_mysql_bind
    {
      .....................
      void        *buffer;      /* buffer to get/put data */
      .....................
    } MYSQL_BIND;
    Code:
    /* Extracts from Example */
    
    MYSQL_BIND bind[3];
    int int_data;
    
    bind[0].buffer = (char *)&int_data;
    Does this typecast have any effect or purpose?
    Thanks all.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Is there a
    #define void char
    somewhere tucked away to allow for people using impossibly old compilers?

    It could be just a relic from the dim and distant past before C got a void type.

    It certainly lacks any value in ANSI-C
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    I did a grep through the MySQL source dir... Didn't find any such define.

    As far as I know, pointers to different types may have different representations on some specific platforms, because those systems are designed to access memory in multiples of 2 or 4 bytes, etc. In this case, sizeof a pointer to char would have to be large than sizeof a pointer to int. But a void pointer should always have the same representation as a char pointer. I don't really see how any of this is relevant though.

    The thing is that it definitively doesn't look like an accident (it occurs in several examples), nor is the code old (comes from the official MySQL 5 Documentation). The actual type the pointer is supposed to be is provided earlier, like so:
    Code:
    bind[0].buffer_type= MYSQL_TYPE_LONG;
    bind[0].buffer= (char *)&int_data;
    bind[0].is_null= 0;

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    In this case, sizeof a pointer to char would have to be large than sizeof a pointer to int.
    I don't see why it can be
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    It has to be, because on those system, increasing the actual "number" of the pointer by the smallest amount possible, would mean that the pointer now points to memory 2 or 4 bytes further (not 1 byte further like on most systems). So if you want to get 1 byte precision with memory, you need more information than what is contained in a normal pointer. So, therefore, an int pointer would be smaller to store than a char pointer, if that system/compiler supports 1 byte char types.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by coder8137
    It has to be.
    No it has not. any pointer points a given byte in the memory.
    Sizeof pointer has nothing to do with number of bytes to be skipped when the pointer is increased for one
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    any pointer points a given byte in the memory.
    Yes, but NOT every byte in memory can be pointed to (on those systems).

    What I mean is that those systems are designed for accessing data that is larger than 1 byte primarily. Everything works in chunks of 4 bytes for example. In some of those systems there is than simply no way to access every byte directly. Basically, your pointers will only be able to point to memory addresses which are a multiples of 4. You can then get some int from that address. However, accessing a char, which is 1 byte, requires a different implementation or is not supported.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The cast can only make things worse, rather than better.

    Whether or not char* and int* have different representations, void* is capable of converting from the original pointer type and back to the original pointer type without loss of information.

    The same cannot be said of
    int * -> char * -> void * -> int *
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  4. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM