Thread: Need help on pointer to VOID

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    85

    Need help on pointer to VOID

    Can anyone explain concisely about pointer variable which points to VOID such as:

    void * void_prt;
    How does memory space set up and allocate for pointer variable? Does it point to nothing memory chunks or to NULL???
    I will appriaciate for your explaining.
    DV007

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well do you understand what a char *ptr is?

    The only difference when it comes to void* pointers is that you can't dereference a void pointer (and you can't do sizeof(void) either).

    For a char *ptr, this is OK
    char c, *p = &c;
    *p = 'a';

    The only difference is that you can't do
    *p = 'a';
    with a void pointer

    You might say
    void *p = malloc( 20 );
    You know p points to 20 bytes, but there is nothing you can say about the content of those 20 bytes (they're undefined anyway), since you can never do a *p type operation to look at what the pointer points to.

    A NULL pointer is a pointer to nothing (as opposed to a pointer to 20 bytes which you know nothing about)

    You can have a NULL pointer of any pointer type
    char *p = NULL;
    void *q = NULL;

    You can't dereference NULL pointers of any type either
    *p = 'a';
    If p was char*, then it would likely fail at run-time with some kind of protection exception.
    If p was void*, the compiler would complain about trying to dereference a void pointer.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    85
    Thanx for your help, Salem.
    But base on what you are explaining, how can you
    know the adress of a VOID pointer variable?
    And since you can't dereference it, then it seem
    like a unuseful data type for creating data structures

    I have seen pointers to VOID usually use in
    real-time embedded programs. Do you know why?
    What the main purpose of using Pointers To VOID?
    and when (in what case) do we need to use it?
    Again, Gracias!
    DV007

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    void *v;

    printf( "%d", &v );

    You actually can dereference a void pointer. You just have to type cast it to whatever type you're dereferencing it as. If it points to an int, dereference it as if you were using a pointer to an integer by type casting.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Former Member
    Join Date
    Oct 2001
    Posts
    955
    of course you can dereference void pointers! that's how the ANSI qsort() (excellent function ) works

    if you want to know what it's pointing to (and you would like to see it as an integer), you do

    Code:
    int a;
    void* p;
    
    ... do all your stuff here
    
    a= *((int *)p);
    same for structs

    Code:
    struct mystruct
    {
        int a;
        char b;
        double x;
    };
    
    mystruct p;
    double a;
    void* q;
    
    ...do all your stuff in here
    //if q points to p, you can do
    
    a=((mystruct*)q)->x;
    
    //or
    
    a=(*((mystruct*)q)).x;
    I'm not sure if these work in C (C++ is ok), but I'm sure you get the idea

    Oskilian

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    85
    Thanxs a lot, guys. Y'all are really helpfull.
    It seems like Pointer to VOID has a good advantage
    in data manipulation??? because you can type cast to it with different data type(int, float, char, struct..), but since C is not STRONG type-checking
    and no GARBAGE collection, I think it's dangerous
    to use or it needs very careful to use in those
    programs involve with human-live risk such as
    Fligh Control application. Do you guys agree?
    It's nice to discuss with y'all.

    DV007

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > of course you can dereference void pointers! that's how the ANSI qsort()
    qsort receives void* pointers, but they have to be cast back into something else before they can be used

    The main advantage of void* (eq in qsort), is that you don't have to worry (too much) about the type of the thing you want to sort (qsort doesn't care about the type, only that it has an order)

    Same goes for general purpose routines like memmove, memcpy.

    > and no GARBAGE collection
    You still need to manage calls to malloc/free - which is a different problem to whether something is void* or not

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    13

    abt VOID *

    hey guys

    Quzah seems to be a the master of C......

    i feel VOID pointers can be used where u have to decide the type of pointer u need at the runtime ,in such cases u declare tht as a pointer to void and type cast it at runtime ,to whtever type u need, int ,char , float ,anything.

    and sizeof (void*) doesnt make sense, it is absurd

    also void* a and a++ is also absurd

  9. #9
    Unregistered
    Guest

    Re: abt VOID *

    Originally posted by ucsbme
    hey guys

    Quzah seems to be a the master of C......

    i feel VOID pointers can be used where u have to decide the type of pointer u need at the runtime ,in such cases u declare tht as a pointer to void and type cast it at runtime ,to whtever type u need, int ,char , float ,anything.

    and sizeof (void*) doesnt make sense, it is absurd

    also void* a and a++ is also absurd
    every object has a size , sizeof(void *) makes perfect sense ,
    i.e.,
    if I declare
    void *p;
    p is an object , it takes up some size in memory , sizeof (p) or sizeof(void *) will tell us how many bytes it takes up
    sizeof(void) doesnt make sense though , as there is no type named void , if sizeof(void *) didnt make sense we would not be able to dynamically allocate a particular number of void size pointers .
    say , I need 20 void * pointers
    so, I do

    void **ppvoid ;
    ppvoid = malloc(20*sizeof(*ppvoid));

    ppvoid[0], ppvoid[1], ....... ppvoid[19] would give me the 20 pointers to void I need

    I wish we had some other syntax for referring to generic pointers .

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: Re: abt VOID *

    Originally posted by Unregistered

    every object has a size , sizeof(void *) makes perfect sense ,
    i.e.,
    if I declare
    void *p;
    p is an object , it takes up some size in memory , sizeof (p) or sizeof(void *) will tell us how many bytes it takes up
    sizeof(void) doesnt make sense though , as there is no type named void , if sizeof(void *) didnt make sense we would not be able to dynamically allocate a particular number of void size pointers .
    Hm. Close. 'sizeof( void* )' only give you the size of the pointer. It doesn't give you the size of what it points at, because what it poitns at could be anything.

    All pointers take up exactly the same amount of space, IIRC. It is simply a memory address. This is basicly why void poitners work.

    Prelude states that sizeof doesn't work correctly with dynamicly allocated memory. I suspect this goes doubly for trying to get the size of something a void pointer points at.

    That said, no, you cannot simply:

    int x;
    void *v = &x;

    sizeof( v );

    ...and expect it to tell you the size of the integer (object) it points at. In this case, the size would like ly happen to be the same, because the size of an ingeter on a 32 bit compiler happens to be the same size as a pointer.

    Code:
    int main ( void )
    {
        printf("%d\n", sizeof( int ) );
        printf("%d\n", sizeof( int* ) );
        printf("%d\n", sizeof( char* ) );
        printf("%d\n", sizeof( float* ) );
        printf("%d\n", sizeof( void* ) );
         return 0;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I suspect this goes doubly for trying to get the size of something a void pointer points at.
    It does, in a way. If you try to get the size of a void pointer then you will simply get the size of a pointer, but if you cast and dereference a void pointer then sizeof will return the size of the data type you cast the void pointer to. Pretty straight forward in it's own twisted way, but what's really interesting is when you dereference a void pointer to a char pointer you get the size of a char and not a char pointer. Here's a fun little test to see just what can happen:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main ( void )
    {
      char c = 'a', *cp;
      double f = 1.2f, *fp;
      void *vf, *vfp, *vc, *vcp;
    
      fp = malloc ( sizeof ( double ) );
      cp = malloc ( 10 * sizeof ( char ) );
      if ( fp != NULL && cp != NULL ) {
        *fp = 2.3f;
        strcpy ( cp, "Test" );
    
        vf = &f;
        vfp = fp;
        vc = &c;
        vcp = cp;
    
        printf ( "Test 1: %u %u\n", sizeof f, sizeof *fp );
        printf ( "Test 2: %u %u\n", sizeof f, sizeof fp );
        printf ( "Test 3: %u %u\n", sizeof vf, sizeof vfp );
        printf ( "Test 4: %u %u\n", sizeof *(double *)vf, sizeof *(double *)vfp );
        printf ( "Test 5: %u %u\n\n", sizeof *(float *)vf, sizeof *(float *)vfp );
    
        printf ( "Test 6: %u %u\n", sizeof c, sizeof *cp );
        printf ( "Test 7: %u %u\n", sizeof c, sizeof cp );
        printf ( "Test 8: %u %u\n", sizeof *(char *)vc, sizeof *(char *)vcp );
        printf ( "Test 9: %u %u\n", sizeof *(double *)vc, sizeof *(double *)vcp );
        printf ( "Test 10: %u %u\n", sizeof *(char *)vc, sizeof *(char *)vcp );
    
        free ( fp );
        free ( cp );
      }
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  12. #12
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284

    Re: Re: Re: abt VOID *

    Why do you misrepresent what I wrote ?
    sizeof (a) will tell you what the size of a is and not what it points to (I did not imply that either directly or indirectly ) a might not even be a pointer ,similarly sizeof(void *) tells you what the sizeof of a "void *" is (which happens to be a pointer type), and since there are objects of type "void *" , it will tell you how much space one object of type "void *" will take ,just as sizeof(int *) will tell what the size of an object of type "int *" is ,not the size of int . sizeof works perfectly as it is supposed to , nothing breaks down in case of dynamically allocated memory .

    Originally posted by quzah


    Hm. Close. 'sizeof( void* )' only give you the size of the pointer. It doesn't give you the size of what it points at, because what it poitns at could be anything.




    Quzah.
    It does not do so because sizeof is not supposed to do so.
    pinko
    The one who says it cannot be done should never interrupt the one who is doing it.

  13. #13
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by Prelude
    >I suspect this goes doubly for trying to get the size of something a void pointer points at.
    It does, in a way. If you try to get the size of a void pointer then you will simply get the size of a pointer, but if you cast and dereference a void pointer then sizeof will return the size of the data type you cast the void pointer to. Pretty straight forward in it's own twisted way, but what's really interesting is when you dereference a void pointer to a char pointer you get the size of a char and not a char pointer. Here's a fun little test to see just what can happen:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main ( void )
    {
      char c = 'a', *cp;
      double f = 1.2f, *fp;
      void *vf, *vfp, *vc, *vcp;
    
      fp = malloc ( sizeof ( double ) );
      cp = malloc ( 10 * sizeof ( char ) );
      if ( fp != NULL && cp != NULL ) {
        *fp = 2.3f;
        strcpy ( cp, "Test" );
    
        vf = &f;
        vfp = fp;
        vc = &c;
        vcp = cp;
    
        printf ( "Test 1: %u %u\n", sizeof f, sizeof *fp );
        printf ( "Test 2: %u %u\n", sizeof f, sizeof fp );
        printf ( "Test 3: %u %u\n", sizeof vf, sizeof vfp );
        printf ( "Test 4: %u %u\n", sizeof *(double *)vf, sizeof *(double *)vfp );
        printf ( "Test 5: %u %u\n\n", sizeof *(float *)vf, sizeof *(float *)vfp );
    
        printf ( "Test 6: %u %u\n", sizeof c, sizeof *cp );
        printf ( "Test 7: %u %u\n", sizeof c, sizeof cp );
        printf ( "Test 8: %u %u\n", sizeof *(char *)vc, sizeof *(char *)vcp );
        printf ( "Test 9: %u %u\n", sizeof *(double *)vc, sizeof *(double *)vcp );
        printf ( "Test 10: %u %u\n", sizeof *(char *)vc, sizeof *(char *)vcp );
    
        free ( fp );
        free ( cp );
      }
      return 0;
    }
    -Prelude
    I think this fits in with the general rule

    if you have something like

    double *d;
    sizeof ( (char *) d ) will be the same as sizeof( char *)
    because (char *) d considered as a rvalue is of type "char *",

    and similarly
    sizeof( ( * ( (char *) d) ) ) would be 1
    as *( (char *) d ) is of type char considered as a rvalue

    another point , I think in C89 the only portable way of printing sizeof is using %lu and casting the sizeof to (unsigned long)
    The reason being that the result of sizeof is of type size_t , the only thing that is told about size_t is that it is an unsigned integral type , and since in C89 long is the largest integral type , there should be no problem with this .

    printf( "sizeof(a)=%lu", (unsigned long) sizeof(a) ) ;
    in C99 one can use %zd and no ugly casts required .

    The only behavior of sizeof which might confuse some newbies is that if we have the declaration
    char a[20];
    then sizeof( a) will be 20 which most probably will be different from sizeof( (char *) ) , even here the general rule is not broken as a is not of type char * , but of type char [20]

    and sizeof(char [20] ) is defined to be 20*sizeof(char) = 20
    Last edited by pinko_liberal; 07-08-2002 at 10:52 PM.
    The one who says it cannot be done should never interrupt the one who is doing it.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: Re: Re: Re: abt VOID *

    Originally posted by pinko_liberal
    Why do you misrepresent what I wrote ?
    First off, I don't see you posting anything here. On that, I'll assume that you were "unregistered".

    I didn't "misrepresent" what you wrote. I misread it, or at the time wasn't clear on what you were trying to get across.

    My reply was not some personal attack on unknown. I was trying to clarify the issue.

    You'll see how I could have possibly misunderstood your point, when your sentences come out like:

    It does not do so because sizeof is not supposed to do so.
    Case and point. This is not only an awkward statement, it's also untrue. Or rather, again it can be misinterpreted.

    'sizeof' is supposed to return the size of anything passed to it. If you pass it a pointer, it returns the size of the pointer. If you pass an object, it returns the size of the object. That is all, nothing more.

    What I was trying to clarify is that exact line of thought. And for the record, there is a type of 'void'. You just cannot create instances of it. If there weren't 'void', how could you use it as return values and parameters?

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    May 2002
    Posts
    85
    I want to say that all of you guys are really helpful and great. Thanx for sharing your ideas
    and knowledge. I encourage you to debate in respect one another, but no intend for raising
    a STAR WAR!
    Back to the question I posted, can you guys show
    how to access and dereference a VOID pointer?
    No one has illustrate this point clearly so far!

    void *vptr;
    int a = 5;
    int b;
    vptr = &a;
    printf("vprt is %d", *vprt); /Error, can't dereference a void pointer*/

    b = *( (int*)vptr );
    *vptr = 10; /*Error too, can't assign env vptr now is point to INT */

    Kinda confuse!
    DV007
    Last edited by dv007; 07-09-2002 at 04:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. saying hello and 1st question
    By darksys in forum C Programming
    Replies: 12
    Last Post: 10-31-2008, 02:58 PM
  2. How to better manage large .cpp files
    By 39ster in forum C++ Programming
    Replies: 6
    Last Post: 08-25-2008, 08:24 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM