c - strdup() function -


I recently found out that strdup () is the function I use so much on the OS Enjoyed doing X is not part of ANSI C, but is part of POSIX. I do not want to rewrite all my code, so I think I'm going to write my own strdup () function. It's not that hard, in fact, it's just a malloc () and strcpy () . Anyway, I have a function, but what am I doing if I write this function and link it to my code, and it is already present in libc? Will my linker or compiler allow me to define my own version of the function, or will I have to give it another name? It would be very convenient if there was a way to reuse the same name, so that if strcpy () is present in the user's libc, then he can use it, but if it is their libc If they are not present, then they are

a) What happens when I type my own function with the same name as the built-in function?

b) What can I do to avoid the bad things happening to me on platforms strdup () to write all my code without having to repeat it Strdup () , which is just a bit tedious?

Usually, you use just under a certain code when you want the function to be To define a #if if the underlying library does not define strdup, there is no problem defining yourself (if they define it in the future, you should take it out ).

  // Only define the stdup for those platforms which Are missing .. #if COMPILER_XYZ || COMPILER_ABC four * strdup (const char *) {// ....} #endif  

Comments