[英]an array for returning int in C programming
I have a question about return an array from a function in C, I do not know why it keeps giving the result as a code dumped.我有一个关于从 C 中的 function 返回数组的问题,我不知道为什么它一直将结果作为转储代码给出。 Please help, I really really thank you.
请帮忙,我真的非常感谢你。 I set up an int list3[size3] but it seems like I have to return with a format of an array as int*list3.
我设置了一个 int list3[size3] 但似乎我必须返回一个数组格式为 int*list3。 So I set up another array as list4[size3] to copy list3 into list4.
所以我将另一个数组设置为 list4[size3] 以将 list3 复制到 list4 中。 But I am not sure it is a reason it cause code dumped, If it is.
但我不确定这是导致代码转储的原因,如果是的话。 please help me with the advice to solve the problem.
请帮助我解决问题的建议。 Thank you so much again!.
再次感谢你!。
#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;
}
}
}
https://onlinegdb.com/-ChT7PA3w https://onlinegdb.com/-ChT7PA3w
You merge
return pointer to the local array.您
merge
返回指针合并到本地数组。 It is UB as this array stops existing when the function returns.它是 UB,因为当 function 返回时,该数组不再存在。 So the returned pointer references an invalid object.
所以返回的指针引用了一个无效的 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);
}
Also use the correct type for sizes size_t
还要对尺寸
size_t
使用正确的类型
You cannot return list3
in merged
, as you are returning the address of the first element (not the array), but the whole list3
array is out of scope and lifetime once the function returns, so it is no longer available (and the pointer is pointing to a place that is no longer valid for that purpose) You need to allocate memory dynamically to allow the array to survive the function call, or use a global variable (which makes the function non-reentrant)您不能在 merge 中
return list3
,因为您要返回第一个元素的地址(不是数组),但是一旦merged
返回,整个list3
数组就超出了 scope 和生命周期,因此它不再可用(并且指针是指向一个不再有效的地方)您需要动态分配 memory 以允许数组在 function 调用中存活,或使用全局变量(这使得 ZC1C425268E687385D1AB5074F14 不可重复)
Couple of problems in your code:您的代码中有几个问题:
First:第一的:
You are returning list3
from merged()
function.您正在从 merge
merged()
function 返回list3
。 list3
is a local(automatic) non-static variable and its lifetime is limited to its scope ie the block in which it has been declared. list3
是一个本地(自动)非静态变量,它的生命周期仅限于它的 scope 即声明它的块。 Any attempt to access it outside of its lifetime lead to undefined behaviour .任何在其生命周期之外访问它的尝试都会导致未定义的行为。 Also,
list3
is VLA (Variable Length Array), they cannot have static
storage duration.另外,
list3
是 VLA(可变长度数组),它们不能有static
存储期限。 Other option is to declare list3
as pointer and allocate memory dynamically to it.其他选项是将
list3
声明为指针并动态分配 memory 给它。
Second:第二:
In the merged()
, you are dereferencing uninitialised pointer list4
.在 merge
merged()
中,您正在取消引用未初始化的指针list4
。 Dereferencing an uninitialised pointer is undefined behaviour.取消引用未初始化的指针是未定义的行为。 Allocate memory to
list4
and then use it.将memory分配给
list4
,然后使用。 Moreover, why you need list4
?此外,为什么需要
list4
? You copy the list1
and list2
to list3
and returning list3
from merged()
function.您将
list1
和list2
复制到list3
并从 merge merged()
function 返回list3
。 I do not see any use of list4
.我没有看到任何使用
list4
。 Better to remove it from merged()
function.最好从 merge
merged()
function 中删除它。
So, only one change is required in merged()
in your code:因此,在您的代码中,
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;
}
Make sure to free the dynamically allocated memory.确保释放动态分配的 memory。 In the
main()
function, you should do:在
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;
}
Couple of points, which I am leaving it up to you to learn and implement:几点,我留给你学习和实施:
There is one serious problem in your code that you not validating the user input.您的代码中存在一个严重问题,即您没有验证用户输入。 Try giving
size1
and size2
as value 0
or negative value or very big positive value and check what happens.尝试将
size1
和size2
作为值0
或负值或非常大的正值并检查会发生什么。 Handle user input appropriately.适当地处理用户输入。 Also, think over, if you really need VLA (Variable Length Array) or you can use fixed size array.
另外,考虑一下,如果你真的需要 VLA(可变长度数组)或者你可以使用固定大小的数组。 A fixed size array can be static.
固定大小的数组可以是 static。
There is scope of improvement in implementation part.实现部分有改进的scope。 Try to find out them and make improvements.
尝试找出它们并进行改进。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.