Skip to main content
gamedev.net
🔒 Locked

memory copying

Started by CProgrammer 4 replies 150+ views
Original Post
CProgrammer
CProgrammer
i have two pointers: int *x; int *y; now i want to copy the content of y into x BUT i want the content of y(lets say size 10) to be copied from x''s 5th slot on. At the end the following should be the case: x[5] = y[0] x[6] = y[1] and so on.(y will be deleted after the process. Please can anyone help me. Thanks in advance -CProgrammer
CWizard
CWizard
I don't really see what is problematic about it. Wouldn't something like this suffice, or am I missing something?
    void weird_copy(int *x, int *y, int size, int offset)
{
while(size--)
x[offset++] = *y++;
}

// or a perhaps faster way

void weird_copy(int *x, int *y, int size, int offset)
{
memcpy((void)(x + offset), (void)y, size * sizeof(int));
}





[edited by - CWizard on December 29, 2002 8:08:34 AM]
CProgrammer
CProgrammer
Thanks for your help the problem is solved. For those interested:
Use "memmove"

memmove(x+5, y, size);

That works. +5 specifies how much to skip in x. Weird that i never needed this earlier.
CWizard
CWizard
quote:
Original post by CProgrammer
memmove(x+5, y, size);

Note that size is in bytes, so you must multiply it with the size of the type (as in my example).
Fruny
Fruny
#include <algorithm>
std::copy( y, y + 10, x + 5 );


Make sure x is large enough.
This will work regardless of the type of the array elements.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.