Like Tree3Likes
  • 3 Post By hk_mp5kpdw

Cannot find ( before ; error

This is a discussion on Cannot find ( before ; error within the C Programming forums, part of the General Programming Boards category; I'm sure it's just a simple syntax error. I understand this code has its share of errors, however right now ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    9

    Cannot find ( before ; error

    I'm sure it's just a simple syntax error. I understand this code has its share of errors, however right now I'm just concerned with the error at the malloc call if structure. I would like to solve the rest of the problems myself. I just cannot seem to find where I have gone wrong with with this particular one. Error should be in line 8.

    Code:
    void *myMalloc(unsigned int num_bytes)
    {
        header *p;                        //Pointers to current and previous headers.
        header temp;
    
        if(base_ptr == NULL)            //If myMalloc has not been called in program yet.
        {   /***********ERROR LINE BELOW**************/
            if((base_ptr = (header*)malloc(MEM_SIZE+(MEM_SIZE%sizeof(header))+sizeof(header))) == NULL) //MEM_SIZE header units.
            {
                printf("Malloc fail\n");//Malloc failed to allocated requested memory.
                return NULL;
            }
            base_ptr[0].size = MEM_SIZE;        //Sets the initial block equal to the total size set by MEM_SIZE.
            base_ptr[0].status = FREE;            //Declares the initial block as free memory.
            base_ptr[0].next = NULL;            //No other block so the first block's ptr field is NULL.
            base_ptr[0].prev = NULL;
        }
        
        p = base_ptr;                    //Set's the current header pointer to the first header.
        
        while(p != NULL)                //While the point has not reach the end of the list.
        {
            if((p->status == FREE) && (p->size >= num_bytes))        //If the current field is free.
            {
                temp.size = p->size;
                temp.prev = p->prev;
                
                p->size = num_bytes;    //Size of payload.
                p->status = USED;        //Memory block is now allocated.
                /*If the requested size is exactly equal to the free block, pointers stays the same.*/
                
                if(temp.size > num_bytes)
                {
                    p->next = (char*)p + (numbytes*sizeof(header));    //Cast to char pointer to handle pointer arithmetic.
                    p->next->size = temp.size - num_bytes;            //Cause errors at end of memory?
                    p->next->status = FREE;
                    p->next->prev = p;
                    p->next->next = temp.next;
                }
                
                return (char*)p + sizeof(header);
            }
    
            p = p->next;
        }
        
        printf("myMalloc cannot allocate the requested amount of memory\n");
        return NULL;
    }
    Thank you!

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    That particular line looks fine to me. Remember that the first error you look at should always be the very very first error your compiler gives you. Please make sure this is the first error, and cut & paste it so we can take a look.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    9
    EDIT: I have fixed most of the errors in my code. Here is the list of errors:

    myMalloc.c: In function ‚myMalloc‚:
    myMalloc.c:45: error: expected ‚)‚ before ‚;‚ token
    myMalloc.c:60: error: expected ‚)‚ before ‚;‚ token
    myMalloc.c:71: warning: assignment from incompatible pointer type
    myMalloc.c:78: warning: initialization from incompatible pointer type
    myMalloc.c: In function ‚free‚:
    myMalloc.c:94: warning: dereferencing ‚void *‚ pointer
    myMalloc.c:94: error: invalid use of void expression
    myMalloc.c:99: error: expected ‚)‚ before ‚;‚ token
    myMalloc.c:109: error: expected ‚)‚ before ‚;‚ token

    Here is the entire code:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    #define USED 1;
    #define FREE 0;
    #define MEM_SIZE 1024;
    
    typedef struct header header;
    
    struct header {
        unsigned int size :31;            //Number of units of block.
        unsigned int status :1;            //Whether block is free or used.
        header    *prev,                    //Pointer to the next header.
                *next;                    //Pointer to the previous head.
    };
    
    void *myMalloc(unsigned int);
    void free(void*);
    
    header *base_ptr;                    //Pointer to the base header.
    
    int main(int argc, char* argv[])    //Driver.
    {
        char *p1, *p2;
        
        p1 = myMalloc(45);
        p2 = myMalloc(32);
        
        printf("%p\n", p1);
        
        return 0;
    }
    
    void *myMalloc(unsigned int num_bytes)
    {
        header *p;                        //Pointers to current and previous headers.
        header temp;
    
        if(base_ptr == NULL)            //If myMalloc has not been called in program yet.
        {   /***********ERROR LINE BELOW**************/
            if((base_ptr = (header*)malloc(MEM_SIZE+(MEM_SIZE%sizeof(header))+sizeof(header))) == NULL) //MEM_SIZE header units.
            {
                printf("Malloc fail\n");//Malloc failed to allocated requested memory.
                return NULL;
            }
            base_ptr[0].size = MEM_SIZE;        //Sets the initial block equal to the total size set by MEM_SIZE.
            base_ptr[0].status = FREE;            //Declares the initial block as free memory.
            base_ptr[0].next = NULL;            //No other block so the first block's ptr field is NULL.
            base_ptr[0].prev = NULL;
        }
        
        p = base_ptr;                    //Set's the current header pointer to the first header.
        
        while(p != NULL)                //While the point has not reach the end of the list.
        {
            if((p->status == FREE) && (p->size >= num_bytes))        //If the current field is free.
            {
                temp.size = p->size;
                temp.prev = p->prev;
                
                p->size = num_bytes;    //Size of payload.
                p->status = USED;        //Memory block is now allocated.
                /*If the requested size is exactly equal to the free block, pointers stays the same.*/
                
                if(temp.size > num_bytes)
                {
                    p->next = (char*)p + (num_bytes*sizeof(header));    //Cast to char pointer to handle pointer arithmetic.
                    p->next->size = temp.size - num_bytes;            //Cause errors at end of memory?
                    p->next->status = FREE;
                    p->next->prev = p;
                    p->next->next = temp.next;
                }
                
                char *c = p;
                return c + sizeof(header);
            }
    
            p = p->next;
        }
        
        printf("myMalloc cannot allocate the requested amount of memory\n");
        return NULL;
    }
    
    void free(void *p)
    {
        char *c;                //Used to simplify pointer arithmetic.
        c -= sizeof(header);
        p = c;
        header h = (header)*p;
        h.status = FREE;
        
        if(h.prev != NULL)
        {
            if(h.prev->status == FREE)
            {
                h.prev->size += h.size + sizeof(header);
                h.prev->next = h.next;            
                p = h.prev;
            } 
        }
        
        if(h.next != NULL)
        {
            if((h.next->status) == FREE)
            {
                h.size += h.next->size + sizeof(header);
                h.next = h.next->next;
            }
        }
    }
    EDIT2: As you can see I also have very little experience dealing with void pointers.
    Last edited by seannabeast; 12-07-2011 at 08:51 AM. Reason: In addition:

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Here's a hint:
    Code:
    #define USED 1;
    #define FREE 0;
    #define MEM_SIZE 1024;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    9
    Silly me. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i cant find the error... Plz help!!
    By fredsilvester93 in forum C Programming
    Replies: 4
    Last Post: 11-29-2011, 03:13 PM
  2. Cant Find Error
    By Toonzaka in forum C Programming
    Replies: 3
    Last Post: 08-10-2008, 03:00 PM
  3. one error....can't find it
    By ammanbesaw in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2007, 10:36 AM
  4. Can anyone find why I am getting this error?
    By Drew in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2003, 06:05 PM
  5. I cant find the error
    By soka in forum C++ Programming
    Replies: 7
    Last Post: 07-14-2003, 09:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21