tags:

views:

104

answers:

3

I am doing a recursive program and I am getting an error about conflicting types:

void* buddyMalloc(int req_size)
{ 
     // Do something here
     return buddy_findout(original_index,req_size); // This is the recursive call
}

void *buddy_findout(int current_index,int req_size)
{
    char *selected = NULL;

    if(front!=NULL)
    {
        if(current_index==original_index)
        {
            // Do something here
            return selected;
        }
        else
        {
            // Do Something here
            return buddy_findout(current_index+1,req_size);
        }
    }
    else
    {
        return buddy_findout(current_index-1,req_size);
    }
}

Error:

buddy.c: At top level:
buddy.c:76: error: conflicting types for ‘buddy_findout’
buddy.c:72: note: previous implicit declaration of ‘buddy_findout’ was here

Please note the file buddy.c in which I am defining this does not contain main and is linked with several other .c files.

A: 

I guess this has to be with missing prototypes.

Add the function prototype of

void *buddy_findout(int current_index,int req_size);

before the function buddyMalloc

codaddict
Thanks I got it fixed..
Adi
+2  A: 

You can't use a function prior to its proper definition without a prototype. buddy_malloc() is using buddy_findout() before it is prototyped or defined, which the compiler will treat as a definition.

Prototype buddy_findout() prior to defining buddy_Malloc(), or define buddy_findout() prior to defining buddy_Malloc().

I suggest prototypes, i.e:

void *buddy_Malloc(int);
void *buddy_findout(int, int);

... Just under your last #include

This avoids any confusion on the order that you define things. Also, consider using size_t (the largest unsigned int type available on the architecture) instead of signed integers when specifying sizes.

Here is your code (corrected) using both methods. Method 1 - using prototypes:

void *buddy_Malloc(int);
void *buddy_findout(int, int);

void* buddyMalloc(int req_size)
{ 
          //Do something here//
         return buddy_findout(original_index,req_size); //This is the recursive fn I call//
}

void *buddy_findout(int current_index,int req_size)
{
     char *selected = NULL;

     if(front!=NULL)
      {
            if(current_index==original_index)
            {
                    //Do something here//
                     return selected ; // 
             }
             else
             {
                 //Do Something here//
                 return buddy_findout(current_index+1,req_size);
              }
        }
       else
        {
                  return buddy_findout(current_index-1,req_size);
         }
}

And method 2, just re-ordering:

void *buddy_findout(int current_index,int req_size)
{
     char *selected = NULL;

     if(front!=NULL)
      {
            if(current_index==original_index)
            {
                    //Do something here//
                     return selected ; // 
             }
             else
             {
                 //Do Something here//
                 return buddy_findout(current_index+1,req_size);
              }
        }
       else
        {
                  return buddy_findout(current_index-1,req_size);
         }
}

void* buddyMalloc(int req_size)
{ 
          //Do something here//
         return buddy_findout(original_index,req_size); //This is the recursive fn I call//
}

In some circles it is sort of considered an art to not need prototypes for static functions, it kind of demonstrates the program was planned out in someone's head before code was written. I don't have much to say about that either way, other than recommending prototypes even for static functions to those who are still learning the nuts and bolts of C.

If buddy_* is going to be exposed for other modules to use, you really need prototypes. Its hard to tell if you intend these to be static or not.

Edit:

If you are putting the prototypes in an external header file, you need to use include guards to ensure that each module includes them only once (and doesn't re-define them to be the exact same thing).

Here is a sample buddy.h:

#ifndef BUDDY_H
#define BUDDY_H

void *buddy_Malloc(int);
void *buddy_findout(int, int);

#endif /* BUDDY_H */

The preprocessor will then keep your modules from throwing that error.

Tim Post
AS per your suggestion, I prototyped it in a file called buddy.hBut I am still getting errors like:buddy.c: At top level:buddy.c:76: error: conflicting types for ‘buddy_findout’buddy.c:72: note: previous implicit declaration of ‘buddy_findout’ was herePlease note that, the function buddy.c in which I am defining this does not contain main and is linked with several other .c files
Adi
Adi, you've got a circular reference somewhere. Did you put include guards in the header? I'll edit my answer.
Tim Post
@Adi - updated my answer
Tim Post
Thanks I got it fixed..
Adi
@Adi, great, don't forget to select this answer if it fixed your problem.
Tim Post
@Tim, Thanks for the detailed explanation. Well I come in the category of learning nuts and bolts of C :)--How do I select an answer ? Being a newbie, i dont knwo it..I am now getting a stack overflow problem in the above recursive code.Here is the message i got*** glibc detected *** ./473_mem: free(): invalid pointer: 0x00c274c0 ***473_mem was the executable file
Adi
@Adi - You probably want to open another question for that, since its not related to this one.
Tim Post
I did open another question for that..I would welcome your feedback for that..
Adi
A: 

buddy_findout is declared to return a pointer to void, but in one place you're attempting to return selected, which is a pointer to char. As others have already pointed out, you also need a prototype for buddy_findout:

void *buddy_findout(int, int);

void *buddy_malloc(int req_size) { 
    return buddy_findout(original_index,req_size);
}

void *buddy_findout(int current_index, int req_size) {
// ...
    if (current_index == original_index)
        return (void *)selected;
// ...
}
Jerry Coffin
Because char pointers are implicitly converted to void pointers, that is not a problem. Casting to a void pointer is when returning is just superfluous.
Roger Pate