Thread: error on in algorithim

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    26

    error on in algorithim

    Code:
    struct block {
        int size;
        struct block *next;
        char data[1];
    };
    
    struct heap {
        int max;
        struct block *free[1];
    };
    
    struct block *GetBlock(int size, struct heap *hPtr);
    struct block *GetFree(struct block *addr, struct heap *hPtr, int size);
    void FreeBlock(struct block *bPtr, struct heap *hPtr);
    
    
    int main()
    {
    
    
    	return 0;
    }
    
    struct block *GetBlock(int size, struct heap *hPtr) {
        struct block *ret;
        if (ret = hPtr->free[size]) {
            hPtr->free[size] = hPtr->free[size]->next;
        } else if (size < hPtr->max) {
            if(ret = GetFlock(size + 1, hPtr)) {
                struct block *bPtr = (struct block *)((char *)ret + (1 << size));
                bPtr->next = hPtr->free[size];
                hPtr->free[size] = bPtr;
            }
        }
        return ret;
    }
    
    struct block *GetFree(struct block *addr, struct heap *hPtr, int size) {
        struct block *prev = &(hPtr->free[size - 1]);  // HACK!
        struct block *current = hPtr->free[size];
        while (current) {
            if (current == addr) {
                 break;
            }
            prev = current;
            current = current->next;
        }
        if (current) {
            prev->next = current->next;
        }
        return current;
    }
    
    void FreeBlock(struct block *bPtr, struct heap *hPtr) {
        int size = bPtr->size;
        while (size < hPtr->max) {
            unsigned long mask = 1 << size;
            unsigned long bit = bPtr & mask;
            struct block *base = (struct block *)((unsigned long)bPtr & ~mask);
            struct block *buddy = (struct block *)((unsigned long)base | (~bit & mask));
            if (GetFree(buddy, hPtr, size)) {
                size = base->size++;
                bPtr = base;
            }
        }
        bPtr->next = hPtr->free[size];
        hPtr->free[size] = bPtr;
    }
    just testing to see if this algorithim works under unix using cc compiler. get the following error when i try to run: test.c:60: error: invalid operands to binary & . It is the following line: unsigned long bit = bPtr & mask; Also, the unsigned long decleration used to be DWORD, which does not work under unix.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by v3dant
    Code:
    void FreeBlock(struct block *bPtr, struct heap *hPtr) {
        int size = bPtr->size;
        while (size < hPtr->max) {
            unsigned long mask = 1 << size;
            unsigned long bit = bPtr & mask;
            struct block *base = (struct block *)((unsigned long)bPtr & ~mask);
            struct block *buddy = (struct block *)((unsigned long)base | (~bit & mask));
            if (GetFree(buddy, hPtr, size)) {
                size = base->size++;
                bPtr = base;
            }
        }
        bPtr->next = hPtr->free[size];
        hPtr->free[size] = bPtr;
    }
    just testing to see if this algorithim works under unix using cc compiler. get the following error when i try to run: test.c:60: error: invalid operands to binary & . It is the following line: unsigned long bit = bPtr & mask; Also, the unsigned long decleration used to be DWORD, which does not work under unix.
    Like the error message says, your operands are not the correct type. For the binary & operator, each of the operands must have integer type. Perhaps you meant to convert the pointer to a long integer using a cast before doing the bitwise AND?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is my algorithim incorrect?
    By leeor_net in forum C Programming
    Replies: 2
    Last Post: 04-04-2004, 04:14 PM
  2. Bresenhams Algorithim
    By curlious in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-10-2003, 02:25 AM
  3. Pathfinding AI? (Not the A* Algorithim)
    By harryP in forum C++ Programming
    Replies: 22
    Last Post: 08-01-2003, 02:32 PM