Best practice for strcpy () or pointing to a different data structure?

Because it is always easier to see the code ...

My parser populates this object:

typedef struct pair { char* elementName; char* elementValue; } pair; 

My interpreter wants to read this object and populate it:

 typedef struct thing { char* label; } thing; 

Should I do this:

 thing.label = pair.elementName; 

or that:

 thing.label = (char*)malloc(strlen(pair.elementName)+1); strcpy(thing.label, pair.elementName); 

EDIT: Yes, probably I should have indicated what the rest of the program will do with objects. Ultimately, I will need to save the “pair” in the file. Therefore, when thing.label is changed, then (at some point) the name of the .elementName pair must be changed to match. So, I think the first is the best way to do this?

+4
source share
7 answers

I would personally do the first, but this is a compromise. The former avoids the need to allocate new memory and copy data to it, but the latter avoids the confusion of smoothing by storing thing.label and pair.elementName , pointing to separate memory addresses, which means that you need to free them both (using first of all, you should be sure to release exactly one to avoid memory leak or double free)

+1
source

There is no good answer to this question, because there is too little context. It all depends on how the rest of the program controls the lifetime of the created objects.

+3
source

Here are some of the things that must be known in order to answer a question:

  • Which object will “contain” the string? Or will both own their own line (in this case, a "deep" copy is needed)?
  • - these are the lifetimes of pair and thing objects connected in some way - will one object always “survive” the other? Does one of these objects have another?

If the pair and thing objects are independent, copying the string data is probably correct. If someone belongs to another, then this may indicate that a simple pointer exchange is appropriate.

Not that these were the only possible answers - just a few simple ones.

+1
source

From the point of view of "object" independence, it is probably better to make a copy of the data to avoid problems with dangling pointers.

It would be more efficient and faster to assign a pointer, but if this extra performance is not very important, you will probably be better off (from the debug point) to make a copy.

0
source

The answer, as always, is "Depends." If everything you do with the "copied" value reads it, probably just copying the address of the pointer (i.e., First) is enough if you clear it correctly. If the “copied” value is changed in any way, you will want to create a whole new line (i.e., the Last) to avoid any unintended side effects caused by the change in the “original” value (unless of course it is definitely desired the effect).

0
source

If you want to make a copy and do all the cleanup afterwards ... in C, you have to do this:

 thing.label = strdup(pair.elementName); 
0
source

I don't want to be a c-police, but use strncpy() safer strcpy() .

 char* strncpy(char *s1, const char *s2, size_t n); 

The strncpy function copies no more than n characters from s2 to s1.

-1
source

Source: https://habr.com/ru/post/1313913/


All Articles