2

I want to copy the content of some char-arrays passed as parameters in a function to another char-arrays. So I passed these arrays as pointers (passing by reference). Then I used memcpy to copy the content of these arrays to some other arrays. But the copying-process was not too exact, although I think that I used memcpy correctly. Some characters was deleted, while some new charcters appeared. I tried then to use strcpy, so the content of these arrays was correctly copied. So I want to understand why copying process failed when using memcpy. Here is the some of my code:

struct student{
    bool statusFlag;
    char lastname[20];
    char firstname[20];
    int  mNr;
 };

here is the method:

 struct student getData(char * lastname, char * firstname, int matNr){
     struct student st;
    int i;
    printf("%s\n",lastname);
    if(insertCounter<=size){
        //get data
        st.statusFlag=1;
        memcpy(st.lastname,lastname,strlen(lastname));
        memcpy(st.firstname,firstname,strlen(firstname));
        st.mNr=matNr;
        printf("%s,%s,%d\n",st.lastname,st.firstname,st.mNr);
        return st;
    }else if(insertCounter>size){
       st.statusFlag=0;
        return st;
}    

When I replaced memcpy with strcpy, The copy-operation was successful:

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
amitakCs
  • 355
  • 9
  • 25
  • 5
    You're not copying the terminating null character. `strlen(…) + 1` should do the trick, although `strcpy` should be used to copy strings as it checks for the null character automatically while copying. – Emil Laine Jun 07 '15 at 21:27
  • 1
    Which language is this? It could be at least either C or C++, but in both of those languages, your code has at least one severe problem other than what you asked about, but the usual way to solve that problem depends on the language. –  Jun 07 '15 at 21:28
  • it's pure c-language – amitakCs Jun 07 '15 at 21:37
  • yes, it's working now.thank you. – amitakCs Jun 07 '15 at 21:41
  • if you are seeing some mistakes. you can tell me about – amitakCs Jun 07 '15 at 21:43
  • The severe problem being referred to is the potential buffer overflows in your `memcpy` lines.If `lastname` and/or `firstname` are too long then the corresponding `st.lastname` and/or `st.firstname` buffers will overflow. – kaylum Jun 07 '15 at 22:00
  • I think since you said "new characters appeared and some are deleted " then this is a matter of null terminator with memcpy , see this :http://stackoverflow.com/questions/2898364/strcpy-vs-memcpy – Jay Shenawy Jun 07 '15 at 22:52

1 Answers1

0

The statement

memcpy(target,source, strlen(source))

should copy all the chars of the string. But, it will stop just short of copying the 0-byte that marks the end of the string. So what you copied won't be a string. This will be a problem if you call any string functions on the new copy (target), basicaly if you use target in any way, you will march off the end, since the end is now unmarked. Probably you will pick up some extra bytes, anything that happens to be in memory after target, worst case you program segfalts if it marches far enough without finding a 0. The function strcpy will copy the 0-byte, I usually use

snprintf(target, sizeof target,  "%s", source); 

Since it does not write past the end of the target buffer, and it always makes room for the 0, protecting against trouble in the next string op.

  • `snprintf` is perceived to be safer (and it is in many ways). But it is prone to producing non-NULL terminated strings (which is itself a security issue). Thus the return value of `snprintf` MUST be checked and error recovery or manual buffer null termination be performed. – kaylum Jun 08 '15 at 21:00
  • @alan-au My experience, and `snprintf`s man page suggest that snprintf is not prone to producing non-NULL terminated strings. From the (OSX) man page: The snprintf() and vsnprintf() functions will write at most n-1 of the characters printed into the output string (the n'th character then gets the terminating `\0'); if the return value is greater than or equal to the n argument, the string was too short and some of the printed characters were discarded. **The output is always null-terminated.** – Christopher Ian Stern Jun 09 '15 at 00:47