Thread: not able to understand this tiny tiny method

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    219

    not able to understand this tiny tiny method

    I just saw a tiny method called concrete which is an one line function but I am not able to understand what it does but I am Interested.

    http://cep.xor.aps.anl.gov/software/...ce.html#l00130

    Look at Line 00130

    Code:
    static inline Node *concrete(QMapData::Node *node) {
      return reinterpret_cast<Node *>(reinterpret_cast<char *>(node) - payload());
    }
    payload() will return an Integer but why reinterpret_cast<char *>(node) - payload() ??
    does reinterpret_cast returns an integer ??
    and why cast an Integer to a pointer to an object ??
    Last edited by noobcpp; 10-20-2008 at 10:00 AM.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    It does this so that it moves the pointer over by byte units, then casts that pointer to a node.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Sorry didn't understand correctly.
    I can understand why it cats char* instead of void* is Qt's business logic and that doesn't makes much headech.
    Oh! cause Its 1 byte.

    but why it does reinterpret_cast<char *>(node) - payload()
    is not understood yet.

    does reinterpret_cast returns an integer ??
    if it returns a pointer (I belive it does)
    It could just do node--

    Is it because QMapData::Node contains another data *forward[1]; ??

    But If so return value of payload() is not same of the size of Node so shouldn't it crash due to orphan pointer access ??
    Last edited by noobcpp; 10-20-2008 at 10:29 AM.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Lets say we have 32-bit sized memory objects

    Code:
    [0123][4567][89AB]
    What if I wanted to place my pointer to one of thse objects right

    Code:
    [0123][4567][89AB]
        ^
    There. How can I possibly do that? What if I called the pointer a char instead. That way I can move over an arbitary number of bytes instead of moving in 4-byte increments. Get it now?

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Thanks understood now.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yeah no problem. Its actually a reasonably common practice to move pointers like that for alignment purposes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 07:50 PM
  2. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-08-2008, 12:30 AM
  3. C# method
    By siten0308 in forum C# Programming
    Replies: 6
    Last Post: 07-15-2008, 07:01 AM
  4. Overriding a method in C
    By DavidDobson in forum C Programming
    Replies: 1
    Last Post: 07-05-2008, 07:51 AM
  5. Replies: 2
    Last Post: 01-22-2008, 04:22 PM