Thread: problem with generic pointers

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    60

    problem with generic pointers

    hey guys, I have 2 programs I am trying to figure out and the generic void pointers are really throwing me off. I realize the values are being swapped in the function but I don't understand how a=4 and b=7 are being sent in and then there in an array. When the numbers are passed in are they being converted to chars? If so what is 4 becoming?

    Code:
    // Generic Pointers
    
     #include <iostream>
     #include <iomanip>
     using namespace std;
    
     void q(void *a, void *b, int n) {
    
         unsigned char *ca = (unsigned char *)a;
         unsigned char *cb = (unsigned char *)b;
         unsigned char c;
    
         for (int i = 0; i < n; i++) {
             c     = ca[i];
             ca[i] = cb[i];
             cb[i] = c;
         }
     }
    
     int main() {
    
         int a = 4, b = 7;
         float x = 2.5f, y = 3.5f;
    
         q(&a, &b, sizeof(int));
         q(&x, &y, sizeof(float));
    
         cout << setprecision(1) << fixed;
         cout << "(a,b) = (" << a << ',' << b << ')' << endl;
         cout << "(x,y) = (" << x << ',' << y << ')' << endl; 
     }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Basically, the idea is to swap the values byte by byte.

    In practice, we are more likely to use std::swap rather than such a byte copy swap because a correct swap of objects of class type may involve more than just swapping byte by byte.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    That's because no "array's" are being passed in. You are passing in the memory location of a and b, not their actual values. Two pointers are required by q, and each call to it in the function main provides it with an address of some data. Since q is supposed to be "generic" in the sense that it should work with different sized data types, we also need to let q know how many bytes a and b are valid for from their base address. q() itself steps through the data a byte at a time up to n swapping the data. The "array's" you speak of are a way to dereference a block of memory at a specified offset. So it can be seen as a sort of shorthand for: (base address pointed to by ca) + (i * sizeof(element of ca)). In this case the size of each element is a byte.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    60
    so for example when c=ca[i] is happening, it is passing in the memory location of the 4?. And at that point I just have to realize a swap is happening because I won't know the actual values of the memory location?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Thegame16
    so for example when c=ca[i] is happening, it is passing in the memory location of the 4?.
    No, the passing in of the memory location happens at the function call, not that point.

    Quote Originally Posted by Thegame16
    And at that point I just have to realize a swap is happening because I won't know the actual values of the memory location?
    That two bytes are being swapped should be obvious as the swap implementation is canonical. What is perhaps less obvious to you is that the idea is to treat the object, be it an int or a float, as an array of bytes, in order to swap the objects byte by byte.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generic Trees Functions Using Pointers to Function
    By rikehmmc in forum C Programming
    Replies: 31
    Last Post: 05-15-2011, 11:44 AM
  2. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  3. Generic Pointers to structures
    By dunxton in forum C Programming
    Replies: 8
    Last Post: 02-20-2009, 10:23 AM
  4. Problem with generic function
    By jlbfunes in forum C++ Programming
    Replies: 23
    Last Post: 08-17-2008, 05:13 PM
  5. Problem with malloc and pointers to pointers
    By mike_g in forum C Programming
    Replies: 7
    Last Post: 03-29-2008, 06:03 PM