C strdup() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
string.h

Definition

In C, strdup() allocates a new copy of a C-string on the heap. You own the result, so you must call free() later. It is common on many systems and is not a classic ISO C function.

You pass one C-string. It is required. The result is a new pointer, or NULL when allocation fails. Include <string.h> where strdup is provided.

Syntax

Pass the string to copy. Start with this call.

C
char *copy = strdup("Hello");
CallWhat it does
strdup("Hello")Allocates a new heap string that holds Hello.

Always free the result. On some systems you may need a feature-test macro or a portable malloc + strcpy fallback.

Library declarations
C
char *strdup(const char *s);

Parameters

ParameterDescription
sRequired. The C-string to copy.

Return Value

Return ValueDescription
New pointerA heap copy of the string. You must free it.
NULLReturned when allocation fails.

C Examples

Example 1 — Basic Duplicate and Free

Create a heap copy, print it, then release the memory.

C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    char *copy = strdup("Hello");

    if (copy == NULL) {
        printf("allocation failed\n");
        return 1;
    }

    printf("copy = %s\n", copy);
    free(copy);
    return 0;
}

How It Works

  1. Allocate and copy. strdup reserves space for “Hello” plus '\0'.
  2. Check NULL. If allocation fails, exit early instead of crashing.
  3. Use the pointer. printf prints the new string on the heap.
  4. Free when done. free(copy) returns the memory to the allocator.

Example 2 — Edit the Copy, Keep the Original

Changes to the duplicate do not change the source string.

C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    const char *src = "Cat";
    char *copy = strdup(src);

    if (copy == NULL) {
        return 1;
    }

    copy[0] = 'B';
    printf("src  = %s\n", src);
    printf("copy = %s\n", copy);

    free(copy);
    return 0;
}

How It Works

  1. Independent memory. copy points to a different buffer than src.
  2. Change one byte. Writing 'B' only updates the heap copy.
  3. Original stays safe. src still prints Cat.
  4. Still free the copy. Ownership of the allocated block remains yours.

Example 3 — Portable Manual Duplicate

Same idea without strdup: size with strlen, allocate, then strcpy.

C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    const char *src = "Fun";
    char *copy = malloc(strlen(src) + 1);

    if (copy == NULL) {
        return 1;
    }

    strcpy(copy, src);
    printf("copy = %s\n", copy);
    free(copy);
    return 0;
}

How It Works

  1. Compute size. strlen(src) + 1 includes the terminator.
  2. Allocate. malloc returns a writable heap buffer (or NULL).
  3. Copy the text. strcpy fills the new buffer from src.
  4. ISO-portable. This pattern works even where strdup is missing.

Frequently Asked Questions

strdup() allocates a new heap copy of a C-string. You must free the result later.
s is required. It is the C-string to copy.
A new pointer to the copy, or NULL when allocation fails.
Many systems declare it in <string.h>, but it is not a classic ISO C function.
Yes. Call free(copy) when you are done.

Next: strlwr()

Convert characters in a string to lowercase (non-standard helper).

strlwr() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful