【发布时间】:2019-07-10 17:36:16
【问题描述】:
我有一个结构,它包含一个指向函数的指针和一个指向函数的指针数组。我将第一个指针(不是数组)作为函数的参数传递,该函数应该告诉我整数数组是否已排序(它可以按升序或降序排列,这是由 compFct 定义的,即参数中给定的函数指针)。
不幸的是,该函数正在更改我的结构中指针数组中的值(没有更改我指向参数中给定函数的指针的值)。
使用 gdb 我设法知道我的数组中的更改是什么时候发生的。它似乎在 printSorted 函数中的第一个 printf 之后被修改。
我的类型定义:
typedef int (*PtrCompFct)(int, int);
typedef int (*PtrSortFct)(int*, int, int, PtrCompFct);
结构:
typedef struct
{
int nbFct;
PtrCompFct compFct;
PtrSortFct *sortFct;
} SortCompFct_s;
这是我调用函数的方式(userChoices 属于 SortCompFct_s 类型):
printSorted(myArr, myArrSize, userChoices->compFct);
还有改变我结构的功能:
int printSorted(int *arr, int arrSize, PtrCompFct compFct)
{
for (int i=0; i<(arrSize-1); i++)
{
if (compFct(arr[i+1], arr[i]))
{
//this is when my array of pointers to function is modified
printf("The array isn't sorted\n\n");
return 0;
}
}
printf("The array is sorted\n\n");
return 1;
}
在 printf 之前使用 gdb:
(gdb) print main::userChoices->sortFct[0]
$36 = (PtrSortFct) 0x5555555548ea <quickSort>
之后:
(gdb) print main::userChoices->sortFct[0]
$37 = (PtrSortFct) 0x7fffffffddc0
如您所见,指向我的 quickSort 函数的指针已被修改。
编辑:包括简化和可验证的代码,问题是这段代码可以正常工作,即使使用 printSorted 函数
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef int (*PtrCompFct)(int, int);
typedef int (*PtrSortFct)(int*, int, int, PtrCompFct);
typedef struct
{
int nbFct;
PtrCompFct compFct;
PtrSortFct *sortFct;
} SortCompFct_s;
typedef SortCompFct_s *PtrSortCompFct_s;
void initTab(int *arr, int arrSize)
{
time_t t;
srand(time(&t));
for (int i=0; i<arrSize; i++)
{
arr[i] = rand();
}
}
int ascendingSort(int elmt1, int elmt2)
{
return (elmt1 < elmt2);
}
int descendingSort(int elmt1, int elmt2)
{
return (elmt1 > elmt2);
}
void switche(int *arr, int ind1, int ind2)
{
int temp = arr[ind1];
arr[ind1] = arr[ind2];
arr[ind2] = temp;
}
int bubbleSort(int *arr, int ind1, int ind2, PtrCompFct fctComp)
{
int sorted;
for (int i=ind1; i<ind2; i++)
{
sorted = 1;
for (int j=0; j<ind2; j++)
{
if (fctComp(arr[j+1], arr[j]))
{
switche(arr, j, j+1);
sorted = 0;
}
}
if (sorted) return 0;
}
return 0;
}
void printArr(int *arr, int arrSize)
{
for (int i=0; i<arrSize; i++)
{
printf("%16d\n", arr[i]);
}
}
int printSorted(int *arr, int arrSize, PtrCompFct compFct)
{
for (int i=0; i<arrSize-1; i++)
{
if (compFct(arr[i+1], arr[i]))
{
//this is when my array of pointers to function is modified
printf("The array isn't sorted\n\n");
return 0;
}
}
printf("The array is sorted\n\n");
return 1;
}
PtrSortCompFct_s menu(void)
{
PtrSortCompFct_s userChoices;
PtrSortFct arrSortFct[] = {bubbleSort};
if ((userChoices = malloc(3*sizeof(int))) != NULL)
{
userChoices->nbFct = 1;
userChoices->compFct = ascendingSort;
userChoices->sortFct = arrSortFct;
}
return userChoices;
}
int main(void)
{
int arrSize = 10;
int arr[arrSize];
initTab(arr, arrSize);
PtrSortCompFct_s userChoices;
if ((userChoices = malloc(3*sizeof(int))) != NULL) userChoices = menu();
printArr(arr, arrSize);
printSorted(arr, arrSize, userChoices->compFct);
userChoices->sortFct[0](arr, 0, arrSize-1, userChoices->compFct);
printArr(arr, arrSize);
printSorted(arr, arrSize, userChoices->compFct);
return 0;
}
【问题讨论】:
-
旁白:为什么
-1在for (int i=0; i<(arrSize-1); i++)中? IAC,您需要将呼叫发布到printSorted(),作为minimal reproducible example 的一部分。我怀疑你的排序功能有问题。 -
main::userChoices->sortFct在哪里/如何获得它的价值? -
是的,不应该是 -2 以避免在数组边界之外读取吗?
-
@chux -1 是因为我的参数是数组中元素的数量,而不是最后一个索引。我的排序功能正在运行,我尝试在修改后使用 gdb 将
userChoices->sortFct[0]的值设置为0x5555555548ea,并且我的程序的其余部分运行良好。 -
没有minimal reproducible example,这篇文章不足以给出一个好的答案。