Solved

strcpy and memcpy... usages??

Posted on 1998-10-04
14
537 Views
Last Modified: 2010-04-15
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
Comment
Question by:Haho
  • 4
  • 3
  • 3
  • +3
14 Comments
 
LVL 6

Expert Comment

by:snoegler
ID: 1253340
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
 
LVL 7

Expert Comment

by:Motaz
ID: 1253341
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
 
LVL 8

Expert Comment

by:Answers2000
ID: 1253342
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
 
LVL 6

Expert Comment

by:snoegler
ID: 1253343
Answers2000, you're right. Of course i forgot '+1'.
BTW: Motaz mentioned this overlapping memory issue :)
0
 
LVL 7

Expert Comment

by:Motaz
ID: 1253344
What did overlapping mean snoegler ?
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1253345
>> 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
 
LVL 1

Author Comment

by:Haho
ID: 1253346
>>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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 8

Expert Comment

by:Answers2000
ID: 1253347
>> 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
 
LVL 1

Expert Comment

by:Musashi
ID: 1253348
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
 
LVL 1

Author Comment

by:Haho
ID: 1253349
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
 

Accepted Solution

by:
Bayo earned 50 total points
ID: 1253350
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
 
LVL 7

Expert Comment

by:Motaz
ID: 1253351
I know what is overlapping but I want know it in snoegler view
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1253352
>>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
 
LVL 1

Author Comment

by:Haho
ID: 1253353
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now