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

  • Home
  • SEARCH
  • 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 63559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:39:06+00:00 2026-05-10T18:39:06+00:00

What is the purpose of the strdup() function in C?

  • 0

What is the purpose of the strdup() function in C?

  • 1 1 Answer
  • 0 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-10T18:39:06+00:00Added an answer on May 10, 2026 at 6:39 pm

    Exactly what it sounds like, assuming you’re used to the abbreviated way in which C and UNIX assigns words, it duplicates strings 🙂

    Keeping in mind it’s actually not part of the current (C17) ISO C standard itself(a) (it’s a POSIX thing), it’s effectively doing the same as the following code:

    char *strdup(const char *src) {     char *dst = malloc(strlen (src) + 1);  // Space for length plus nul     if (dst == NULL) return NULL;          // No memory     strcpy(dst, src);                      // Copy the characters     return dst;                            // Return the new string } 

    In other words:

    1. It tries to allocate enough memory to hold the old string (plus a ‘\0’ character to mark the end of the string).

    2. If the allocation failed, it sets errno to ENOMEM and returns NULL immediately. Setting of errno to ENOMEM is something malloc does in POSIX so we don’t need to explicitly do it in our strdup. If you’re not POSIX compliant, ISO C doesn’t actually mandate the existence of ENOMEM so I haven’t included that here(b).

    3. Otherwise the allocation worked so we copy the old string to the new string(c) and return the new address (which the caller is responsible for freeing at some point).

    Keep in mind that’s the conceptual definition. Any library writer worth their salary may have provided heavily optimised code targeting the particular processor being used.

    One other thing to keep in mind, it looks like this is currently slated to be in the C2x iteration of the standard, along with strndup, as per draft N2912 of the document.


    (a) However, functions starting with str and a lower case letter are reserved by the standard for future directions. From C11 7.1.3 Reserved identifiers:

    Each header declares or defines all identifiers listed in its associated sub-clause, and optionally declares or defines identifiers listed in its associated future library directions sub-clause.*

    The future directions for string.h can be found in C11 7.31.13 String handling <string.h>:

    Function names that begin with str, mem, or wcs and a lowercase letter may be added to the declarations in the <string.h> header.

    So you should probably call it something else if you want to be safe.


    (b) The change would basically be replacing if (d == NULL) return NULL; with:

    if (d == NULL) {     errno = ENOMEM;     return NULL; } 

    (c) Note that I use strcpy for that since that clearly shows the intent. In some implementations, it may be faster (since you already know the length) to use memcpy, as they may allow for transferring the data in larger chunks, or in parallel. Or it may not 🙂 Optimisation mantra #1: "measure, don’t guess".

    In any case, should you decide to go that route, you would do something like:

    char *strdup(const char *src) {     size_t len = strlen(src) + 1;       // String plus '\0'     char *dst = malloc(len);            // Allocate space     if (dst == NULL) return NULL;       // No memory     memcpy (dst, src, len);             // Copy the block     return dst;                         // Return the new string } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 65k
  • Answers 65k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer EF, at least in the current version, doesn't support this.… May 11, 2026 at 11:07 am
  • added an answer The best answer I can come up with is to… May 11, 2026 at 11:07 am
  • added an answer One difference I see in reviewing the two is that… May 11, 2026 at 11:06 am

Related Questions

What is the purpose of the strdup() function in C?
What is the purpose of the Using block in C#? How is it different
What is the purpose of the colon before a block in Python? Example: if
What is the purpose of the LongLength property for arrays in .Net. Using a
What is the purpose of the RAILS_GEM_VERSION setting in config/environment.rb ? Is it supposed
What is the purpose of the code behind view file in ASP.NET MVC besides
In C++, what is the purpose of the scope resolution operator when used without
What is the purpose/meaning of the Version property on a FormsAuthenticationTicket?
What exactly is the purpose of the 'obj' directory in .NET?
What is the purpose of annotations in Java? I have this fuzzy idea of

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.