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 8932175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:16:57+00:00 2026-06-15T09:16:57+00:00

In C, you can use strdup to succinctly allocate a buffer and copy a

  • 0

In C, you can use strdup to succinctly allocate a buffer and copy a string into it. As far as I’m aware, however, there is no similar function for general memory. For example, I can’t say

struct myStruct *foo = malloc(sizeof(struct myStruct));
fill_myStruct(foo);

struct myStruct *bar = memdup(foo, sizeof(struct myStruct));
// bar is now a reference to a new, appropriately sized block of memory,
//   the contents of which are the same as the contents of foo

My question, then, is threefold:

  1. Is there some standard library function like this that I don’t know about?
  2. If not, is there a succinct and preferably standard way to do this without explicit calls to malloc and memcpy?
  3. Why does C include strdup but not memdup?
  • 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. Editorial Team
    Editorial Team
    2026-06-15T09:16:58+00:00Added an answer on June 15, 2026 at 9:16 am

    Making a copy of an arbitrary memory structure isn’t as straight forward as copying a string. How should you handle the case where the structure contains pointers to other structures (such as strings) for example? What does it mean to “duplicate” such a structure? There isn’t one right answer to this, unlike the case for string. In that case, it’s probably better to just let the application developer create a mechanism for making a copy of the structure according to their use cases rather than confuse the issue by pretending that there is a canonical way to handle it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can use the key argument for the min function to compare a list
I'm trying to use the strdup() function in C but I'm getting an odd
I can use @font-face but it does not work in all browsers. Are there
Can use select into with multiple cte? for example in the below code the
HBase can use HDFS as back-end distributed file system. However, their default block size
I can use the following to create a function dynamically in javascript: var MyClass
You can use ftplib for full FTP support in Python. However the preferred way
You can use SelectFolder() to get a folder or GetOpenFolderitem(filter as string) to get
I recently became aware that the strdup() function I've enjoyed using so much on
I can use -d option to put files into a database. mongofiles -d test

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.