|
From: Sebastien M. <seb...@te...> - 2002-12-11 06:55:12
|
Hi Zach,
Not at all, you can try to put the word inline in this case. If your
function isn't too big, and isn't recursive (and have not weird
properties, like calling a funtion calling itself) this function will
be certainly inline everywhere you will use it so it will be faster
since the code of the function will be direclty put instead of the
call of the function. It it can't inline it and you put even so the
word inline, the worst you can get is a warning. Note that if you
compile with -O3 (gcc option) every function will be try to put as an
inline function.
The way I suggest was just to have the declaration in the same time
than the definition to be sure that the compiler use your function. If
you put your function in another module and link the module together,
the compiler will use the stdlib function :(
For instance:
/************
* test1.cpp *
*************/
#include <stdlib.h> /* This header has to be set BEFORE teset.h,
* otherwise you get a conflict in the definition,
* the compiler will say that the atoi function is
* already defined. */
#include "test.h"
int main()
{
atoi();
return 0;
}
/*********
* test.h *
**********/
int atoi(const char *)
{
printf("It's my atoi :)\n");
return 0;
}
if you compile gcc test1.cpp and run the a.out you get:
It's my atoi :)
as output.
But if you name the test.h in test2.cpp, remove the #include "test.h"
line of the test1.cpp file and compile gcc test1.cpp test2.cpp and run
the a.out, you get no output because the atoi function used is not
yours!
Now if you change
int atoi(const char *)
by
inline int atoi(const char *)
you function will run faster, but the behaviour will be the same,
anyway, it's always a good thing to make the inline word. By the way,
don't forget to put -O1, -O2 or -O3, because by default no function
will be inlined, even if you use the inline word.
I hope it's more clear, maybe you knew most of things I said :)
Sebastien.
On Tue, Dec 10, 2002 at 11:12:03PM -0500, Zachary Uram wrote:
> Hi Sebastien,
>
> Ah is this what an "inline function declaration" is? How can I do this in
> the header file?
>
> Zach
>
> At 11:04 PM 12/10/02, Sebastien Morand wrote:
> >Hi Zach,
> >If you use the include stdlib and want to use your own atoi
> >definition, you have to definite it (not only the declaration) in a
> >specific header your will include in all file where you use this function.
> >You must be sure that your file with the definition of atoi is include
> >AFTER stdlib.h if you use it in the same file. In all tests I made it
> >works.
> >
> >Have fun in coding,
> >Sebastien.
>
> Zachary Uram
> John 3:16
> <><
>
--
Time remaining: 28 weeks 6 days 0 hours 0 minutes
|