Skip to main content
GameDev.net gamedev.net
🔒 Locked

simple question

Started by eldee Dec 3, 2002 at 4:41 PM 4 replies 400+ views
Original Post
eldee
eldee
anybody know offhand what the routine is for copying only part of an array in c++? my first thought was to traverse through it with a loop and define all of the elements manually, but im sure there's a much much better way say for example, i have this: int *LargeBoxOfApples; LargeBoxOfApples = (int*) new int[1000]; //and this: int *SmallBoxOfApples; SmallBoxOfApples = (int*) new int[100]; say i want to copy the first 100 elements of the LargeBox into the SmallBox... [edit: i know that in this example a loop would be sufficient, but for my actual purposes, i'll be dealing with many thousands of elements, not just 100] -eldee ;another space monkey; [ Forced Evolution Studios ]

::evolve:: Do NOT let Dr. Mario touch your genitals. He is not a real doctor! [edited by - eldee on December 3, 2002 5:47:57 PM]

LizardAl
LizardAl
how about


memcpy(smallBox, largeBox, sizeof(int) * 100);

?

scott
eldee
eldee
there we go.. for some reason i had memcmp stuck in my head, which
of course is for comparing memory.

thanks again guys.

-eldee
;another space monkey;
[ Forced Evolution Studios ]

::evolve::

Do NOT let Dr. Mario touch your genitals. He is not a real doctor!

eldee
eldee
hrm, i just realized that i''ll need to be able to
specify a starting and ending point in the copy buffer...
is a loop my only option here?

-eldee
;another space monkey;
[ Forced Evolution Studios ]

::evolve::

Do NOT let Dr. Mario touch your genitals. He is not a real doctor!

LizardAl
LizardAl

memcpy(smallBox, &largeBox[start], sizeof(int) * (end - start));


where end - start <= sizeof(smallBox)/sizeof(smallBox[0])

scott

edit : erm...well the sizeof won't work with new, of course, but you get the idea...


[edited by - LizardAl on December 4, 2002 1:25:13 PM]

Topic Locked

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

Sign in to reply to this topic.