Thread: Can anybody explain this line?

  1. #1
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476

    Wink Can anybody explain this line?

    Hi, can anybody explain this code? I saw this on a proprietary 3rd party library so I don't quite understand what it is trying to achieve. I don't understand the reason for the reinterpret casting. It seems redundant (CMIIW). Why would anyone want to do that? Thanks in advance.

    Code:
    ...
    private:
    T *freeListHead;
    
    T *myfunction()
    {
        ...
        freeListHead = *(reinterpret_cast<T**>(freeListHead));
        return freeListHead ;
    }
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It's not redundant, it's treating the freeListHead variable as if it had a different level of indirection. The code must have a linked list where the next pointer is basically at the same address as the item pointed to. That is not the clearest way to write it, but it works. I've done it that way before I learnt how to do it cleaner.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by iMalc View Post
    It's not redundant, it's treating the freeListHead variable as if it had a different level of indirection. The code must have a linked list where the next pointer is basically at the same address as the item pointed to. That is not the clearest way to write it, but it works. I've done it that way before I learnt how to do it cleaner.
    You're right. Silly me for not noticing earlier. It is a linked list. And a peculiar one I must say.

    Thanks. Consider the problem solved.

    Now, back to the memory leak problem. The debugging never ends.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  2. Finding carriage returns (\c) in a line
    By JizJizJiz in forum C++ Programming
    Replies: 37
    Last Post: 07-19-2006, 05:44 PM
  3. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  4. could someone explain this line to me please
    By hostensteffa in forum C Programming
    Replies: 2
    Last Post: 06-23-2002, 10:10 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM