更新时间:2023-01-20 12:43:50
在C数组简单地是一个连续的内存区,一个指向其开始*。因此,合并它们包括:
Arrays in C simply are a contiguous area of memory, with a pointer to their start*. So merging them involves:
的sizeof
每个元素)的malloc
)的新数组C,它是A + B的大小。的memcpy
)从A内存为C,免费
)A和B的记忆。sizeof
each element)malloc
) a new array C that is the size of A + B.memcpy
) the memory from A to C,free
) the memory of A and B.请注意,这是一个昂贵的操作,但是这是基本理论。如果您正在使用一个库,它提供了一些抽象,你可能会更好。如果A和B是比较复杂的,然后一个简单的数组(比如排序数组),您需要做聪明的复制,然后步骤3和4(参见:how我该合并具有不同的值到一个数组)两个数组。
Note that this is an expensive operation, but this is the basic theory. If you are using a library that provides some abstraction, you might be better off. If A and B are more complicated then a simple array (e.g. sorted arrays), you will need to do smarter copying then steps 3 and 4 (see: how do i merge two arrays having different values into one array).