Trouble in understanding void pointers workings
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Trouble in understanding void pointers workings

  1. #1
    Join Date
    Sep 2013
    Posts
    4

    Trouble in understanding void pointers workings

    Hello everybody, I am a beginner c/c++ wannabe programmer who is currently taking his first course on it. I was confused about pointers so I read the article on them on cplusplus.com, and while it clarified them a great deal, towards the very end I encountered some code that no matter how hard I tried, just couldn't make any sense as to why it works. Here it is:

    Code:
    #include <iostream>
    using namespace std;
    void increase (void* data, int psize)
    {
      if ( psize == sizeof(char) )
      { char* pchar; pchar=(char*)data; ++(*pchar); }
      else if (psize == sizeof(int) )
      { int* pint; pint=(int*)data; ++(*pint); }
    }
    int main ()
    {
      char a = 'x';
      int b = 1602;
      increase (&a,sizeof(a));
      increase (&b,sizeof(b));
      cout << a << ", " << b << endl;
      return 0;
    }
    This code returns: y, 1603.

    The particular issue that I have here is with the definition of the function increase; let's examine it's behavior when it's first argument is an int: it checks to see if it's input psize is the size of int. All right. It then creates a pointer called pint. Great. Now, it assigns to the POINTER pint the VALUE to which the pointer "data" points at, which... doesn't make sense at all to me! Doesn't that mean the the pointer now points to an address in memory that is equal to the value of "data"? And what in the world is at that address? The function then dereferences pint and therefore increases the value of what's pointed at by pint by 1, that part I understand. BUT, how comes that the value of what's pointed at by pint is the value of data?! - I have already shown before how I came to the conclusion that before executing ++(*pint), the pointer pint is changed to point to the location in memory with the value of what's pointed at by the pointer data - and who knows what's at that location in memory! (but it certainly isn't the value of data).

    I am sorry for this long text, but I found no other/shorter way of explaining my problem/incapacity to understand. I apologize if this might seem foolish if you're an experienced programmer, but again, I'm a novice and I'm really trying to understand and break down everything to it's basics until I start actually using it. That being said, could you please set me straight on the explained above?

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,095

    Re: Trouble in understanding void pointers workings

    Code:
     
    if ( psize == sizeof(char) ) //checks if the pointer points to a char
    { 
        char* pchar; //creates an uninitialized pointer to a char
        pchar=(char*)data; // sets pchar point to the data passed in as an argument
        ++(*pchar);  // increments whatever pchar points to, in this case the letter 'x'
    }
     else if (psize == sizeof(int) ) // checks to see if it's the size of an int
    {
        int* pint;//creates an int pointer
        pint=(int*)data; //assigns pint to point to data
        ++(*pint); // increments the int that pint points to by 1
    }
    Note that relying on sizeof to determine the data type is terrible coding and not something you should actually do.

  3. #3
    Join Date
    Sep 2013
    Posts
    4

    Re: Trouble in understanding void pointers workings

    Thank you for your respnse!
    What you said is
    Code:
    pchar=(char*)data; // sets pchar point to the data passed in as an argument
    What I don't understand is WHY does it work that way. The way I see and understand it is this:
    1. pchar is a pointer, so it points to an address in memory.
    2. (char*)data means that we are dereferencing the pointer "data" so as to obtain it's value.
    3. if we set pchar=(char*)data, then pchar no longer points to *data (the value of what's pointed at by the pointer "data"), instead, it points some other location in memory which has THE ADDRESS equal to the value of what's pointed at by the pointer "data".
    Please tell me where my logic breaks down.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,095

    Re: Trouble in understanding void pointers workings

    Quote Originally Posted by CatAnion View Post
    Thank you for your respnse!
    What you said is
    Code:
    pchar=(char*)data; // sets pchar point to the data passed in as an argument
    What I don't understand is WHY does it work that way. The way I see and understand it is this:
    1. pchar is a pointer, so it points to an address in memory.
    2. (char*)data means that we are dereferencing the pointer "data" so as to obtain it's value.
    3. if we set pchar=(char*)data, then pchar no longer points to *data (the value of what's pointed at by the pointer "data"), instead, it points some other location in memory which has THE ADDRESS equal to the value of what's pointed at by the pointer "data".
    Please tell me where my logic breaks down.
    pChar and data are both pointers. They contain an address in memory. Just as if you assigned one int to another it would contain the same value, assigning the value of one pointer to another gives them the save value. They point to the same thing after the assignment. He's casting data to a char* so that the compiler knows how to dereference it.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,095

    Re: Trouble in understanding void pointers workings

    Quote Originally Posted by CatAnion View Post
    2. (char*)data means that we are dereferencing the pointer "data" so as to obtain it's value.
    This incorrect too. (char*)data is called casting. It's telling the compiler to ignore the type that was declared and treat it as if it were a char*.

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,381

    Re: Trouble in understanding void pointers workings

    Lets take a simpler example - one that just increments an int

    Code:
    void increase (int* data)
    {
    int* pint;
    
        pint = (int*)data;
        ++(*pint);
    }
    The function increase takes one argument - data - which is defined as a pointer to an int. So data holds a value which is a memory address at which an integer value is stored.

    pint is defined as a pointer to an int, But the memory address at which this int is held has not been set.

    pint is set equal to data. pint now points to the same memory location as data points. It is cast using a 'c style' cast to a pointer to an int. In this simple example this cast is not needed as both data and pint are defined as pointer to int. However in the original example, data was a type pointer to void so this case was needed.

    *pint obtains the data stored at the memory location pointed to by pint (dereferences pint). Then the value stored at the memory location is incremented by 1.

    I hope this helps clear up your problem. Understanding memory reference in c/c++ can be difficult at first until 'the penny drops' and it becomes clear.

    I agree with GCDEF in post #2. The code is not a good example of using pointers. You might also like to look at
    http://www.learncpp.com/cpp-tutorial...n-to-pointers/
    http://www.tutorialspoint.com/cplusp...p_pointers.htm
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  7. #7
    Join Date
    Sep 2013
    Posts
    4

    Re: Trouble in understanding void pointers workings

    This is where I was having the issue - casting was not something explained in that article or in my course (at least not properly). Now I understand. One more thing: if (char*)data is casting, then what would dereferencing the pointer look like?

  8. #8
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,381

    Re: Trouble in understanding void pointers workings

    Quote Originally Posted by CatAnion View Post
    This is where I was having the issue - casting was not something explained in that article or in my course (at least not properly). Now I understand. One more thing: if (char*)data is casting, then what would dereferencing the pointer look like?

    Code:
    int *pint; //This is a pointer to an int
    
    int value;  //This is an integer
    
       pint = (int *)data;  //data is 'casted' to int * - ie pointer to an int
    
       value = *pint;       //value is the data to which pint points - pint dereferenced
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Trouble in understanding void pointers workings

    Quote Originally Posted by CatAnion View Post
    This is where I was having the issue - casting was not something explained in that article or in my course (at least not properly). Now I understand. One more thing: if (char*)data is casting, then what would dereferencing the pointer look like?
    Code:
    *((char *)data);
    Also, as soon as you understand what your program is doing, throw the program away, and quickly. As GCDEF pointed out, you should never use sizeof() to determine what to cast a type to.
    Code:
    #include <iostream>
    using namespace std;
    
    template <typename T>
    void increase (T* data)
    {
        ++(*data);
    }
    
    int main ()
    {
      char a = 'x';
      int b = 1602;
      increase (&a);
      increase (&b);
      cout << a << ", " << b << endl;
      return 0;
    }
    As long as the pre-increment "++" is defined for type T, the code will work.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Sep 2013
    Posts
    4

    Re: Trouble in understanding void pointers workings

    I understand now. Thank you for your replies guys!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center