• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 555
  • Last Modified:

strcpy and memcpy... usages??

Hi guys,

   I have a question.. I have been looking at some in-house source codes and I noticed that to copy strings, sometimes strcpy is used and sometimes memcpy is used...
I can't differenciate when to use strcpy and memcpy..
can anybody explain WHEN and WHY in IT projects we would use 'strcpy' and/or 'memcpy' and does it not matter?


Thanks a lot...

David
0
Haho
Asked:
Haho
  • 4
  • 3
  • 3
  • +3
1 Solution
 
snoeglerCommented:
The only difference is that memcpy copies a given number of bytes, regardless of a zero
terminator in the block to be copied. 'strcpy' stops on each zero terminator.
Maybe 'memcpy' is used when strings like this are to be copied:
"string 1\0string 2\0string3\0\0" (double \0\0 at the end). Of course, the length of this strings
must be known before copying.
But the following lines are equivalent:

char *string1,*string2;
/* .... */
strcpy(string2,string1);
memcpy(string2,string1,strlen(string1));
0
 
MotazCommented:
Hello Haho,  Snoegler answer your question.

In addition to that there are another important deference : If the two string ( Dest and Source ) are overlapped ( have an intersection in memory location ) memcpy will preduce an unpredictable results so that strcpy is more safe and sutable in most cases.

Motaz
0
 
Answers2000Commented:
snoegler - I think you mean

memcpy(string2,string1,strlen(string1)+1);

If a zero occurs anywhere in the middle of a string then memcpy is likely to be used.

memcpy is also used to copy structures etc.


Motaz - memcpy is not guaranteed to work for overlapping strings either
(begin quote)
The memcpy function copies count bytes of src to dest. If the source and destination overlap, this function does not ensure that the original source bytes in the overlapping region are copied before being overwritten. Use memmove to handle overlapping regions.
(end quote)


0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
snoeglerCommented:
Answers2000, you're right. Of course i forgot '+1'.
BTW: Motaz mentioned this overlapping memory issue :)
0
 
MotazCommented:
What did overlapping mean snoegler ?
0
 
Answers2000Commented:
>> BTW: Motaz mentioned this overlapping memory issue :)

My point is that Motaz's original comment on overlapping is NOT correct.

Both strcpy and memcpy are NOT guaranteed to work on overlapping strings.

For memcpy, see the quote in my previous comment

For strcpy
(begin quote)
The behavior of strcpy is undefined if the source and destination strings overlap.
(end quote)

Motaz - I thought you should no what I we mean by overlapping as you brought it up, but point is that the destination memory block "overlaps" with the source block, example:

char someString[] = "Hello World" ;
char * src = someString + 5 ;
char * dest = someString ;

strcpy( dest, src ) ; /* May not work, undefined behaviour */

or

memcpy( dest, src, strlen(src)+1) ; /* Undefined behaviour too */

0
 
HahoAuthor Commented:
>>If a zero occurs anywhere in the middle of a string then memcpy is likely to
     be used.

But zeroes can happen a lot in normal string text, don't u guys agree??
And '\0' is not 0. So is the statement above correct...
My understanding is that ( after all the info) is that memcpy is good for strings that may contain '\0' (null terminator) but we should also be wary of zeroes???
hmm...

to motaz: I am sorry but I rejected your answer as snoegler has given a very good answer and also the fact u asked what overlapping is when u yourself gave the answer.. :)
any other comments is welcome...
David
0
 
Answers2000Commented:
>> But zeroes can happen a lot in normal string text, don't u guys agree??

Zeroes (I mean char c = 0 ; ) don't occur in the middle of C style strings.  All the standard library string functions (strcpy, strcat, etc.) assume 0 occurs to mark the end of the string, and NOT in the middle of the string.  The mem** (memcpy etc) functions do handle 0's in the middle of a block of memory

>>And '\0' is not 0. So is the statement above correct...

'\0' is 0, try this program to prove it

#include <stdio.h>
int main( int argc, char * argv )
{
char c1 = 0 ;
char c2 = '\0' ;

if ( c1 == c2 )
{
 printf( "The same" ) ;
} else
{
 printf( "Different" ) ;
}
return 0 ;
}



0
 
MusashiCommented:
The memxxx functions work for all binary data.  The strxxx functions work for a small but important subset of this data, ie, strings as defined by the C language.
0
 
HahoAuthor Commented:
Thanks Answer2000 ,
   I am a bit confused :(
So if I have a string
  strcpy( str1, "01234");
what will happen? In this case is the '0' a char = 0x00 or is it
0x30??

David
0
 
BayoCommented:
Hello.

memcpy it´s more general funtion, strcpy only copy NULL terminated strings and memcpy can copy all types of arrays not only char.

to use memcpy you need especify the size of the array this avoid the null termination.

memcpy repeat the char if it´s not for all the long I.E.
memcpy(MyArray,0,sizeof(MyArray)); full all the array of 0´s

0
 
MotazCommented:
I know what is overlapping but I want know it in snoegler view
0
 
Answers2000Commented:
>>So if I have a string
  strcpy( str1, "01234");
>>what will happen? In this case is the '0' a char = 0x00 or is it 0x30??

In this case it's 0x30

Consider

char c1 = '\0' ;
char c2 = '0' ;
char c3 = 0 ;

In this case c1 will contain 0x00
In this case c2 will contain 0x30
In this case c3 will contain 0x00

Characters like c1 and c3 CAN NOT occur in the middle of a string, because strcpy, strcat, etc. WILL NOT handle them.

Characters like c2 CAN occur in the middle of the string and strcpy, strcat, etc. WILL handle them.

If you read back by previous comments considering that by character 0 I mean 0x00 (the NUL character, ASCII value 0) they should make more sense.


0
 
HahoAuthor Commented:
Thanks , esp. to Answers2000 and also Sneogler who has taken great pains to explain the whole difference thing to me.. and I must say that I understand it well now... :)
Take care... until my next confused question ... ;)

David


0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now