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.
You can’t use a function prior to its proper definition without a prototype.
buddy_malloc()is usingbuddy_findout()before it is prototyped or defined, which the compiler will treat as a definition.Prototype
buddy_findout()prior to definingbuddy_Malloc(), or definebuddy_findout()prior to definingbuddy_Malloc().I suggest prototypes, i.e:
… Just under your last
#includeThis 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:
And method 2, just re-ordering:
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:The preprocessor will then keep your modules from throwing that error.