C++ Copy Array Without dependency on original -
how copy array new array if want new array not dependent on original array in c++. know has using pointers new c++ , don't quite understand how works
short answer
if have 2 arrays defined 10-byte each, unsigned char arrayone[10], arraytwo[10];, independent, , copying 1 other done way:
for(unsigned i=0; < 10; i++) arraytwo[i] = arrayone[i]; you use memcpy:
memcpy(arraytwo, arrayone, 10); // 10 bytes copied arrayone arraytwo and use std::copy:
std::copy(arrayone, arrayone+10, arraytwo); // copy 10 elements arrayone arraytwo long answer
if perform copy of array, new array won't have dependency on original after copy, if correctly understanding mean "dependency".
it more clear if think of memory point of view, , not language point of view. array sequence of values stored in memory. lets consider code:
unsigned char myarray[10] { 12, 14, 16, 18, 20, 22, 24, 26, 28, 30 }; after compilation , execution, these 10 bytes stored @ given location in memory. imagine location 0x1234 (for simplicity, consider our computer have 16-bits memory):
location |0x1234|0x1235|0x1236| .... --------------------------------------------------- content | 12 | 14 | 16 | ..... --------------------------------------------------- in context, have 10 bytes stored, , 2-byte variable called myarray taking 2 bytes in memory, this:
myarray myarray values --------------- ---------------------- location |0x0333|0x0334| .... |0x1234|0x1235|0x1236| ..... --------------------------------------------------- content | 0x12 | 0x34 | .... | 12 | 14 | 16 | ..... --------------------------------------------------- then, when this:
myarray[2] = 99; you doing this:
*(myarray + 2) = 99; which, in turn, means this:
*(0x1234 + 2) = 99; this way pointers work. myarray pointer: variable holds address array starts. knowing array has elements consecutive, , each element occupies 1 byte, accessing element 8 accessing start of array plus 8, *(myarray + 8) or myarray[8].
so, if wanted make independent copy of array, need 10 bytes located in different place in memory, , pointer start of these new 10 bytes: is, array. code it:
unsigned char myarray[10] { 12, 14, 16, 18, 20, 22, 24, 26, 28, 30 }; unsigned char otherarray[10]; for(unsigned i=0; < 10; i++) otherarray[i] = myarray[i]; this creates 2 independent arrays same content: 2 spaces of 10 bytes, 2 pointers locations in memory. let's opperation second array:
otherarray[2] = 99; imagining otherarray = 0x4455, same as:
*(0x4455 + 2) = 99; let's imagine result of in our imaginary 16-bits memory:
myarray myarray values --------------- ---------------------- location |0x0333|0x0334| .... |0x1234|0x1235|0x1236| ..... --------------------------------------------------- content | 0x12 | 0x34 | .... | 12 | 14 | 16 | ..... --------------------------------------------------- ^^^^^^ original left untouched otherarray otherarray values --------------- ---------------------- location |0x0422|0x0423| .... |0x4455|0x4456|0x4457| ..... --------------------------------------------------- content | 0x44 | 0x55 | .... | 12 | 14 | 99 | ..... --------------------------------------------------- ^^^^^ changed value then, have our 2 arrays, 1 of them being copy of other, independent.
Comments
Post a Comment