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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:58:06+00:00 2026-05-25T05:58:06+00:00

Whenever i have a function that takes a c-string, and want to store its

  • 0

Whenever i have a function that takes a c-string, and want to store its value inside a linked list, should i do it like this…

void add(char* str)
{
    node *n = malloc(sizeof(node));
    n->value = str;
}

or rather…

void add(char* str)
{
    node *n = malloc(sizeof(node));
    char* copy = malloc(strlen(str)+1);
    strcpy(copy, str);
    n->value = copy;
}

Thanks in advance.

  • 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-05-25T05:58:07+00:00Added an answer on May 25, 2026 at 5:58 am

    it really depends on where are the arguments coming from, and what are your intentions with those arguments.

    if you know that the strings passed as argument are always available through the entire lifetime of your linked list, and that they are not modified nor freed, or that there is no function which would have any side-effect affecting your linked-list, then you can simply copy the pointer and not bother to copy the entire string.

    if any of the above is not true (by which i mean also if you don’t know the answer to one of the above), then it will be safer to copy the entire string.

    some specific examples:

    • you are developing a small application which reads a csv file, stores the values in a linked list for sorting, then writes the values back to an xml file: you have the control over the entire lifetime of your strings, you don’t have to copy them.

    • you are writing a linked-list library possibly, distributed on the net, possibly used by hundreds of people active in all kind of field: you don’t know what will be passed to your library, you don’t know if the library user will free the string before manipulating the linked-list, then you copy the entire string.

    also note that this kind of design decision is better documented somewhere: it is your responsibility as a developer to make clear that your function will store the pointer without copying the string, or that you will copy the string and will need another function call to release the memory. (this is called design by contract: you establish a contract between the code using your function and the function itself, you’d better respect it or you are going to have problems, in the form of data corruption or software crash). one possible way to make your intention clear is the use of an appropriately placed const keyword.

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

Sidebar

Related Questions

I have a function that takes another function as a parameter. Something like this
I have a function that whenever it is called it splits the window and
If I have a function that takes int *& , what does it means?
Whenever I have a library that use across different websites/ applications I've always just
I have this very annoying issue, whenever i call a function: void renderGame::renderMovingBlock(movingBlock* blockToRender){
So, on a web-page there are some components that have to be re-positioned whenever
I have 30 buttons with nothing in default text value. What i want to
I have an Arduino sketch that takes a timet and when that timet is
I have an Arduino sketch that takes a timet and when that timet is
I have an mvc3 web page that takes a url parameter representing a date,

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.