Solved

strcpy and memcpy... usages??

Posted on 1998-10-04
14
541 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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
 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SCANF - LIMIT THE NUMBER OF CHARARACTERS 1 66
How to build c program using make in mingw environment? 9 72
delete-remove 14 97
windows 10 pro lost profile. 10 45
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 opening and writing to files in the C programming language.

828 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