Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 110607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:15:13+00:00 2026-05-11T02:15:13+00:00

I recently became aware that the strdup() function I’ve enjoyed using so much on

  • 0

I recently became aware that the strdup() function I’ve enjoyed using so much on OS X is not part of ANSI C, but part of POSIX. I don’t want to rewrite all my code, so I think I’m just going to write my own strdup() function. It’s not that hard, really, it’s just a malloc() and a strcpy().

Anyway, I have the function, but what am I doing if I write this function and link it to my code, and it already exists in the libc? Will my linker or compiler allow me to basically define my own version of the function, or do I have to give it another name? It would be terribly convenient if there was a way to reuse the same name, so that if strcpy() exists in the user’s libc they could use that, but if it didn’t exist in their libc they could use my version instead, with as little code change as possible.

The short version:

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

  • b) What can I do to avoid bad things happening to me on platforms that don’t have strdup() without rewriting all my code to not use strdup(), which is just a bit tedious?

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T02:15:13+00:00Added an answer on May 11, 2026 at 2:15 am

    Usually, you just use an #if to define the function you want when under a certain compiler. If the built-in library doesn’t define strdup, there is no problem in defining it yourself (other than if they do define it in the future, you’ll have to take it out.)

    // Only define strdup for platforms that are missing it.. #if COMPILER_XYZ || COMPILER_ABC char *strdup(const char *) {    // .... } #endif 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've recently started using the DynamicQuery API , and it quickly became apparent that
I recently became part of a complex embedded project team for which I will
I run my blog using Wordpress and all too recently became a big believer
In our company, just until recently, we were not using namespaces because some compilers
I recently finished a class that we're using to tie Access to some WCF
I recently became aware of the System.Web.UI.WebControls.Localize control in a lab for the ASP.NET
I thought that I understood how MacOSX manages memory, but recently I’ve become baffled
I have recently become aware that our code has several implementations of a version
I recently became a part of the team of developers writing our flagship product.
I recently became lead on getting an inordinate amount of testing done in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.