Jump to content
YOUR-AD-HERE
HOSTING
TOOLS

Locked function memcpy


dEEpEst

Recommended Posts

function

[h=1]memcpy[/h]void * memcpy ( void * destination, const void * source, size_t num );

Copy block of memory

Copies the values of num bytes from the location pointed to by source directly to the memory block pointed to by destination.

 

The underlying type of the objects pointed to by both the source and destination pointers are irrelevant for this function; The result is a binary copy of the data.

 

The function does not check for any terminating null character in source - it always copies exactly num bytes.

 

To avoid overflows, the size of the arrays pointed to by both the destination and source parameters, shall be at least numbytes, and should not overlap (for overlapping memory blocks, memmove is a safer approach).

 

[h=3]Parameters[/h]destinationPointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.sourcePointer to the source of data to be copied, type-casted to a pointer of type const void*.numNumber of bytes to copy.

size_t is an unsigned integral type.

[h=3]Return Value[/h]destination is returned.

 

[h=3]Example[/h][TABLE=class: snippet]

[TR]

[TD=class: rownum, align: right]1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

[/TD]

[TD=class: source]/* memcpy example */

#include

#include

 

struct {

char name[40];

int age;

} person, person_copy;

 

int main ()

{

char myname[] = "Pierre de Fermat";

 

/* using memcpy to copy string: */

memcpy ( person.name, myname, strlen(myname)+1 );

person.age = 46;

 

/* using memcpy to copy structure: */

memcpy ( &person_copy, &person, sizeof(person) );

 

printf ("person_copy: %s, %d \n", person_copy.name, person_copy.age );

 

return 0;

}[/TD]

[TD=class: C_btnholder]

 

 

[/TD]

[/TR]

[/TABLE]

 

 

 

Output:

[TABLE=class: snippet]

[TR]

[TD=class: output]

person_copy: Pierre de Fermat, 46

 

 

[/TD]

[/TR]

[/TABLE]

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.