How to copy a chuck of memory?

P

PengYu.UT

Hi,

Suppose I have a void pointer pointing to a chunk of memory, I want to
copy the contents to another chunk of memory. Of course I can convert
void pointer to char pointer and use a for loop to copy each char, but
I fill it is slow. Is there any way I can copy the whole chunk at one
shot.

Best wishes,
Peng
 
D

David Resnick

Hi,

Suppose I have a void pointer pointing to a chunk of memory, I want to
copy the contents to another chunk of memory. Of course I can convert
void pointer to char pointer and use a for loop to copy each char, but
I fill it is slow. Is there any way I can copy the whole chunk at one
shot.

Best wishes,
Peng

memcpy if the chunks can't overlap. memmove if they can.

-David
 
M

Malcolm

Suppose I have a void pointer pointing to a chunk of memory, I want to
copy the contents to another chunk of memory. Of course I can convert
void pointer to char pointer and use a for loop to copy each char, but
I fill it is slow. Is there any way I can copy the whole chunk at one
shot.
Normally when you want to do this, the array will be an array of integers or
whatever.
So by writing the function
copyint(int *dest, int *source, int N)

you can copy each int in a single instruction, which will usually be faster
than copying over one byte at a time.

memcpy() will often do this internally by looking for arguments which are a
whole number of integers and suitably aligned. You can do this yourself, but
obviously as soon as you start looking at pointer alignments directly you
have lost portability.

Also, some computers come with a DMA engine, which will copy large chunks of
memory asynchronously. You can't write a C interface to such a device in
ANSI C, of course, for often some C-callable function will be provided to
use it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top