[英]an array for returning int in C programming
我有一個關於從 C 中的 function 返回數組的問題,我不知道為什么它一直將結果作為轉儲代碼給出。 請幫忙,我真的非常感謝你。 我設置了一個 int list3[size3] 但似乎我必須返回一個數組格式為 int*list3。 所以我將另一個數組設置為 list4[size3] 以將 list3 復制到 list4 中。 但我不確定這是導致代碼轉儲的原因,如果是的話。 請幫助我解決問題的建議。 再次感謝你!。
#include<stdio.h>
int* merged(int[], int[], int, int);
void sort(int[], int);
int main() {
int size1;
printf("Enter list1: ");
scanf("%d", &size1);
int list1[size1];
for (int i = 0; i < size1; i++) {
scanf("%d", &list1[i]);
}
int size2;
printf("Enter list2: ");
scanf("%d", &size2);
int list2[size2];
for (int i = 0; i < size2; i++) {
scanf("%d", &list2[i]);
}
int* list3 = merged(list1, list2, size1, size2);
printf("The merged list is: ");
int size3 = size1 + size2;
for (int i = 0; i < size3; i++) {
printf("%d ", list3[i]);
}
printf("\n");
}
int* merged(int list1[], int list2[], int size1, int size2) {
int list3[size1 + size2];
for (int i = 0; i < size1; i++) {
list3[i] = list1[i];
}
int count = size1;
for (int i = 0; i < size2; i++) {
list3[count] = list2[i];
count++;
}
int size3 = size1 + size2;
sort(list3, size3);
int* list4;
for (int i = 0; i < size3; i++) {
list4[i] = list3[i];
}
return list3;
}
void sort(int list3[], int size3) {
for (int i = 0; i < size3; i++) {
int min = list3[i];
int min_index = i;
for (int j = i + 1; j < size3; j++) {
if (list3[j] < min) {
min = list3[j];
min_index = j;
}
}
if (min_index != i) {
list3[min_index] = list3[i];
list3[i] = min;
}
}
}
您merge
返回指針合並到本地數組。 它是 UB,因為當 function 返回時,該數組不再存在。 所以返回的指針引用了一個無效的 object。
int *merged(const int * restrict list1, const int * restrict list2, size_t size1, size_t size2)
{
int *result = NULL;
int l1size = size1 * !!list1, l2size = size2 * !!list2;
size_t newsize = l1size + l2size;
if(newsize)
{
result = malloc(newsize * sizeof(*result));
if(result)
{
if(l1size) memcpy(result, list1, l1size * sizeof(*result));
if(l2size) memcpy(result + l1size, list2, l2size * sizeof(*result));
}
}
return result;
}
void printList(const int * restrict list, size_t size)
{
if(size && list)
{
for(size_t index = 0; index < size; index++)
printf("[%2zu] = %3d\n", index, list[index]);
}
}
void initList(int * restrict list, size_t size, int maxval)
{
if(size && list)
{
for(size_t index = 0; index < size; index++)
list[index] = rand() % maxval;
}
}
int main() {
size_t size1 = 20;
size_t size2 = 10;
int list1[size1], list2[size2];
initList(list1, size1, 100);
initList(list2, size2, 100);
printList(list1, size1);
printf("----------------\n");
printList(list2, size2);
printf("----------------\n");
int *list3 = merged(list1, list2, size1, size2);
printList(list3, size2 + size1);
printf("----------------\n");
free(list3);
}
還要對尺寸size_t
使用正確的類型
您不能在 merge 中return list3
,因為您要返回第一個元素的地址(不是數組),但是一旦merged
返回,整個list3
數組就超出了 scope 和生命周期,因此它不再可用(並且指針是指向一個不再有效的地方)您需要動態分配 memory 以允許數組在 function 調用中存活,或使用全局變量(這使得 ZC1C425268E687385D1AB5074F14 不可重復)
您的代碼中有幾個問題:
第一的:
您正在從 merge merged()
function 返回list3
。 list3
是一個本地(自動)非靜態變量,它的生命周期僅限於它的 scope 即聲明它的塊。 任何在其生命周期之外訪問它的嘗試都會導致未定義的行為。 另外, list3
是 VLA(可變長度數組),它們不能有static
存儲期限。 其他選項是將list3
聲明為指針並動態分配 memory 給它。
第二:
在 merge merged()
中,您正在取消引用未初始化的指針list4
。 取消引用未初始化的指針是未定義的行為。 將memory分配給list4
,然后使用。 此外,為什么需要list4
? 您將list1
和list2
復制到list3
並從 merge merged()
function 返回list3
。 我沒有看到任何使用list4
。 最好從 merge merged()
function 中刪除它。
因此,在您的代碼中, merged()
中只需要進行一項更改:
int*merged(int list1[], int list2[], int size1, int size2) {
int * list3 = malloc ((size1 + size2) * sizeof (int));
if (list3 == NULL) {
//handle the allocation failure
//I am exiting..
printf ("Failed to allocate memory\n");
exit (EXIT_FAILURE);
}
....
....
....
sort (list3, size3);
// removed list4 from code
return list3;
}
確保釋放動態分配的 memory。 在main()
function 中,你應該這樣做:
int main (void) {
....
....
....
for (int i = 0; i < size3; i++) {
printf("%d ", list3[i]);
}
printf("\n");
// free dynamically allocated memory
free (list3);
return 0;
}
幾點,我留給你學習和實施:
您的代碼中存在一個嚴重問題,即您沒有驗證用戶輸入。 嘗試將size1
和size2
作為值0
或負值或非常大的正值並檢查會發生什么。 適當地處理用戶輸入。 另外,考慮一下,如果你真的需要 VLA(可變長度數組)或者你可以使用固定大小的數組。 固定大小的數組可以是 static。
實現部分有改進的scope。 嘗試找出它們並進行改進。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.