我有一个包含指向函数的指针和一个函数指针数组的结构。我将第一个指针(不是数组)作为函数的参数传递,该函数应该告诉我整数数组是否排序(它可以按升序或降序排列,这是由compFct定义的,这是指向参数中给出的函数的指针。
不幸的是,该函数正在改变我的struct中指针数组中的值(不会更改我的指针在参数中给出的函数的值)。
使用gdb我设法知道我的数组何时进行了更改。它似乎在printSorted函数中的第一个printf之后被修改。
我的typedef:
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;
}
在printf之前使用gdb:...以及之后:
问题的根本原因在于如何初始化userChoices->sortFct
(您没有显示执行此初始化的代码)。
该数组指向悬空堆或堆栈内存,并且调用printf
会覆盖该内存。
if ((userChoices = malloc(6*sizeof(int))) != NULL) userChoices = menu();
该代码完全是假的:为userChoices
堆分配内存然后立即用userChoices
的返回值覆盖menu()
只会泄漏内存。正如评论中所提到的,6*sizeof(int)
也完全是假的。
我猜你的menu()
看起来像这样:
struct SortCompFct_s* menu()
{
struct SortCompFct_s ret;
ret.compFct = &SomeFunc;
ret.sortFct = malloc(...);
ret.sortFct[0] = &quickSort;
return &ret; // Oops: returning address of a local!
}
如果这实际上就是你所做的那样,悬空堆栈就是你的问题。你应该打开最大编译器警告(如果使用GCC,则为-Wall -Wextra
),因此编译器会告诉你你做错了什么。
更新:
我的猜测很接近:
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;
}
问题是userChoices->sortFct
指向本地(堆栈)变量arrSortFct
。从menu
返回后,该局部变量变为无效,并且在那时userChoices->sortFct
指向悬空堆栈(正如我猜测的那样)。
以下是编写此函数的正确方法(为清晰起见,省略了malloc
返回的错误检查):
PtrSortCompFct_s menu(void)
{
PtrSortCompFct_s userChoices;
PtrSortFct arrSortFct[] = {bubbleSort};
if ((userChoices = malloc(sizeof(*userChoices)) != NULL)
{
userChoices->nbFct = 1;
userChoices->compFct = ascendingSort;
userChoices->sortFct = malloc(sizeof(arrSortFct));
memcpy(userChoices->sortFct, arrSortFct, sizeof(arrSortFct));
}
return userChoices;
}
你还应该像这样修理你的main
:
PtrSortCompFct_s userChoices;
PtrSortCompFct_s userChoices = menu();
... use userChoices ...
free(userChoices->sortFct);
free(sortFct);
return 0;