memmove

The memmove function copies the content of a memory block into another memory location. It doesn't check if the destination memory buffer has enough space to receive the copy, so it should be used with care. This function is similar to memcpy, however it is safe to use even if the memory blocks overlap.

This function is declared in string.h, and has a behavior similar to strcpy. However, it doesn't require that the memory block contains only characters. It also doesn't require that it be NULL terminated.

The memcpy function requires three arguments. The first argument is a pointer to the destination memory. The second argument is a pointer to the source memory block. The third argument is the size of the block we want to copy, given in a integral variable of type size_t.

The return value of memmove is equal to the memory address passed as the first parameter for the function.

The following code shows an example of memcpy use:

#include        
#include        

#define MEM_SIZE 10
#define BLOCK_SIZE 7

int main()
{
        int source[MEM_SIZE];
        int i;
        for (i=0; i<MEM_SIZE; i++) {
                source[i] = i+1;
        }
        memmove(source+MEM_SIZE-BLOCK_SIZE, source, BLOCK_SIZE*sizeof(int));
        for (i=MEM_SIZE-BLOCK_SIZE; i<MEM_SIZE; i++) {
                printf("source[%d] = %d\n", i, source[i]);
        }
        return 0;
}

This code will print the following:

source[3] = 1
source[4] = 2
source[5] = 3
source[6] = 4
source[7] = 5
source[8] = 6
source[9] = 7
Article created on 2008-08-19 22:02:41

Post a comment