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.