Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Consider the following C function which takes as argument a string, which is then stored inside a struct:

struct mystruct* usestring(char* string)
    struct mystruct *struct;
    struct = malloc(sizeof(struct mystruct));
    struct->string = string;
    return struct;

My understanding is that the string passed to the function is the same string that is stored inside the struct.

What is the proper etiquette in this situation? Should I make a copy of the string and store that in the struct, or should I expect that the function caller will not modify the string later?

share|improve this question

closed as too broad by Dan Pichelman, Wayne M, Giorgio, GlenH7, MichaelT Aug 8 '14 at 0:59

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

This is way too broad to answer. It's often to be expected, but also often unexpected. Ownership of dynamically allocated memory is a huge topic; in fact, people have written entire new languages with garbage collection mainly to escape the problems brought about by correctly tracking such ownership. – Kilian Foth Aug 7 '14 at 13:56
I would think the objectively correct thing to do is document your choice. You have no clue who might use this function in the future and trying to predict what they'll expect is an exercise in futility. If you find by using this function you want some other ownership behavior, update the function and perhaps add some rational for the new behavior in the documentation – jozefg Aug 7 '14 at 13:58
You should assume anything could happen unless the documentation says otherwise. – Doval Aug 7 '14 at 14:08
Thanks for the clarifications. I felt like I might be doing something wrong by having it possible to damage the contents of the struct. – user2590835 Aug 7 '14 at 17:12
up vote 10 down vote accepted

This will always depend on many factors, but as elementary guidance:

  1. For pointers to const, ownership should not be transferred and the callee should not assume the pointed object will continue to exist after it returns.

  2. For pointers to non-const, ownership transfer must be documented. Whether ownership is transferred or not is a matter of what is more practical in the particular place.

  3. Pointers should be to const whenever possible.

This is not always the most efficient thing to do (you sometimes copy when the object actually will continue to exist), but it is a way to reduce the number of places where you have to be careful.

share|improve this answer
Thanks for the clarifications – user2590835 Aug 7 '14 at 17:19

As others have said, it depends on context.

Consider the following code, which creates new instances of struct mystruct and adds them to a list:

while ( some_condition )
  char inputString[SIZE];
  if ( getInput( inputString ) )
    struct mystruct *newStruct = usestring( inputString );
    addEntry( list, newStruct );

Every instance of newStruct in the list will wind up pointing to the same string; even better, that pointer will be invalid after the while loop exits.

In this particular case, you should make a copy of the input string and store that to the struct. Of course, that opens up questions about how that memory is to be recovered, who's responsible for it, etc.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.