[英]About the dynamic memory allocation for arrays in C language
我要實現的是創建兩個排序后的 arrays 的合並數組,例如 [0, 1, 2, 3, 4] + [2, 4, 6, 8, 10] => [0, 1, 2, 2, 3, 4, 4, 8, 10],通過比較每個數組中的兩個元素
我正在嘗試將這種算法應用於動態分配的 arrays 並將指針 arguments 投擲到定制的 merge() function。 請參考以下摘錄
int* merge(int*, int*);
int main(int argc, const char * argv[]) {
int* arrayA;
int* arrayB;
int* mergedArray;
int index;
arrayA = (int*) malloc(sizeof(int) * 5);
arrayB = (int*) malloc(sizeof(int) * 5);
//filling out the array A with number elements like [0, 1, 2, 3, 4]
for(index = 0; index < 5; index++){
*(arrayA + sizeof(int) * index) = index;
printf("%d", *(arrayA + sizeof(int) * index));
}
//filling out the array A with number elements like [2, 4, 6, 8, 10]
for(index = 0; index < 5; index++){
*(arrayB + sizeof(int) * index) = (index + 1) * 2;
printf("%d", *(arrayB + sizeof(int) * index));
}
printf("\n");
mergedArray = (int *) merge(arrayA, arrayB);
for(index = 0; index < 10; index++){
printf("%d", *(mergedArray + sizeof(int) * index));
}
return 0;
}
合並函數如下
//My take on merge() function
int *merge(int *arrayA, int *arrayB) {
int *mergedArray;
int i = 0, j = 0, k = 0; // i for arrayA / j for arrayB / k for mergedArray
int arrayALength;
int arrayBLength;
mergedArray = (int *)malloc(sizeof(int) * 10);
arrayALength = 5;
arrayBLength = 5;
while (i < arrayALength && j < arrayBLength) {
printf("%d / %d\n", *(arrayA + (i) * sizeof(int)), *(arrayB + (j) * sizeof(int)));
if (*(arrayA + (sizeof(int) * i)) < *(arrayB + (sizeof(int) * j))) {
*(mergedArray + (k++ * sizeof(int))) = *(arrayA + (i++ * sizeof(int)));
printf("%d", *(mergedArray + (k - 1) * sizeof(int)));
} else {
*(mergedArray + (k++ * sizeof(int))) = *(arrayB + (j++ * sizeof(int)));
printf("%d", *(mergedArray + (k - 1) * sizeof(int)));
}
printf("\n");
}
for (; i < arrayALength; i++) {
*(mergedArray + (k++ * sizeof(int))) = *(arrayA + (i * sizeof(int)));
}
for (; j < arrayBLength; j++) {
*(mergedArray + (k++ * sizeof(int))) = *(arrayB + (j * sizeof(int)));
}
return mergedArray;
}
結果是……
01234
246810
0 / 2
0
1 / 2
1
2 / 2
2
2 / 4
2
4 / 4
4
4 / 0
0
4 / 1
1
4 / 2
2
0122401240Program ended with exit code: 0
如果您查看結果中的第一行“01234”,我非常有信心 [0, 1, 2, 3, 4] 存儲在 arrayA 指向的 memory 內部,但在 merge() function 內部如果我打印相應的元素,它會顯示 [0, 1, 2, 4],中間省略元素“3”。
不僅程序的最終結果“0122401240”確實表明我的代碼中存在邏輯謬誤,這是我找不到的。
如果你能看到邏輯錯誤,請毫不猶豫地指出一個,你能告訴我為什么分配的 memory 中的元素有差異嗎?
你的代碼真的很復雜,你可以簡化很多
解決您的問題的一種方法是使這兩個 arrays 連續,以便您可以像處理單個數組一樣處理它,如下面的代碼所示
#include <stdlib.h>
#include <stdio.h>
int mysort(const void* a, const void* b){
return (*(int*)a-*(int*)b);
}
int main(){
int sz1= 5, sz2=5;
int* arr1 = malloc((sz1+sz2)*sizeof(int)); // Allocate for both arrays
int* arr2 = &arr1[sz1]; // Go to the 2nd array
my_fill(arr1); // Setup your arrays as you wish
my_fill(arr2);
qsort(arr1, sz1+sz2, sizeof(int), mysort); // Sort both arrays using the standard qsort.
}
顯然,如果您想使用自定義排序算法,例如歸並排序,這是可能的。 您可以用您的自定義算法替換qsort
。
您的代碼應該受益於輸入 arrays 已經排序的事實。 只需取最小值並將其復制到 output 數組中。 對於每個輸入數組,維護一個索引,告訴您下一個未使用的元素在哪里。
喜歡:
#include <stdio.h>
#include <stdlib.h>
int* mergeSortedArraysToSingleSortedArray(int* arrA, size_t szA, int* arrB, size_t szB)
{
int* res = malloc((szA + szB) * sizeof *arrA);
if (res == NULL) return NULL;
size_t iA = 0;
size_t iB = 0;
size_t i = 0;
// Merge from A and B
while (iA < szA && iB < szB)
{
if (arrA[iA] <= arrB[iB])
{
res[i] = arrA[iA];
++iA;
}
else
{
res[i] = arrB[iB];
++iB;
}
++i;
}
// Take rest of A (if any)
while (iA < szA)
{
res[i] = arrA[iA];
++iA;
++i;
}
// Take rest of B (if any)
while (iB < szB)
{
res[i] = arrB[iB];
++iB;
++i;
}
return res;
}
int main(void)
{
int arrA[] = {0, 1, 2, 3, 4};
int arrB[] = {2, 4, 6, 8, 10};
size_t szA = sizeof(arrA)/sizeof(*arrA);
size_t szB = sizeof(arrB)/sizeof(*arrB);
int* arrMerged = mergeSortedArraysToSingleSortedArray(arrA, szA, arrB, szB);
if (arrMerged)
{
for (size_t i = 0; i < szA + szB; ++i)
{
printf("%d ", arrMerged[i]);
}
printf("\n");
}
return 0;
}
Output:
0 1 2 2 3 4 4 6 8 10
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.